Skip to main content

validator

Attribute Macro validator 

Source
#[validator]
Expand description

Generates a compile-time JSON Schema validator.

Reads and compiles the schema at compile time, generating inherent methods on the annotated struct:

  • pub fn is_valid(instance: &serde_json::Value) -> bool
  • pub fn validate(instance: &Value) -> Result<(), jsonschema::ValidationError<'_>>
  • pub fn iter_errors(instance: &Value) -> jsonschema::ErrorIterator<'_>

The evaluate method is not implemented yet.

Schema errors are emitted as regular Rust compile errors.

§Required attribute

Exactly one schema source must be provided:

  • path = "relative/or/absolute/path/to/schema.json"
  • schema = r#"{"type":"string"}"#

§Optional attributes

  • backend = SerdeJson|Pyo3|Magnus (default: SerdeJson; under Pyo3 the generated methods take &Bound<'_, PyAny> and return PyResult<...>, and need the pyo3 feature of jsonschema; under Magnus they take &magnus::Value and return Result<..., magnus::Error>, and need the magnus feature)
  • draft = Draft4|Draft6|Draft7|Draft201909|Draft202012 (a qualified path like jsonschema::Draft::Draft7 is accepted; only the final segment is inspected)
  • base_uri = "json-schema:///root/main.json"
  • resources = { "uri" => { schema = r#"..."# }, "uri2" => { path = "..." } }
  • validate_formats = true|false
  • ignore_unknown_formats = true|false (default: true)
  • formats = { "name" => crate::path::to::format_fn }
  • keywords = { "name" => crate::path::to::keyword_factory_fn } (same factory signature as ValidationOptions::with_keyword; a failing factory panics at the validator’s first use, and overriding built-in keywords is not supported)
  • content_media_types = { "name" => crate::path::to::check_fn }
  • content_encodings = { "name" => { check = crate::path::to::check_fn, convert = crate::path::to::convert_fn } } (same fn signatures as ValidationOptions::{with_content_media_type,with_content_encoding})
  • email_options = { ... }
  • pattern_options = { ... }
  • methods = { is_valid = <bool>, validate = <bool>, iter_errors = <bool> } (all default to true; a method set to false is not generated, and at least one must stay enabled)

email_options keys:

  • minimum_sub_domains = <usize>
  • no_minimum_sub_domains = <bool>
  • required_tld = <bool>
  • allow_domain_literal = <bool>
  • allow_display_text = <bool>

pattern_options keys:

  • engine = fancy_regex|regex
  • backtrack_limit = <usize> (only for fancy_regex)
  • size_limit = <usize>
  • dfa_size_limit = <usize>

§Example

ⓘ
// Inline schema
#[jsonschema::validator(schema = r#"{"maxLength": 5}"#)]
struct Short;

// Or load it from a file:
// #[jsonschema::validator(path = "schema.json")]

let instance = serde_json::json!("value");
assert!(Short::is_valid(&instance));
Short::validate(&instance)?;

With options:

ⓘ
#[jsonschema::validator(
    path = "schema.json",
    draft = Draft202012,
    validate_formats = true,
    formats = {
        "currency" => crate::formats::is_currency,
    },
    pattern_options = {
        engine = regex,
        size_limit = 1_000_000,
        dfa_size_limit = 2_000_000,
    }
)]
struct Validator;