pub struct CompiledSchema { /* private fields */ }Expand description
Compiled JSON Schema for efficient repeated validation
Compiling a schema is relatively expensive, so this type caches the compiled schema for efficient reuse.
§Thread Safety
CompiledSchema uses Arc internally and is safe to share
across threads.
§Examples
ⓘ
use hedl_json::validation::{CompiledSchema, ValidationConfig};
use serde_json::json;
let schema = json!({
"type": "object",
"properties": {
"id": {"type": "integer"}
}
});
let compiled = CompiledSchema::compile(&schema, &ValidationConfig::default())?;
// Validate multiple documents efficiently
let doc1 = json!({"id": 1});
let doc2 = json!({"id": 2});
assert!(compiled.validate(&doc1).is_valid);
assert!(compiled.validate(&doc2).is_valid);Implementations§
Source§impl CompiledSchema
impl CompiledSchema
Sourcepub fn compile(
schema: &JsonValue,
config: &ValidationConfig,
) -> Result<Self, SchemaError>
pub fn compile( schema: &JsonValue, config: &ValidationConfig, ) -> Result<Self, SchemaError>
Compile a JSON Schema for validation
§Arguments
schema- JSON Schema document as aserde_json::Valueconfig- Validation configuration
§Returns
Ok(CompiledSchema)- Successfully compiled schemaErr(SchemaError)- Schema is invalid
§Examples
ⓘ
use hedl_json::validation::{CompiledSchema, ValidationConfig};
use serde_json::json;
let schema = json!({
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string",
"minLength": 1
});
let compiled = CompiledSchema::compile(&schema, &ValidationConfig::default())?;Sourcepub fn validate(&self, instance: &JsonValue) -> ValidationResult
pub fn validate(&self, instance: &JsonValue) -> ValidationResult
Validate a JSON value against the schema
§Arguments
instance- JSON document to validate
§Returns
ValidationResult with validation outcome and any errors
§Examples
ⓘ
use hedl_json::validation::{CompiledSchema, ValidationConfig};
use serde_json::json;
let schema = json!({"type": "integer"});
let compiled = CompiledSchema::compile(&schema, &ValidationConfig::default())?;
let result = compiled.validate(&json!(42));
assert!(result.is_valid);
let result = compiled.validate(&json!("not an integer"));
assert!(!result.is_valid);Trait Implementations§
Source§impl Clone for CompiledSchema
impl Clone for CompiledSchema
Source§fn clone(&self) -> CompiledSchema
fn clone(&self) -> CompiledSchema
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for CompiledSchema
impl !RefUnwindSafe for CompiledSchema
impl Send for CompiledSchema
impl Sync for CompiledSchema
impl Unpin for CompiledSchema
impl !UnwindSafe for CompiledSchema
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more