Skip to main content

validate_against_schema

Function validate_against_schema 

Source
pub fn validate_against_schema(value: &Value, schema: &Value) -> Result<()>
Available on crate feature validate-schema only.
Expand description

Validate value against the JSON Schema 2020-12 document schema. Both inputs are Value trees — the schema is usually loaded from a YAML / JSON file via crate::from_str, or built programmatically.

Multiple violations are aggregated into a single error message, each line carrying the JSON-pointer path of the offending instance. The path syntax follows RFC 6901: / for the root, /port for a top-level field, /items/0/name for nested data. A schema-side build failure (malformed schema document) is reported separately so callers can distinguish “your schema is broken” from “your data is broken”.

§Errors

  • The schema cannot be compiled (invalid JSON Schema shape).
  • The instance violates one or more constraints declared in the schema.
  • Internal JSON serialization fails for either input (vanishingly unlikely — would indicate a noyalib serializer bug).

§Examples

use noyalib::{from_str, validate_against_schema, Value};

let schema: Value = from_str(
    "type: object\nrequired: [port]\nproperties:\n  port:\n    type: integer\n",
).unwrap();
let v: Value = from_str("port: 8080\n").unwrap();
validate_against_schema(&v, &schema).unwrap();