Skip to main content

registry

Macro registry 

Source
macro_rules! registry {
    ($state:ident { $($field:ident : $resource:ty),* $(,)? } hooks: $hooks:ident $(,)?) => { ... };
    ($state:ident { $($field:ident : $resource:ty),* $(,)? }) => { ... };
}
Available on crate features http and validation only.
Expand description

Implement Validators from a list of resources.

The validators have to live somewhere on your state, and looking them up by name is the same match every time:

ⓘ
ash_jsonapi::registry! {
    App { todos: Todo, users: User }
}

todos and users are fields on your state holding a DocumentValidator — or anything that derefs to one, which is usually what you want, since your state has to be Clone and a validator is not. The names they answer to are the resources’ own NAME. Build the fields with the generated Todo::validator() at startup:

ⓘ
let state = App {
    todos: Arc::new(Todo::validator()?),
    users: Arc::new(User::validator()?),
    /* … the rest of your state … */
};

§With hooks

A state that carries hooks as well as validators names the field after the resource list:

ⓘ
ash_jsonapi::registry! {
    App { todos: Todo, users: User }
    hooks: hooks
}

Without this arm the macro writes the whole Validators impl, leaving nowhere to add hooks — so a service wanting both had to hand-write the lookup.

A resource whose bodies you never parse — a read-only one — needs no entry. A service where nothing parses a body needs no registry! at all: Validators::validator defaults to None, so impl Validators for App {} is the whole wiring.

Omitting an entry for a resource you do route CREATE or UPDATE for is a wiring bug, and reported as one — the request fails naming the resource, rather than skipping validation.