#[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) -> boolpub 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; underPyo3the generated methods take&Bound<'_, PyAny>and returnPyResult<...>, and need thepyo3feature ofjsonschema; underMagnusthey take&magnus::Valueand returnResult<..., magnus::Error>, and need themagnusfeature)draft = Draft4|Draft6|Draft7|Draft201909|Draft202012(a qualified path likejsonschema::Draft::Draft7is accepted; only the final segment is inspected)base_uri = "json-schema:///root/main.json"resources = { "uri" => { schema = r#"..."# }, "uri2" => { path = "..." } }validate_formats = true|falseignore_unknown_formats = true|false(default:true)formats = { "name" => crate::path::to::format_fn }keywords = { "name" => crate::path::to::keyword_factory_fn }(same factory signature asValidationOptions::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 asValidationOptions::{with_content_media_type,with_content_encoding})email_options = { ... }pattern_options = { ... }methods = { is_valid = <bool>, validate = <bool>, iter_errors = <bool> }(all default totrue; a method set tofalseis 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|regexbacktrack_limit = <usize>(only forfancy_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;