Skip to main content

Validator

Trait Validator 

Source
pub trait Validator {
    // Required methods
    fn meta(&self) -> &Meta;
    fn meta_mut(&mut self) -> &mut Meta;
    fn check(&self, value: &mut Value) -> Result<(), Error>;

    // Provided method
    fn validate(&self, value: &mut Value) -> Result<(), Error> { ... }
}
Expand description

A validator: a rule (check) plus human-facing Meta.

Each validator stores a Meta and returns it from meta. check is the rule; it receives &mut Value (not LocatedValue) so it can coerce in place — e.g. a numeric string into an integer. validate is provided: it runs check, attaches this validator’s Meta to any error (innermost wins), and applies the output conversion in meta().convert on success. Composite validators recurse by calling validate on their children, then attach the child’s Location via Error::under_key/Error::under_index.

Required Methods§

Source

fn meta(&self) -> &Meta

This validator’s human-facing metadata.

Source

fn meta_mut(&mut self) -> &mut Meta

Mutable access to this validator’s metadata (backs the WithMeta builder setters).

Source

fn check(&self, value: &mut Value) -> Result<(), Error>

The validation rule: check (and coerce) value in place.

Provided Methods§

Source

fn validate(&self, value: &mut Value) -> Result<(), Error>

Run check; on error attach this validator’s Meta (innermost wins); on success apply the output conversion in meta().convert, if any.

Trait Implementations§

Source§

impl<V: Validator + 'static> From<V> for Box<dyn Validator>

Source§

fn from(validator: V) -> Self

Converts to this type from the input type.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§