Class JsonSchemaValidator

java.lang.Object
org.apache.juneau.bean.jsonschema.JsonSchemaValidator
All Implemented Interfaces:
PropertyValidator

public final class JsonSchemaValidator extends Object implements PropertyValidator
Validates values against a JsonSchema bean per the JSON Schema Draft 2020-12 specification.

This is the juneau-bean-jsonschema implementation of the commons-side PropertyValidator SPI. It is registered as a PropertyValidatorFactory via ServiceLoader so that marshalling-side code (e.g. MarshalledPropertyPostProcessor) can install schema validation closures on bean properties without taking a compile-time dependency on this module.

Supported keywords (v1):
  • Any type: type, enum, const.
  • Numeric: minimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf (Draft 2020-12 numeric form for the exclusive bounds).
  • String: minLength, maxLength, pattern.
  • Array: minItems, maxItems, uniqueItems, items (recursive — single-schema form only; tuple form via JsonSchemaArray is not yet validated).
  • Object: minProperties, maxProperties, required, properties (recursive on Map-shaped values).
Deferred keywords:
  • format — informational only by default; validators may treat it as advisory.
  • patternProperties, additionalProperties, prefixItems, contains, minContains, maxContains, dependentRequired, dependentSchemas.
  • Composition keywords: allOf, anyOf, oneOf, not, if/then/else.
  • References: $ref, $defs (the bean carries them; the validator does not resolve them yet).
Descriptive keywords (always ignored):
  • title, summary, description, examples, $comment, deprecated, readOnly, writeOnly.
Java-value mapping:

Java values are matched against JSON Schema type keywords as follows:

  • string — CharSequence or Character.
  • integer — integral Number (Byte, Short, Integer, Long, BigInteger) or a floating-point number whose value has no fractional part.
  • number — any Number.
  • boolean — Boolean.
  • array — Collection or Java array.
  • object — Map or any other reference type (treated as a bean — the marshalling layer installs separate validators on the bean's own @Schema-annotated properties).
  • null — null.
  • any — always matches.
Nested validation:

For property-level validation, the validator recurses into:

  • items — every element of an array / collection is validated against the items schema.
  • properties on a Map value — each declared sub-property is validated against its sub-schema.

The validator does not recurse into Java beans via reflection. Inner @Schema annotations on a nested bean's own properties are validated independently by the marshalling layer's per-property install hooks.

Thread safety:
Instances are immutable and safe to share across threads. The compiled Pattern for the pattern keyword is cached on construction.
Interruptible pattern matching:

A pattern keyword can encode a catastrophically-backtracking regular expression that a hostile input can drive into effectively-unbounded work. Matcher never checks Thread.isInterrupted(), so such a match cannot be stopped by interrupting the matching thread - it runs to completion, pinning a core even after the caller has given up. To make a runaway match abortable, the string is fed to the matcher through an JsonSchemaValidator.InterruptibleCharSequence: because the matcher reads its input exclusively via CharSequence.charAt(int), every character read periodically samples the thread's interrupt status and, once set, throws to unwind the match promptly. A caller that bounds validation on a worker thread (for example an MCP tools/call DoS guard) can therefore reclaim the thread by interrupting it, rather than leaking a core to a match that ignores cancellation.

See Also:
Since:
10.0.0
See Also: