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());Sourcepub fn to_json_schema(&self) -> Value
pub fn to_json_schema(&self) -> Value
Exports all registered schemas as a JSON Schema document with $defs.
Returns a JSON Schema document following draft 2020-12 with all registered
schemas under the $defs key.
§Example
use postmortem::{SchemaRegistry, Schema};
let registry = SchemaRegistry::new();
registry.register("UserId", Schema::integer().positive()).unwrap();
registry.register("Email", Schema::string().email()).unwrap();
let json_schema = registry.to_json_schema();
// Returns:
// {
// "$schema": "https://json-schema.org/draft/2020-12/schema",
// "$defs": {
// "UserId": { "type": "integer", "exclusiveMinimum": 0 },
// "Email": { "type": "string", "format": "email" }
// }
// }Sourcepub fn export_schema(&self, name: &str) -> Option<Value>
pub fn export_schema(&self, name: &str) -> Option<Value>
Exports a single schema as a standalone JSON Schema document.
Returns a JSON Schema document for the named schema, including all
referenced schemas under $defs. Returns None if the schema doesn’t exist.
§Example
use postmortem::{SchemaRegistry, Schema};
let registry = SchemaRegistry::new();
registry.register("UserId", Schema::integer().positive()).unwrap();
registry.register("User", Schema::object()
.field("id", Schema::ref_("UserId"))
.field("email", Schema::string().email())
).unwrap();
let user_schema = registry.export_schema("User").unwrap();
// Returns a complete JSON Schema with User schema and UserId in $defsTrait Implementations§
Source§impl Clone for SchemaRegistry
impl Clone for SchemaRegistry
Source§impl Default for SchemaRegistry
impl Default for SchemaRegistry
Source§impl RegistryAccess for SchemaRegistry
impl RegistryAccess for SchemaRegistry
Source§fn get_schema(&self, name: &str) -> Option<Arc<dyn ValueValidator>>
fn get_schema(&self, name: &str) -> Option<Arc<dyn ValueValidator>>
Auto Trait Implementations§
impl Freeze for SchemaRegistry
impl !RefUnwindSafe for SchemaRegistry
impl Send for SchemaRegistry
impl Sync for SchemaRegistry
impl Unpin for SchemaRegistry
impl !UnwindSafe for SchemaRegistry
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)