Skip to main content

ValidatorBuilder

Struct ValidatorBuilder 

Source
pub struct ValidatorBuilder<'a, S: ResourceDef> { /* private fields */ }
Available on crate feature validation only.
Expand description

Builds a DocumentValidator for one resource.

Created by DocumentValidator::builder.

Implementations§

Source§

impl<'a, S: ResourceDef> ValidatorBuilder<'a, S>

Source

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.

Source

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).

Source

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.

Source

pub fn format<F>(self, name: impl Into<String>, check: F) -> Self
where F: Fn(&str) -> bool + Send + Sync + 'static,

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.

Source

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.

Auto Trait Implementations§

§

impl<'a, S> !RefUnwindSafe for ValidatorBuilder<'a, S>

§

impl<'a, S> !UnwindSafe for ValidatorBuilder<'a, S>

§

impl<'a, S> Freeze for ValidatorBuilder<'a, S>
where &'a S: Freeze,

§

impl<'a, S> Send for ValidatorBuilder<'a, S>
where &'a S: Send,

§

impl<'a, S> Sync for ValidatorBuilder<'a, S>
where &'a S: Sync,

§

impl<'a, S> Unpin for ValidatorBuilder<'a, S>
where &'a S: Unpin,

§

impl<'a, S> UnsafeUnpin for ValidatorBuilder<'a, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more