pub struct ValidatorBuilder<'a, S: ResourceDef> { /* private fields */ }validation only.Expand description
Builds a DocumentValidator for one resource.
Created by DocumentValidator::builder.
Implementations§
Source§impl<'a, S: ResourceDef> ValidatorBuilder<'a, S>
impl<'a, S: ResourceDef> ValidatorBuilder<'a, S>
Sourcepub fn attributes(self, schema: Value) -> Self
pub fn attributes(self, schema: Value) -> Self
The JSON Schema an attributes object must satisfy.
Anything JSON Schema 2020-12 accepts — usually an object with
properties and required.
Sourcepub fn hide(self, names: impl IntoIterator<Item = impl Into<String>>) -> Self
pub fn hide(self, names: impl IntoIterator<Item = impl Into<String>>) -> Self
Drop these properties from the attribute schema.
For fields your storage has but the wire format does not: the primary key (JSON:API carries it in the envelope) and any foreign key behind a relationship (carried as linkage).
Sourcepub fn annotate_formats(self) -> Self
pub fn annotate_formats(self) -> Self
Stop asserting format, leaving it an annotation.
JSON Schema 2020-12 makes format an annotation by default, and
ash-jsonschema follows that. This crate turns assertion on, because
a resource declaring format: "email" in a request schema means it
wants the check — an unasserted format is a validation rule that
silently does nothing.
Turn it off when a schema is shared with a consumer that only annotates, so both ends agree on what is accepted.
Asserted: date, date-time, time, duration, email,
idn-email, hostname, ipv4, ipv6, uuid, json-pointer and
relative-json-pointer. Any other name stays an annotation unless you
give it a check with format.
Sourcepub fn format<F>(self, name: impl Into<String>, check: F) -> Self
pub fn format<F>(self, name: impl Into<String>, check: F) -> Self
Assert one format with a check of your own.
For a format the specification does not define — an internal id shape, a tenant-scoped key — or to replace a built-in check with a stricter one:
DocumentValidator::builder(&schema)
.attributes(attributes)
.format("employee-id", |value| {
value.strip_prefix("E-").is_some_and(|n| n.len() == 6)
})
.build()?The schema stays ordinary JSON Schema: another validator annotates
"format": "employee-id" where this one asserts it. The check only
ever sees strings, since format says nothing about other types.
Sourcepub fn build(self) -> Result<DocumentValidator, BuildError>
pub fn build(self) -> Result<DocumentValidator, BuildError>
Compile the validator.
Do this once at startup: a schema that will not compile should stop the service rather than surface as a 500 on the first request.