Struct jsonschema::JSONSchema
source · pub struct JSONSchema { /* private fields */ }
Expand description
The structure that holds a JSON Schema nodes.
Implementations§
source§impl JSONSchema
impl JSONSchema
sourcepub fn options() -> CompilationOptions
pub fn options() -> CompilationOptions
Return a default CompilationOptions
that can configure
JSONSchema
compilation flow.
Using options you will be able to configure the draft version
to use during JSONSchema
compilation
Example of usage:
let maybe_jsonschema: Result<JSONSchema, _> = JSONSchema::options()
.with_draft(Draft::Draft7)
.compile(&schema);
sourcepub fn compile(schema: &Value) -> Result<JSONSchema, ValidationError<'_>>
pub fn compile(schema: &Value) -> Result<JSONSchema, ValidationError<'_>>
Compile the input schema for faster validation.
sourcepub fn validate<'instance>(
&'instance self,
instance: &'instance Value,
) -> Result<(), ErrorIterator<'instance>>
pub fn validate<'instance>( &'instance self, instance: &'instance Value, ) -> Result<(), ErrorIterator<'instance>>
Run validation against instance
and return an iterator over ValidationError
in the error case.
sourcepub fn is_valid(&self, instance: &Value) -> bool
pub fn is_valid(&self, instance: &Value) -> bool
Run validation against instance
but return a boolean result instead of an iterator.
It is useful for cases, where it is important to only know the fact if the data is valid or not.
This approach is much faster, than validate
.
sourcepub const fn apply<'a, 'b>(&'a self, instance: &'b Value) -> Output<'a, 'b>
pub const fn apply<'a, 'b>(&'a self, instance: &'b Value) -> Output<'a, 'b>
Apply the schema and return an Output
. No actual work is done at this point, the
evaluation of the schema is deferred until a method is called on the Output
. This is
because different output formats will have different performance characteristics.
§Examples
“basic” output format
let schema_json = serde_json::json!({
"title": "string value",
"type": "string"
});
let instance = serde_json::json!{"some string"};
let schema = JSONSchema::options().compile(&schema_json).unwrap();
let output: BasicOutput = schema.apply(&instance).basic();
let output_json = serde_json::to_value(output).unwrap();
assert_eq!(output_json, serde_json::json!({
"valid": true,
"annotations": [
{
"keywordLocation": "",
"instanceLocation": "",
"annotations": {
"title": "string value"
}
}
]
}));
sourcepub fn config(&self) -> Arc<CompilationOptions>
pub fn config(&self) -> Arc<CompilationOptions>
The CompilationOptions
that were used to compile this schema