Struct valico::json_schema::scope::Scope
source · pub struct Scope { /* private fields */ }
Implementations§
source§impl Scope
impl Scope
pub fn new() -> Scope
pub fn without_formats(version: SchemaVersion) -> Scope
pub fn with_formats<F>(build_formats: F, version: SchemaVersion) -> Scopewhere F: FnOnce(&mut FormatBuilders),
pub fn set_version(self, version: SchemaVersion) -> Self
sourcepub fn supply_defaults(self) -> Self
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 theitems
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 appliednot
: 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 discardedanyOf
: 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 returnedoneOf
: just as foranyOf
, apart from checking that the instance does not validate the remaining schemasallOf
: 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).
pub fn compile( &mut self, def: Value, ban_unknown: bool ) -> Result<Url, SchemaError>
pub fn compile_with_id( &mut self, id: &Url, def: Value, ban_unknown: bool ) -> Result<(), SchemaError>
pub fn compile_and_return( &mut self, def: Value, ban_unknown: bool ) -> Result<ScopedSchema<'_>, SchemaError>
pub fn compile_and_return_with_id<'a>( &'a mut self, id: &Url, def: Value, ban_unknown: bool ) -> Result<ScopedSchema<'a>, SchemaError>
pub fn add_keyword<T>(&mut self, keys: Vec<&'static str>, keyword: T)where T: Keyword + 'static,
pub fn resolve<'a>(&'a self, id: &Url) -> Option<ScopedSchema<'a>>
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere T: Any,
source§fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.