jsonschema

Struct Validator

source
pub struct Validator { /* 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

source

pub fn options() -> ValidationOptions

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);
source

pub fn new(schema: &Value) -> Result<Validator, ValidationError<'static>>

Create a validator using the default options.

source

pub fn compile(schema: &Value) -> Result<Validator, ValidationError<'static>>

👎Deprecated since 0.20.0: Use Validator::new instead

Create a validator using the default options.

DEPRECATED: Use Validator::new instead.

source

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.

source

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 Validator::validate.

source

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

use serde_json::json;

let schema = json!({
    "title": "string value",
    "type": "string"
});
let instance = json!("some string");

let validator = jsonschema::validator_for(&schema)
    .expect("Invalid schema");

let output = validator.apply(&instance).basic();
assert_eq!(
    serde_json::to_value(output)?,
    json!({
        "valid": true,
        "annotations": [
            {
                "keywordLocation": "",
                "instanceLocation": "",
                "annotations": {
                    "title": "string value"
                }
            }
        ]
    })
);
source

pub fn draft(&self) -> Draft

The Draft which was used to build this validator.

source

pub fn config(&self) -> Arc<ValidationOptions>

The ValidationOptions that were used to build this validator.

Trait Implementations§

source§

impl Debug for Validator

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T