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
impl SchemaRegistry
Sourcepub fn with_max_depth(self, depth: usize) -> Self
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);Sourcepub fn register<S>(
&self,
name: impl Into<String>,
schema: S,
) -> Result<(), RegistryError>where
S: ValueValidator + 'static,
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());Sourcepub fn get(&self, name: &str) -> Option<Arc<dyn ValueValidator>>
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());Sourcepub fn validate_refs(&self) -> Vec<String>
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"]);Sourcepub fn validate(
&self,
schema_name: &str,
value: &Value,
) -> Result<Validation<Value, SchemaErrors>, RegistryError>
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());