pub struct Validator<F: Json = SerdeJson> { /* private fields */ }Expand description
A compiled JSON Schema validator.
This structure represents a JSON Schema that has been parsed and compiled into an efficient internal representation for validation. It contains the root node of the schema tree and the configuration options used during compilation.
Implementations§
Source§impl Validator<SerdeJson>
impl Validator<SerdeJson>
Sourcepub fn options<'i>() -> ValidationOptions<'i>
pub fn options<'i>() -> ValidationOptions<'i>
Create a default ValidationOptions for configuring JSON Schema validation.
Use this to set the draft version and other validation parameters.
§Example
let validator = jsonschema::options()
.with_draft(Draft::Draft7)
.build(&schema);Sourcepub fn async_options<'i>() -> ValidationOptions<'i, Arc<dyn AsyncRetrieve>>
pub fn async_options<'i>() -> ValidationOptions<'i, Arc<dyn AsyncRetrieve>>
Create a default ValidationOptions configured for async validation.
Use this to set the draft version and other validation parameters when working with schemas that require async reference resolution.
§Example
let schema = json!({
"$ref": "https://example.com/schema.json"
});
let validator = jsonschema::async_options()
.with_draft(Draft::Draft202012)
.build(&schema)
.await?;For sync validation, use options() instead.
Source§impl<F: Json> Validator<F>
impl<F: Json> Validator<F>
Sourcepub fn validate<'i>(
&self,
instance: F::Node<'i>,
) -> Result<(), ValidationError<'i>>
pub fn validate<'i>( &self, instance: F::Node<'i>, ) -> Result<(), ValidationError<'i>>
Validate instance against schema and return the first error if any.
§Errors
Returns the first ValidationError describing why instance does not satisfy the schema.
Sourcepub fn iter_errors<'i>(&'i self, instance: F::Node<'i>) -> ErrorIterator<'i> ⓘ
pub fn iter_errors<'i>(&'i self, instance: F::Node<'i>) -> ErrorIterator<'i> ⓘ
Run validation against instance and return an iterator over ValidationError in the error case.
Sourcepub fn is_valid(&self, instance: F::Node<'_>) -> bool
pub fn is_valid(&self, instance: F::Node<'_>) -> 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 Validator::validate.
Sourcepub fn evaluate(&self, instance: F::Node<'_>) -> Evaluation
pub fn evaluate(&self, instance: F::Node<'_>) -> Evaluation
Evaluate the schema and expose structured output formats.