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,
impl<T> Schema<T>where
T: DeserializeOwned + JsonSchema + 'static,
Sourcepub fn derived() -> Schema<T>
pub fn derived() -> Schema<T>
Derives the schema from T (draft-07, additionalProperties: false
on objects) and validates by deserializing into T.
Sourcepub fn derived_with(dialect: SchemaDialect) -> Schema<T>
pub fn derived_with(dialect: SchemaDialect) -> Schema<T>
Like Schema::derived with an explicit dialect.
Source§impl<T> Schema<T>where
T: DeserializeOwned + 'static,
impl<T> Schema<T>where
T: DeserializeOwned + 'static,
Sourcepub fn typed_from_json_schema(schema: Value) -> Schema<T>
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>
impl Schema<Value>
Sourcepub fn from_json_schema(schema: Value) -> Schema<Value>
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.
Sourcepub fn empty_object() -> Schema<Value>
pub fn empty_object() -> Schema<Value>
The empty object schema ({type: object, properties: {}, additionalProperties: false}) used when no schema is given.
Source§impl<T> Schema<T>where
T: 'static,
impl<T> Schema<T>where
T: 'static,
Sourcepub fn lazy(
json_schema: impl FnOnce() -> Value + Send + 'static,
validate: impl Fn(Value) -> Result<T, TypeValidationError> + Send + Sync + 'static,
) -> Schema<T>
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.
Sourcepub fn with_json_schema_and_validator(
json_schema: Value,
validate: impl Fn(Value) -> Result<T, TypeValidationError> + Send + Sync + 'static,
) -> Schema<T>
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.
Sourcepub fn with_validator(
self,
validate: impl Fn(Value) -> Result<T, TypeValidationError> + Send + Sync + 'static,
) -> Schema<T>
pub fn with_validator( self, validate: impl Fn(Value) -> Result<T, TypeValidationError> + Send + Sync + 'static, ) -> Schema<T>
Replaces the validator, keeping the JSON Schema.
Sourcepub fn transformed(
&self,
transform: SchemaTransform,
) -> Result<Schema<T>, SchemaError>
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§impl<T> Schema<T>
impl<T> Schema<T>
Sourcepub fn json_schema(&self) -> &Value
pub fn json_schema(&self) -> &Value
Returns the JSON Schema, generating it on first access.
Sourcepub fn validate(&self, value: Value) -> Result<T, TypeValidationError>
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.