Skip to main content

Validate

Trait Validate 

Source
pub trait Validate {
    // Required method
    fn validate(&self, value: &Value) -> Validation;
}
Expand description

A field constraint that knows how to check a candidate value. An embedder implements this on its own constraint type (an enum with a vocabulary variant, a reference variant, whatever it needs); crate::FieldRule::validate dispatches to it generically.

use fig::Value;
use fig_schema::{Term, Validate, Validation, validate_enum};

// The embedder's own constraint type — this crate never sees its shape.
enum Constraint {
    Enum { values: Vec<Term>, closed: bool },
    Ref,
}

impl Validate for Constraint {
    fn validate(&self, value: &Value) -> Validation {
        match self {
            Constraint::Enum { values, closed } => validate_enum(values, *closed, value),
            Constraint::Ref => Validation::Ok, // resolved against the workspace
        }
    }
}

let visibility = Constraint::Enum {
    values: vec![Term::value("public"), Term::value("private")],
    closed: true,
};
assert!(visibility.validate(&Value::Str("public".into())).is_ok());
assert!(visibility.validate(&Value::Str("secret".into())).is_reject());

Required Methods§

Source

fn validate(&self, value: &Value) -> Validation

Check value against this constraint.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§