Validator

Struct Validator 

Source
pub struct Validator<R: Read> { /* private fields */ }
Expand description

The JSON validator.

Implementations§

Source§

impl<R: Read> Validator<R>

Source

pub fn new(read: R) -> Self

Creates a new Validator instance with the given reader without any constraints. You could prefer to use the from_slice, from_str, or from_reader functions

Source

pub fn with_max_depth(self, max_depth: usize) -> Self

Sets the maximum depth of the JSON structure.

Source

pub fn with_max_string_length(self, max_string_length: usize) -> Self

Sets the maximum length of strings.

Source

pub fn with_max_array_entries(self, max_array_length: usize) -> Self

Sets the maximum number of entries in arrays.

Source

pub fn with_max_object_entries(self, max_object_length: usize) -> Self

Sets the maximum number of entries in objects.

Source

pub fn with_max_object_entry_name_length( self, max_object_entry_name_length: usize, ) -> Self

Sets the maximum length of object entry names.

Source

pub fn allow_duplicate_object_entry_name(self) -> Self

Allows duplicate object entry names.

Source

pub fn disallow_duplicate_object_entry_name(self) -> Self

Disallows duplicate object entry names.

Source

pub fn validate(self) -> Result<(), ValidatorError>

Validates the JSON payload in a single call, and consumes current Validator instance.

§Returns
  • Ok(()) - If the JSON payload is valid and did not violate any constraints.
  • Err - If the JSON payload is invalid or violates any constraints.
§Errors
  • Error - If the JSON payload is invalid or violates any constraints.

In the extreme case, the error might return an Err and indicate this crate is running into buggy code. Please report it to the crate maintainer if you see this error.

Source

pub fn validate_with_steps( &mut self, steps: usize, ) -> Result<bool, ValidatorError>

Validates the JSON payload in multiple calls.

§Arguments
  • steps - The number of steps to validate the JSON payload, roughly corresponds to the number of tokens processed.
§Returns
  • Ok(true) - If the validation is finished and no errors.
  • Ok(false) - If the validation is not finished yet, and you should call this function again.
  • Err - If the JSON payload is invalid or violates any constraints.
§Errors
  • Error - If the JSON payload is invalid or violates any constraints.

In the extreme case, the error might return an Err and indicate this crate is running into buggy code. Please report it to the crate maintainer if you see this error.

§WARNING

The validator will be invalidated once this method returns an Err or Ok(true), and calling any methods for Validator instance is undefined behavior.

Auto Trait Implementations§

§

impl<R> Freeze for Validator<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for Validator<R>
where R: RefUnwindSafe,

§

impl<R> Send for Validator<R>
where R: Send,

§

impl<R> Sync for Validator<R>
where R: Sync,

§

impl<R> Unpin for Validator<R>
where R: Unpin,

§

impl<R> UnwindSafe for Validator<R>
where R: UnwindSafe,

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, 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.