JsonSchema class final
A JSON Schema document (draft 2020-12) that checks decoded JSON.
Use it to check JSON you receive before you use it: a request body, a
file, a message. validate takes the decoded JSON (what jsonDecode
returns) and lists every place that does not fit, with its JSON
pointer. ObjectMapper uses it for classes marked with
@JsonSchemaSource when it has a schemaResolver.
Supported keywords: type (also a list, and integer), enum,
const, properties, required, additionalProperties,
patternProperties, propertyNames, minProperties,
maxProperties, dependentRequired, dependentSchemas, items,
prefixItems, minItems, maxItems, uniqueItems, contains,
minContains, maxContains, minLength and maxLength (in Unicode
code points), pattern (a Dart RegExp in Unicode mode, not
anchored), minimum, maximum, exclusiveMinimum,
exclusiveMaximum, multipleOf, format, allOf, anyOf, oneOf,
not, if / then / else, $ref, $defs / definitions, $id,
$anchor, and the schemas true and false. Other keywords (title,
description, default, examples, ...) are ignored.
unevaluatedProperties, unevaluatedItems and $dynamicRef are not
supported (ignored).
format is checked for date-time, date, time (RFC 3339; a time
needs its offset), email (something @ a domain, no spaces), uri
(with a scheme), uri-reference, uuid, ipv4 and ipv6; other
formats are ignored.
A $ref is resolved against the base URI (the $ids around it): a
fragment (#/$defs/item, #anchor) in the same document, another
document through the registry by its absolute URI. A $ref that
loops without reaching a value is reported, not followed forever.
final schema = JsonSchema.parse('''
{"type": "object", "required": ["id"],
"properties": {"id": {"type": "integer", "minimum": 1}}}
''');
final result = schema.validate(jsonDecode('{"id": 0}'));
result.isValid; // false
result.violations.first.path; // /id
result.violations.first.message; // must be at least 1
Spring (Java): Spring has no counterpart; Java applications use a
library such as networknt's json-schema-validator.
Constructors
-
JsonSchema(Map<
String, Object?> document, {Uri? id, JsonSchemaRegistry? registry}) -
The schema
document, with the URIidit is known by (its$idis resolved against it) and theregistrythat holds the documents its$refs point to. - JsonSchema.parse(String text, {Uri? id, JsonSchemaRegistry? registry})
-
The schema written as JSON
text;trueandfalseare the schemas that accept everything and nothing. Throws a FormatException for text that is not JSON or not a schema.factory
Properties
-
document
→ Map<
String, Object?> -
The decoded schema document.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- id → Uri?
-
The URI of the document: its
$id(resolved against the URI it was given), else that URI;nullwhen it has neither.final - registry → JsonSchemaRegistry?
-
The documents other documents'
$refs are looked up in;nullwhen the schema refers only to itself.final - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
override
-
validate(
Object? instance) → JsonSchemaResult -
Checks
instance, decoded JSON (Map<String, Object?>,List,num,String,boolornull), and returns every violation.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited