pub struct Schema { /* private fields */ }Expand description
Schema for validating configuration
Implementations§
Source§impl Schema
impl Schema
Sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Self>
pub fn from_file(path: impl AsRef<Path>) -> Result<Self>
Load a schema from a file (JSON or YAML based on extension)
Sourcepub fn validate(&self, value: &Value) -> Result<()>
pub fn validate(&self, value: &Value) -> Result<()>
Validate a Value against this schema
Returns Ok(()) if valid, or an error with details about the first validation failure.
Sourcepub fn validate_collect(&self, value: &Value) -> Vec<ValidationError>
pub fn validate_collect(&self, value: &Value) -> Vec<ValidationError>
Validate and collect all errors (instead of failing on first)
Sourcepub fn to_markdown(&self) -> String
pub fn to_markdown(&self) -> String
Generate markdown documentation from the schema
Sourcepub fn to_template(&self) -> String
pub fn to_template(&self) -> String
Generate a YAML template from the schema
Creates a configuration template with default values and comments indicating required fields and descriptions.
Sourcepub fn get_default(&self, path: &str) -> Option<Value>
pub fn get_default(&self, path: &str) -> Option<Value>
Get the default value for a config path from the schema
Navigates the schema’s properties structure to find the default
value for the given dot-separated path.
§Example
let schema = Schema::from_yaml(r#"
type: object
properties:
database:
type: object
properties:
port:
type: integer
default: 5432
"#).unwrap();
assert_eq!(schema.get_default("database.port"), Some(holoconf_core::Value::Integer(5432)));
assert_eq!(schema.get_default("missing"), None);Sourcepub fn allows_null(&self, path: &str) -> bool
pub fn allows_null(&self, path: &str) -> bool
Check if null is allowed for a config path in the schema
Returns true if:
- The schema allows
type: "null"ortype: ["string", "null"] - The path doesn’t exist in the schema (permissive by default)
§Example
let schema = Schema::from_yaml(r#"
type: object
properties:
nullable_field:
type: ["string", "null"]
non_nullable:
type: string
"#).unwrap();
assert!(schema.allows_null("nullable_field"));
assert!(!schema.allows_null("non_nullable"));
assert!(schema.allows_null("missing")); // permissive for undefined pathsTrait Implementations§
Auto Trait Implementations§
impl Freeze for Schema
impl !RefUnwindSafe for Schema
impl Send for Schema
impl Sync for Schema
impl Unpin for Schema
impl !UnwindSafe for Schema
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