Class JsonSchemaValidator
- All Implemented Interfaces:
PropertyValidator
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 viaJsonSchemaArrayis not yet validated). - Object:
minProperties,maxProperties,required,properties(recursive onMap-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—CharSequenceorCharacter.integer— integralNumber(Byte, Short, Integer, Long, BigInteger) or a floating-point number whose value has no fractional part.number— anyNumber.boolean—Boolean.array—Collectionor Java array.object—Mapor 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.propertieson aMapvalue — 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 compiledPattern 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:
-
Method Summary
Modifier and TypeMethodDescriptionJsonSchema<?>Returns the schema this validator was built from.static JsonSchemaValidatorof(JsonSchema<?> schema) Builds a validator from aJsonSchemabean.static JsonSchemaValidatorBuilds a validator from a JSON-Schema-shapedJsonMap.voidValidates the given value against the schema this validator was built from.
-
Method Details
-
of
Builds a validator from aJsonSchemabean.- Parameters:
schema- The schema to validate against. Must not benull , or anIllegalArgumentExceptionis thrown.- Returns:
- A new validator.
-
of
Builds a validator from a JSON-Schema-shapedJsonMap.Equivalent to
of(JsonSchemaBeanGenerator.toBean(schemaMap)).- Parameters:
schemaMap- The schema map. Must not benull , or anIllegalArgumentExceptionis thrown.- Returns:
- A new validator.
-
getSchema
Returns the schema this validator was built from.- Returns:
- The schema.
-
validate
Description copied from interface:PropertyValidatorValidates the given value against the schema this validator was built from.- Specified by:
validatein interfacePropertyValidator- Parameters:
value- The value to validate. May benull .- Throws:
SchemaValidationException- If the value violates the schema.
-