Skip to main content

Validators

Trait Validators 

Source
pub trait Validators:
    Clone
    + Send
    + Sync
    + 'static {
    // Provided methods
    fn validator(&self, _resource: &str) -> Option<&DocumentValidator> { ... }
    fn hooks(&self) -> Option<&Hooks> { ... }
}
Available on crate features http and validation only.
Expand description

Your application state, as the generated handlers see it.

Only the validators live here, because they are per-resource but built once at startup. The operations are on Create, Read, Update and Delete — one trait each, so a resource implements only what it actually serves.

§A read-only service implements this for free

Validators check request bodies, and GET has none. A service that routes only LIST and READ therefore never reaches a validator, so the method below defaults to None and such a service needs no registry! at all:

ⓘ
// The whole implementation, for a service that accepts no bodies.
impl Validators for App {}

Route an operation that does carry a body — CREATE or UPDATE — without registering its validator, and the request fails as the wiring bug it is, naming the resource. Use registry! to write the lookup rather than spelling it out.

Provided Methods§

Source

fn validator(&self, _resource: &str) -> Option<&DocumentValidator>

The compiled request-body validator for one resource, by name.

None means the resource has no validator registered. For a service that accepts no request bodies that is the correct answer and the default; for one that routes CREATE or UPDATE it is a wiring bug, and reported as one rather than as a 404.

Source

fn hooks(&self) -> Option<&Hooks>

The hooks to run around every operation on this state.

None — the default — skips the mechanism entirely: no event is built and no hook list is walked, so a service that registers no hooks pays nothing for their existence.

This lives on Validators rather than a trait of its own because every state already implements Validators; a second required trait would make every existing service add an empty impl to keep compiling, and a blanket impl would make the override impossible rather than optional.

ⓘ
impl Validators for App {
    fn hooks(&self) -> Option<&Hooks> {
        Some(&self.hooks)
    }
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§