Expand description
This crate supports JsonSchema validation for drafts 2020-12, 2019-09, 7, 6 and 4.
let mut schemas = Schemas::new(); // container for compiled schemas
let mut compiler = Compiler::new();
let sch_index = compiler.compile("schema.json", &mut schemas)?;
let instance: Value = serde_json::from_reader(File::open("instance.json")?)?;
let valid = schemas.validate(&instance, sch_index).is_ok();If schema file has no $schema, it assumes latest draft.
You can override this:
compiler.set_default_draft(Draft::V7);The use of this option is HIGHLY encouraged to ensure continued correct operation of your schema. The current default value will not stay the same over time.
§Examples
- example_from_strings: loading schemas from Strings
- example_from_https: loading schemas from
http(s) - example_custom_format: registering custom format
- example_custom_content_encoding: registering custom contentEncoding
- example_custom_content_media_type: registering custom contentMediaType
§Compile Errors
println!("{compile_error}");
println!("{compile_error:#}"); // prints cause if anyUsing alterate form in display will print cause if any.
This will be useful in cases like CompileError::LoadUrlError,
as it would be useful to know whether the url does not exist or
the resource at url is not a valid json document.
§Validation Errors
ValidationError may have multiple causes resulting
in tree of errors.
println!("{validation_error}") prints:
jsonschema validation failed with file:///tmp/customer.json#
at '': missing properties 'age'
at '/billing_address': missing properties 'street_address', 'city', 'state'The alternate form println!("{validation_error:#}") prints:
jsonschema validation failed with file:///tmp/customer.json#
[I#] [S#/required] missing properties 'age'
[I#/billing_address] [S#/properties/billing_address/$ref] validation failed with file:///tmp/address.json#
[I#/billing_address] [S#/required] missing properties 'street_address', 'city', 'state'here I refers to the instance document and S refers to last schema document.
for example:
- after line 1:
Srefers tofile:///tmp/customer.json - after line 3:
Srefers tofile://tmp/address.json
§Output Formats
ValidationError can be converted into following output formats:
- flag
validation_error.flag_output() - basic
validation_error.basic_output() - detailed
validation_error.detailed_output()
The output object implements serde::Serialize.
It also implement Display to print json:
println!("{output}"); // prints unformatted json
println!("{output:#}"); // prints indented jsonStructs§
- Absolute
Keyword Location - The absolute, dereferenced location of the validating keyword
- Compiler
- JsonSchema compiler.
- Decoder
- Defines Decoder for
contentEncoding. - File
Loader - Flag
Output - Simplest output format, merely the boolean result.
- Format
- Defines format for
formatkeyword. - Instance
Location - The location of the JSON value within the instance being validated
- Keyword
Path - JsonPointer in schema.
- Media
Type - Defines Mediatype for
contentMediaType. - Output
Unit - Single OutputUnit used in Basic/Detailed output formats.
- Schema
Index - Identifier to compiled schema.
- Schemas
- Collection of compiled schemas.
- Scheme
UrlLoader - Types
- Set of
Types - Validation
Error - Error type for validation failures.
Enums§
- Compile
Error - Error type for compilation failures.
- Draft
- Supported draft versions
- Error
Kind - A list specifying general categories of validation errors.
- Instance
Token - Token in InstanceLocation json-pointer.
- Output
Error - Error of
OutputUnit. - Schema
Token - Token for schema.
- Type
- JSON data types for JSONSchema
Traits§
- UrlLoader
- A trait for loading json from given
url