SchemaRegistry

Struct SchemaRegistry 

Source
pub struct SchemaRegistry { /* private fields */ }
Expand description

A thread-safe registry for storing and retrieving named schemas.

The registry enables schema reuse through references. Schemas can be registered with string names and then referenced from other schemas using Schema::ref_().

§Thread Safety

The registry uses Arc<RwLock<...>> for thread-safe access:

  • Multiple threads can validate concurrently (read-only access)
  • Registration operations are serialized (write access)

§Example

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

let registry = SchemaRegistry::new();

// Register base schemas
registry.register("Email", Schema::string()).unwrap();
registry.register("UserId", Schema::integer().positive()).unwrap();

// Register schemas that use references
registry.register("User", Schema::object()
    .field("id", Schema::ref_("UserId"))
    .field("email", Schema::ref_("Email"))
).unwrap();

Implementations§

Source§

impl SchemaRegistry

Source

pub fn new() -> Self

Creates a new empty schema registry with default max depth (100).

Source

pub fn with_max_depth(self, depth: usize) -> Self

Sets the maximum reference depth for circular reference prevention.

The default max depth is 100. When validating recursive schemas, if the reference chain exceeds this depth, validation fails with a max_depth_exceeded error.

§Example
use postmortem::SchemaRegistry;

let registry = SchemaRegistry::new()
    .with_max_depth(50);
Source

pub fn register<S>( &self, name: impl Into<String>, schema: S, ) -> Result<(), RegistryError>
where S: ValueValidator + 'static,

Registers a schema with the given name.

Returns an error if a schema with the same name is already registered.

§Errors

Returns RegistryError::DuplicateName if the name is already registered.

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

let registry = SchemaRegistry::new();
registry.register("Email", Schema::string()).unwrap();

// Duplicate registration fails
assert!(registry.register("Email", Schema::string()).is_err());
Source

pub fn get(&self, name: &str) -> Option<Arc<dyn ValueValidator>>

Retrieves a schema by name.

Returns None if no schema with the given name is registered.

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

let registry = SchemaRegistry::new();
registry.register("Email", Schema::string()).unwrap();

let schema = registry.get("Email");
assert!(schema.is_some());

let missing = registry.get("Unknown");
assert!(missing.is_none());
Source

pub fn validate_refs(&self) -> Vec<String>

Validates that all schema references can be resolved.

Returns a list of reference names that don’t exist in the registry. This should be called after all schemas are registered to ensure reference integrity.

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

let registry = SchemaRegistry::new();
registry.register("User", Schema::object()
    .field("id", Schema::ref_("UserId"))  // UserId not registered!
).unwrap();

let unresolved = registry.validate_refs();
assert_eq!(unresolved, vec!["UserId"]);
Source

pub fn validate( &self, schema_name: &str, value: &Value, ) -> Result<Validation<Value, SchemaErrors>, RegistryError>

Validates a value against a named schema.

This is the main entry point for validation when using the registry. It looks up the schema by name and validates the value with full support for schema references and depth tracking.

§Errors

Returns RegistryError::SchemaNotFound if the schema name doesn’t exist.

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

let registry = SchemaRegistry::new();
registry.register("User", Schema::object()
    .field("name", Schema::string().min_len(1))
    .field("age", Schema::integer().positive())
).unwrap();

let result = registry.validate("User", &json!({
    "name": "Alice",
    "age": 30
})).unwrap();

assert!(result.is_success());

Trait Implementations§

Source§

impl Clone for SchemaRegistry

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Default for SchemaRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl RegistryAccess for SchemaRegistry

Source§

fn get_schema(&self, name: &str) -> Option<Arc<dyn ValueValidator>>

Gets a schema by name from the registry.

Auto Trait Implementations§

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<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> 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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.