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