SchemaLike

Trait SchemaLike 

Source
pub trait SchemaLike: Send + Sync {
    type Output;

    // Required methods
    fn validate(
        &self,
        value: &Value,
        path: &JsonPath,
    ) -> Validation<Self::Output, SchemaErrors>;
    fn validate_to_value(
        &self,
        value: &Value,
        path: &JsonPath,
    ) -> Validation<Value, SchemaErrors>;

    // Provided methods
    fn validate_with_context(
        &self,
        value: &Value,
        path: &JsonPath,
        _context: &ValidationContext,
    ) -> Validation<Self::Output, SchemaErrors> { ... }
    fn validate_to_value_with_context(
        &self,
        value: &Value,
        path: &JsonPath,
        _context: &ValidationContext,
    ) -> Validation<Value, SchemaErrors> { ... }
    fn collect_refs(&self, _refs: &mut Vec<String>) { ... }
}
Expand description

A trait for schema types that can validate JSON values.

SchemaLike enables schema polymorphism, allowing different schema types to be composed together for validating nested structures. Any type that implements this trait can be used as a field schema in an ObjectSchema.

The Send + Sync bounds allow schemas to be safely shared across threads and used in trait objects like Box<dyn SchemaLike>.

§Example

use postmortem::{Schema, JsonPath};
use serde_json::json;

// Both StringSchema and IntegerSchema implement SchemaLike,
// so they can be used as field schemas in an object schema.
let object = Schema::object()
    .field("name", Schema::string().min_len(1))
    .field("age", Schema::integer().positive());

Required Associated Types§

Source

type Output

The output type produced by successful validation.

Required Methods§

Source

fn validate( &self, value: &Value, path: &JsonPath, ) -> Validation<Self::Output, SchemaErrors>

Validates a value against this schema.

Returns Validation::Success with the validated value on success, or Validation::Failure with accumulated errors on failure.

Source

fn validate_to_value( &self, value: &Value, path: &JsonPath, ) -> Validation<Value, SchemaErrors>

Validates a value and returns the result as a serde_json::Value.

This method allows schema types with different output types to be used uniformly in object schemas where all fields are stored as Value.

Provided Methods§

Source

fn validate_with_context( &self, value: &Value, path: &JsonPath, _context: &ValidationContext, ) -> Validation<Self::Output, SchemaErrors>

Validates a value with registry context for schema reference resolution.

This method is used when validating with a registry that contains named schemas. The context provides access to the registry for resolving schema references and tracks depth to prevent infinite loops in circular references.

The default implementation ignores the context and delegates to validate(), which preserves backward compatibility for schemas that don’t use references.

§Example
use postmortem::{Schema, JsonPath};
use serde_json::json;

// Most schemas work the same with or without context
let schema = Schema::string().min_len(1);
let result = schema.validate(&json!("hello"), &JsonPath::root());
assert!(result.is_success());
Source

fn validate_to_value_with_context( &self, value: &Value, path: &JsonPath, _context: &ValidationContext, ) -> Validation<Value, SchemaErrors>

Validates a value with context and returns the result as a serde_json::Value.

This combines validate_with_context and validate_to_value for use in container schemas where all fields must return Value.

The default implementation delegates to validate_to_value() for backward compatibility.

Source

fn collect_refs(&self, _refs: &mut Vec<String>)

Collects all schema reference names used by this schema.

This method traverses the schema structure and adds the names of all referenced schemas to the provided vector. It’s used by the registry to validate that all references can be resolved.

The default implementation does nothing, which is correct for schemas that don’t contain references. Container schemas (Object, Array) and combinator schemas (AnyOf, AllOf, etc.) override this to traverse their children.

§Example
use postmortem::{Schema, SchemaLike};

// Most schemas have no references
let schema = Schema::string().min_len(1);
let mut refs = Vec::new();
schema.collect_refs(&mut refs);
assert_eq!(refs.len(), 0);

Implementors§