livery_openapi_validate (livery v0.2.0)

View Source

Request validation against a JSON-Schema subset, plus a middleware that rejects malformed request bodies with 422.

validate/2 checks a decoded JSON term (maps with binary keys, lists, binaries, numbers, booleans, null) against a schema map.

Supported keywords:

  • core: type (single or a list of types), enum, const
  • numbers: minimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf
  • strings: minLength, maxLength, pattern
  • objects: required, properties, additionalProperties (false or a schema), minProperties, maxProperties
  • arrays: items, minItems, maxItems, uniqueItems
  • combinators: allOf, anyOf, oneOf

Schema keys may be atoms or binaries; property names inside properties must be binaries (they are matched against decoded JSON keys). It is a pragmatic subset, not a complete JSON Schema implementation ($ref, if/then/else, patternProperties, and dependentSchemas are not supported).

The call/3 middleware reads #{body_schema => Schema} from its state, decodes the request's JSON body, validates it, and on failure short-circuits with a 422 whose body lists the errors. On success it stores the decoded body under meta(body, Decoded).

Summary

Functions

Validate a decoded JSON term against a schema subset.

Types

error()

-type error() :: {binary(), binary()}.

schema()

-type schema() :: map().

Functions

call/3

-spec call(livery_req:req(), livery_middleware:next(), #{body_schema := schema()}) -> livery_resp:resp().

validate(Value, Schema)

-spec validate(term(), schema()) -> ok | {error, [error()]}.

Validate a decoded JSON term against a schema subset.