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
(schema schema-uri
&
{:as _opts
:keys [schema-validator-config]
:or {schema-validator-config schema-validator-config}})
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-validator-config
(optional): Custom
SchemaValidatorsConfig
(default: this namespace's
schema-validator-config
).
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-validator-config` (optional): Custom `SchemaValidatorsConfig` (default: this namespace's `schema-validator-config`). 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")
Default reusable SchemaValidatorsConfig
instance.
This configures the validator to:
nullable
fields correctlyJSON_PATH
for error paths in validation resultsThis config can be reused for schema load/validation for performance and consistency.
Default reusable `SchemaValidatorsConfig` 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.
(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)
(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).:validation-result
: Custom function to extract validation errors
(defaults to this namespace's validation-result
).Behavior:
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.
(validation-result r)
Extracts validation errors from a ValidationResult object.
Arguments:
r
(ValidationResult): The result object returned by a validation call.Returns: A sequence of maps, one for each validation error, containing:
:type
: Error type string from validation:path
: JSON path to offending element:error
: Error code string:message
: Human-friendly error message
Returns nil
if the validation result contains no errors.Extracts validation errors from a ValidationResult object. Arguments: - `r` (ValidationResult): The result object returned by a validation call. Returns: A sequence of maps, one for each validation error, containing: - `:type`: Error type string from validation - `:path`: JSON path to offending element - `:error`: Error code string - `:message`: Human-friendly error message Returns `nil` if the validation result contains no errors.
cljdoc builds & hosts documentation for Clojure/Script libraries
Ctrl+k | Jump to recent docs |
← | Move to previous article |
→ | Move to next article |
Ctrl+/ | Jump to the search field |