Skip to main content

Validate

Trait Validate 

Source
pub trait Validate {
    type Error;

    // Required method
    fn validate(&self) -> Result<(), Self::Error>;
}
Expand description

A type that can validate itself.

Implement this trait to express the validity rules of a type. Use Valid<T> to wrap validated values and carry the proof in the type system.

§Example

use reliakit_validate::{Validate, ValidationError};

struct Score(u8);

impl Validate for Score {
    type Error = ValidationError;

    fn validate(&self) -> Result<(), Self::Error> {
        if self.0 > 100 {
            return Err(ValidationError::new("score must not exceed 100"));
        }
        Ok(())
    }
}

assert!(Score(100).validate().is_ok());
assert!(Score(101).validate().is_err());

Required Associated Types§

Source

type Error

The error type returned when validation fails.

Required Methods§

Source

fn validate(&self) -> Result<(), Self::Error>

Checks whether self satisfies its validity rules.

Returns Ok(()) if valid, or an error describing what failed.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§