rainier-validation 1.0.1

Validation rules, the validator, and form request contracts for Rainier.
Documentation
rainier-validation-1.0.1 has been yanked.

rainier-validation

Validation and request contracts: the [Rule] set, the [Validator] that applies it, and [FormRequest] — the contract a controller action declares instead of checking input by hand.

use rainier_validation::{Rule, Validator};
use serde_json::json;

let validator = Validator::new()
    .rules("email", vec![Rule::Required, Rule::Email])
    .rules("password", vec![Rule::Required, Rule::Min(8.0), Rule::Confirmed]);

let errors = validator
    .validate(&json!({ "email": "ada@example.com", "password": "short" }))
    .unwrap_err();

assert!(errors.first("password").unwrap().contains("at least 8"));

The three states of a field

Absent, null and empty are distinct, and only the presence rules (Required, Present, NotNull) look at the first two. Every other rule skips a field that is not there — so [Rule::Email] means "if supplied it must be an email" and [Rule::Required, Rule::Email] means "must be supplied, and must be an email". See [rule] for the full argument.