Skip to main content

Schema

Struct Schema 

Source
pub struct Schema<T> { /* private fields */ }
Expand description

A JSON Schema paired with a function that validates a JSON value and converts it into T.

The JSON Schema is produced lazily on first access and cached; cloning a schema shares the cache and the validator.

Implementations§

Source§

impl<T> Schema<T>
where T: DeserializeOwned + JsonSchema + 'static,

Source

pub fn derived() -> Schema<T>

Derives the schema from T (draft-07, additionalProperties: false on objects) and validates by deserializing into T.

Source

pub fn derived_with(dialect: SchemaDialect) -> Schema<T>

Like Schema::derived with an explicit dialect.

Source§

impl<T> Schema<T>
where T: DeserializeOwned + 'static,

Source

pub fn typed_from_json_schema(schema: Value) -> Schema<T>

Uses a raw JSON Schema and validates by deserializing into T.

With the json-schema-validation feature the value is first checked against the JSON Schema, so constraints that serde cannot express (ranges, patterns) are enforced as well.

Source§

impl Schema<Value>

Source

pub fn from_json_schema(schema: Value) -> Schema<Value>

Uses a raw JSON Schema; the value is returned unchanged after validation.

With the json-schema-validation feature the value is checked against the schema (compiled lazily on first validation); without it every value passes.

Source

pub fn empty_object() -> Schema<Value>

The empty object schema ({type: object, properties: {}, additionalProperties: false}) used when no schema is given.

Source

pub fn any() -> Schema<Value>

A schema that accepts any JSON value.

Source§

impl<T> Schema<T>
where T: 'static,

Source

pub fn lazy( json_schema: impl FnOnce() -> Value + Send + 'static, validate: impl Fn(Value) -> Result<T, TypeValidationError> + Send + Sync + 'static, ) -> Schema<T>

Creates a schema from a lazily generated JSON Schema and a validator.

Source

pub fn with_json_schema_and_validator( json_schema: Value, validate: impl Fn(Value) -> Result<T, TypeValidationError> + Send + Sync + 'static, ) -> Schema<T>

Creates a schema from a JSON Schema value and a validator.

Source

pub fn with_validator( self, validate: impl Fn(Value) -> Result<T, TypeValidationError> + Send + Sync + 'static, ) -> Schema<T>

Replaces the validator, keeping the JSON Schema.

Source

pub fn transformed( &self, transform: SchemaTransform, ) -> Result<Schema<T>, SchemaError>

Returns a copy whose JSON Schema is rewritten by transform immediately; validation is unchanged.

§Errors

Returns crate::SchemaError::UnsupportedTransform if the schema cannot be represented by the requested transform.

Source

pub fn erased(&self) -> Schema<Value>

Returns a Schema<Value> that runs this schema’s validation but yields the original JSON value.

Source§

impl<T> Schema<T>

Source

pub fn json_schema(&self) -> &Value

Returns the JSON Schema, generating it on first access.

Source

pub fn validate(&self, value: Value) -> Result<T, TypeValidationError>

Validates value and converts it into T.

§Errors

Returns TypeValidationError when the value does not match.

Trait Implementations§

Source§

impl<T> Clone for Schema<T>

Source§

fn clone(&self) -> Schema<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Schema<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Schema<T>

§

impl<T> !UnwindSafe for Schema<T>

§

impl<T> Freeze for Schema<T>

§

impl<T> Send for Schema<T>
where Arc<dyn Fn(Value) -> Result<T, TypeValidationError> + Sync + Send>: Send,

§

impl<T> Sync for Schema<T>
where Arc<dyn Fn(Value) -> Result<T, TypeValidationError> + Sync + Send>: Sync,

§

impl<T> Unpin for Schema<T>
where Arc<dyn Fn(Value) -> Result<T, TypeValidationError> + Sync + Send>: Unpin,

§

impl<T> UnsafeUnpin for Schema<T>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.