pub struct Schema { /* private fields */ }Expand description
JSON Schema-backed message contract for a channel.
Implementations§
Source§impl Schema
impl Schema
Sourcepub fn new(definition: Value) -> Result<Self, SchemaValidationError>
pub fn new(definition: Value) -> Result<Self, SchemaValidationError>
Builds a schema from a JSON Schema definition.
§Errors
Returns SchemaValidationError::InvalidSchema when the definition cannot be compiled.
Sourcepub fn with_id(
id: SchemaId,
definition: Value,
) -> Result<Self, SchemaValidationError>
pub fn with_id( id: SchemaId, definition: Value, ) -> Result<Self, SchemaValidationError>
Builds a schema from a JSON Schema definition with an explicit identifier.
§Errors
Returns SchemaValidationError::InvalidSchema when the definition cannot be compiled.
Sourcepub fn definition(&self) -> &Value
pub fn definition(&self) -> &Value
Returns the wrapped JSON Schema definition.
Sourcepub fn validate<Payload>(
&self,
payload: Payload,
) -> Result<(), SchemaValidationError>
pub fn validate<Payload>( &self, payload: Payload, ) -> Result<(), SchemaValidationError>
Validates JSON payload bytes after applying known schema defaults.
§Errors
Returns SchemaValidationError when the payload is not JSON, cannot receive required
defaults, or does not match this schema.
Sourcepub fn validate_and_apply_defaults<Payload>(
&self,
payload: Payload,
) -> Result<Vec<u8>, SchemaValidationError>
pub fn validate_and_apply_defaults<Payload>( &self, payload: Payload, ) -> Result<Vec<u8>, SchemaValidationError>
Validates payload bytes and returns the normalized JSON bytes to deliver.
§Errors
Returns SchemaValidationError when the payload cannot be parsed, normalized, validated,
or serialized for delivery.
Sourcepub fn evolve_add_field(
&self,
name: impl Into<String>,
field_schema: Value,
default: Value,
) -> Result<Self, SchemaValidationError>
pub fn evolve_add_field( &self, name: impl Into<String>, field_schema: Value, default: Value, ) -> Result<Self, SchemaValidationError>
Evolves an object schema by adding a required field with a default value.
§Errors
Returns SchemaValidationError when this is not an object schema, the field schema is
invalid, the default does not match the field schema, or the evolved schema cannot compile.