Struct valico::json_schema::scope::Scope

source ·
pub struct Scope { /* private fields */ }

Implementations§

source§

impl Scope

source

pub fn new() -> Scope

source

pub fn without_formats(version: SchemaVersion) -> Scope

source

pub fn with_formats<F>(build_formats: F, version: SchemaVersion) -> Scopewhere F: FnOnce(&mut FormatBuilders),

source

pub fn set_version(self, version: SchemaVersion) -> Self

source

pub fn supply_defaults(self) -> Self

use default values to compute an enriched version of the input

JSON schema foresees the default attribute in any schema but does not assign it a specific semantics; it is merely suggested that it may be used to supply default values, e.g. for use in an interactive editor. The only specification is that the value of a default attribute shall be a JSON value that SHOULD validate its schema if supplied.

This feature activates defaults as a mechanism for schema authors to include defaults such that consuming programs can rely upon the presence of such paths even if the validated JSON object does not contain values at these paths. This allows for example non-required properties to be parsed as mandatory by supplying the fallback within the schema.

The most basic usage is to add defaults to scalar properties (like strings or numbers). A more interesting aspect is that defaults bubble up through the property tree:

  • an element of properties with a default value will create a default value for its parent unless that one declares a default itself
  • if an array is given as the value of an items property and all schemas in that array provide a default, then a default is created for the schema containing the items clause unless that schema declares a default itself
  • the default of a $ref schema is the default of its referenced schema

When validating an instance against the thus enriched schema, each path that has a default in the schema and no value in the instance will have the default added at that path (a copy will be made and returned within the ValidationState structure).

The following validators interact additionally with the defaults:

  • contains: if there is an object in the array that validates the supplied schema, then that object is outfitted with the defaults of that schema; all other array elements remain unchanged (i.e. only the first match gets defaults)
  • dependencies: if the instance triggers a dependent schema and validates it, then that schema’s defaults will be applied
  • not: the supplied schema is used to validate a copy of the instance with defaults added to determine whether to reject the original instance, but the enriched instance is then discarded
  • anyOf: the search of a schema for which the supplied instance is valid is conducted with enriched instances according to the schema being tried; the first enrichted instance that validates the schema is returned
  • oneOf: just as for anyOf, apart from checking that the instance does not validate the remaining schemas
  • allOf: first, make one pass over the supplied schemas, handing each one the enriched instance from the previous (aborting in case of errors); second, another such pass, starting with the result from the first; third, a check whether the enrichment results from the two passes match (it is an error if they are different — this is an approximation, but a reasonable one)

Please note that supplying default values this way can lead to a schema that equates to the false schema, i.e. does not match any instance, so don’t try to be too clever, especially with the not, allOf, and oneOf validators.

Caveat emptor

The order in which validators are applied to an instance is UNDEFINED apart from the rule that properties and items will be moved to the front (but the order between these is UNDEFINED as well). Therefore, if one validator depends on the fact that a default value has been injected by processing another validator, then the result is UNDEFINED (with the exception stated in the previous sentence).

source

pub fn compile( &mut self, def: Value, ban_unknown: bool ) -> Result<Url, SchemaError>

source

pub fn compile_with_id( &mut self, id: &Url, def: Value, ban_unknown: bool ) -> Result<(), SchemaError>

source

pub fn compile_and_return( &mut self, def: Value, ban_unknown: bool ) -> Result<ScopedSchema<'_>, SchemaError>

source

pub fn compile_and_return_with_id<'a>( &'a mut self, id: &Url, def: Value, ban_unknown: bool ) -> Result<ScopedSchema<'a>, SchemaError>

source

pub fn add_keyword<T>(&mut self, keys: Vec<&'static str>, keyword: T)where T: Keyword + 'static,

source

pub fn resolve<'a>(&'a self, id: &Url) -> Option<ScopedSchema<'a>>

Trait Implementations§

source§

impl Debug for Scope

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Scope

§

impl Send for Scope

§

impl !Sync for Scope

§

impl Unpin for Scope

§

impl !UnwindSafe for Scope

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> Downcast for Twhere T: Any,

source§

fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.