Liking cljdoc? Tell your friends :D

s-exp.legba.json-schema

JSON Schema validation utilities. Provides helpers to load, cache, and validate JSON Schemas

JSON Schema validation utilities.
Provides helpers to load, cache, and validate JSON Schemas
raw docstring

read-schemaclj

(read-schema schema-uri
             &
             {:as _opts
              :keys [schema-registry-config validate-schema]
              :or {schema-registry-config schema-registry-config
                   validate-schema true}})

Loads and builds a JSON Schema validator instance from a URI or file path.

Arguments:

  • schema-uri (string): Location of the JSON Schema (file:/..., http:/..., classpath:/..., etc)

  • :schema-registry-config (optional): Custom SchemaRegistryConfig (default: this namespace's schema-registry-config).

  • :validate-schema (optional, default true): When true, validates the loaded schema against the JSON Schema draft 2020-12 meta-schema and throws if it does not conform.

Returns:

An instance of the JSON Schema validator (com.networknt.schema.JsonSchema). This object can be reused to validate multiple values or payloads.

Example: (schema "classpath:///tmp/foo.schema.json") (schema "file:///data/schema/bar.json") (schema "https://schemas.org/example.schema.json")

Loads and builds a JSON Schema validator instance from a URI or file path.

Arguments:
  - `schema-uri` (string): Location of the JSON Schema (file:/..., http:/...,
classpath:/..., etc)

  - `:schema-registry-config` (optional): Custom
`SchemaRegistryConfig` (default: this namespace's `schema-registry-config`).

  - `:validate-schema` (optional, default true): When true, validates the
loaded schema against the JSON Schema draft 2020-12 meta-schema and throws if
it does not conform.

Returns:

  An instance of the JSON Schema validator (`com.networknt.schema.JsonSchema`).
  This object can be reused to validate multiple values or payloads.

Example:
  (schema "classpath:///tmp/foo.schema.json")
  (schema "file:///data/schema/bar.json")
  (schema "https://schemas.org/example.schema.json")
raw docstring

read-schema-strclj

(read-schema-str s
                 &
                 {:as _opts
                  :keys [schema-uri schema-registry-config validate-schema]
                  :or {schema-registry-config schema-registry-config
                       validate-schema true}})

Loads and builds a JSON Schema validator from a raw JSON string s.

An optional :schema-uri can be provided to anchor $ref resolution; if omitted a random urn:legba:<uuid> URI is generated.

Options mirror schema: :schema-registry-config, :validate-schema (default true).

Returns a com.networknt.schema.JsonSchema instance.

Loads and builds a JSON Schema validator from a raw JSON string `s`.

An optional `:schema-uri` can be provided to anchor `$ref` resolution; if
omitted a random `urn:legba:<uuid>` URI is generated.

Options mirror `schema`: `:schema-registry-config`, `:validate-schema`
(default true).

Returns a `com.networknt.schema.JsonSchema` instance.
raw docstring

schemacljdeprecated

Alias for read-schema. Loads and builds a JSON Schema validator from a URI.

Alias for `read-schema`. Loads and builds a JSON Schema validator from a URI.
raw docstring

schema-registry-configclj

Default reusable SchemaRegistryConfig instance.

This configures the validator to:

  • Enable JSON Schema reference preloading and caching
  • Enable format assertions (per the JSON Schema spec)
  • Set maximum reference nesting depth to 40
  • Handle nullable fields correctly
  • Use JSON_PATH for error paths in validation results

This config can be reused for schema load/validation for performance and consistency.

Default reusable `SchemaRegistryConfig` instance.

This configures the validator to:
- Enable JSON Schema reference preloading and caching
- Enable format assertions (per the JSON Schema spec)
- Set maximum reference nesting depth to 40
- Handle `nullable` fields correctly
- Use `JSON_PATH` for error paths in validation results

This config can be reused for schema load/validation for performance and consistency.
raw docstring

validateclj

(validate schema
          val
          &
          {:as _opts
           :keys [validation-result]
           :or {validation-result validation-result}})

Validates a value against a previously loaded or constructed schema.

Arguments:

  • schema: The schema object returned from schema
  • val: The input to validate. Can be a JsonNode or a JSON string.
  • :validation-result (optional): Override for the function used to extract validation errors (defaults to this namespace's validation-result).

Returns: A sequence of error maps (see validation-result), or nil if valid.

Example: (validate myschema "{"foo":42}") (validate myschema my-jackson-json-node)

Validates a value against a previously loaded or constructed schema.

Arguments:
  - `schema`: The schema object returned from `schema`
  - `val`: The input to validate. Can be a JsonNode or a JSON string.
  - `:validation-result` (optional): Override for the function used to extract
validation errors (defaults to this namespace's `validation-result`).

Returns:
  A sequence of error maps (see `validation-result`), or nil if valid.

Example:
  (validate myschema "{"foo":42}")
  (validate myschema my-jackson-json-node)
raw docstring

validate!clj

(validate! schema val & {:as opts})

Validates a value against a given JSON Schema and throws if invalid.

Arguments:

  • schema (JsonSchema): A schema instance created by the schema function.
  • val: The data to validate (can be a Jackson JsonNode or a JSON string).
  • Optional keyword arguments:
    • :validation-result: Custom function to extract validation errors (defaults to this namespace's validation-result).

Behavior:

  • If validation succeeds (no errors), returns nil.
  • If validation fails, throws an ex-info exception with: :type --> :s-exp.legba.json-schema/failed-validation :errors --> Sequence of error maps (see validation-result). :val --> The input value that failed validation.

Example: (validate! myschema "{"name":42}") ; throws if invalid (validate! myschema my-jackson-json-node)

Useful for workflows where validation failure should abort or be handled via exception.

Validates a value against a given JSON Schema and throws if invalid.

Arguments:
  - `schema` (JsonSchema): A schema instance created by the `schema` function.
  - `val`: The data to validate (can be a Jackson JsonNode or a JSON string).
  - Optional keyword arguments:
      - `:validation-result`: Custom function to extract validation errors
        (defaults to this namespace's `validation-result`).

Behavior:
  - If validation succeeds (no errors), returns nil.
  - If validation fails, throws an ex-info exception with:
      :type   --> :s-exp.legba.json-schema/failed-validation
      :errors --> Sequence of error maps (see `validation-result`).
      :val    --> The input value that failed validation.

Example:
  (validate! myschema "{"name":42}") ; throws if invalid
  (validate! myschema my-jackson-json-node)

Useful for workflows where validation failure should abort or be handled via exception.
raw docstring

validate-schema!clj

(validate-schema! user-schema)

Validates a loaded JSON Schema against the JSON Schema draft 2020-12 meta-schema. Throws an ex-info with :type :s-exp.legba.json-schema/invalid-schema and :errors if the schema does not conform.

Validates a loaded JSON Schema against the JSON Schema draft 2020-12
meta-schema. Throws an ex-info with `:type
:s-exp.legba.json-schema/invalid-schema` and `:errors` if the schema does not
conform.
raw docstring

validation-resultclj

(validation-result r)

Extracts and formats schema validation errors from a ValidationResult object.

Takes a NetworkNT ValidationResult and returns a vector of error maps, where each map contains:

  • :path (string) JSON path of the error location
  • :pointer (string) JSON pointer to the schema fragment
  • :location (string) Instance location in the JSON document
  • :detail (string) Validation error message/detail

Returns nil if there are no errors. Useful for turning validator output into a more consumable shape for clients, APIs, or error reporting.

Extracts and formats schema validation errors from a ValidationResult object.

Takes a NetworkNT ValidationResult and returns a vector of error maps,
where each map contains:
  - :path     (string)   JSON path of the error location
  - :pointer  (string)   JSON pointer to the schema fragment
  - :location (string)   Instance location in the JSON document
  - :detail   (string)   Validation error message/detail

Returns nil if there are no errors. Useful for turning validator output into
a more consumable shape for clients, APIs, or error reporting.
raw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close