Expand description
§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.
Re-exports§
pub use form_request::field;pub use form_request::FormRequest;pub use form_request::Validated;pub use rule::Outcome;pub use rule::Rule;pub use validator::RuleSet;pub use validator::ValidationErrors;pub use validator::Validator;
Modules§
- form_
request - Request contracts —
FormRequestand theValidatedextractor. - rule
- Validation rules —
Ruleand the built-in set. - validator
- The
Validatorand itsValidationErrors.
Functions§
- lookup
- Read a dotted path out of a JSON tree, indexing arrays numerically.