pub struct Schema { /* private fields */ }azure_policy only.Expand description
A schema represents a type definition that can be used for validation.
Schema is a lightweight wrapper around a [Type] that provides reference counting
for efficient sharing and cloning. It serves as the primary interface for working
with type definitions in the Regorus type system.
§Usage
Schemas are typically created by deserializing from JSON Schema format:
use serde_json::json;
// Create a schema from JSON
let schema_json = json!({
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 }
},
"required": ["name"]
});
let schema: Schema = serde_json::from_value(schema_json).unwrap();§Supported Schema Features
The schema system supports a subset of JSON Schema features needed for Azure Policy and other use cases:
- Basic types:
any,null,boolean,integer,number,string - Complex types:
array,set,object - Value constraints:
enum,const - Composition:
anyOffor union types - String constraints:
minLength,maxLength,pattern - Numeric constraints:
minimum,maximum - Array constraints:
minItems,maxItems - Object features:
properties,required,additionalProperties - Discriminated unions: via
allOfwith conditional schemas
§Thread Safety
While Schema itself is not Send or Sync due to the use of Rc, it can be
safely shared within a single thread and cloned efficiently. For multi-threaded
scenarios, consider wrapping in Arc if needed.
§Examples
§Simple String Schema
let schema = json!({ "type": "string", "minLength": 1 });
let parsed: Schema = serde_json::from_value(schema).unwrap();§Complex Object Schema
let schema = json!({
"type": "object",
"properties": {
"users": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"email": { "type": "string", "pattern": "^[^@]+@[^@]+$" }
},
"required": ["id", "email"]
}
}
}
});
let parsed: Schema = serde_json::from_value(schema).unwrap();§Union Types with anyOf
let schema = json!({
"anyOf": [
{ "type": "string" },
{ "type": "integer", "minimum": 0 }
]
});
let parsed: Schema = serde_json::from_value(schema).unwrap();Implementations§
Source§impl Schema
impl Schema
Sourcepub fn from_serde_json_value(
schema: Value,
) -> Result<Self, Box<dyn Error + Send + Sync>>
pub fn from_serde_json_value( schema: Value, ) -> Result<Self, Box<dyn Error + Send + Sync>>
Parse a JSON Schema document into a Schema instance.
Provides better error messages than serde_json::from_value.
Sourcepub fn from_json_str(s: &str) -> Result<Self, Box<dyn Error + Send + Sync>>
pub fn from_json_str(s: &str) -> Result<Self, Box<dyn Error + Send + Sync>>
Parse a JSON Schema document from a string into a Schema instance.
Provides better error messages than serde_json::from_str.
Sourcepub fn validate(&self, value: &Value) -> Result<(), ValidationError>
pub fn validate(&self, value: &Value) -> Result<(), ValidationError>
Validates a Value against this schema.
Returns Ok(()) if the value conforms to the schema, or a ValidationError
with detailed error information if validation fails.
§Example
use regorus::schema::Schema;
use regorus::Value;
use serde_json::json;
let schema_json = json!({
"type": "string",
"minLength": 1,
"maxLength": 10
});
let schema = Schema::from_serde_json_value(schema_json).unwrap();
let value = Value::from("hello");
assert!(schema.validate(&value).is_ok());Trait Implementations§
Source§impl<'de> Deserialize<'de> for Schema
impl<'de> Deserialize<'de> for Schema
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserializes a JSON Schema into a Schema instance.
This method handles the deserialization of JSON Schema documents into Regorus’ internal type system. It supports two main formats:
- Regular typed schemas - Standard JSON Schema with a
typefield - Union schemas - Schemas using
anyOfto represent union types
§Supported JSON Schema Formats
§Regular Type Schemas
Standard JSON Schema documents with a type field are deserialized directly:
{
"type": "string",
"minLength": 1,
"maxLength": 100
}{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 }
},
"required": ["name"]
}§Union Type Schemas with anyOf
Schemas using anyOf are converted to Type::AnyOf variants:
{
"anyOf": [
{ "type": "string" },
{ "type": "integer", "minimum": 0 },
{ "type": "null" }
]
}§Error Handling
This method will return a deserialization error if:
- The JSON contains unknown/unsupported fields (due to
deny_unknown_fields) - The JSON structure doesn’t match any supported schema format
- Individual type definitions within the schema are invalid
- Required fields are missing (e.g.,
typefield for regular schemas)