Skip to main content

Module validation

Module validation 

Source
Expand description

Request validation for Ferro framework.

Provides Laravel-inspired validation with declarative rules.

§Flash round-trip (Phase 137)

On validation failure a POST handler can redirect back and preserve both errors and old form input in the session flash:

use ferro_rs::validation::{Validator, rules::*};
use ferro_rs::rules;

// POST handler
let data = req.input::<serde_json::Value>().await?;
if let Err(e) = Validator::new(&data)
    .rules("name", rules![required()])
    .validate()
{
    let referer = req.header("Referer");
    return e.with_old_input(&data).redirect_back(referer);
}

// GET handler — repopulate form fields
InputProps {
    default_value: req.old("name"),
    error: req.validation_error("name"),
    ..Default::default()
}

§Example

use ferro_rs::validation::{Validator, rules};

let data = serde_json::json!({
    "email": "user@example.com",
    "password": "secret123",
    "age": 25
});

let validator = Validator::new(&data)
    .rules("email", rules![required, email])
    .rules("password", rules![required, min(8)])
    .rules("age", rules![required, integer, min(18)]);

if let Err(errors) = validator.validate() {
    println!("Validation failed: {:?}", errors);
}

Structs§

Accepted
Field must be accepted (yes, on, 1, true).
Alpha
Field must contain only alphabetic characters.
AlphaDash
Field must contain only alphanumeric characters, dashes, and underscores.
AlphaNum
Field must contain only alphanumeric characters.
Between
Field must be between min and max size.
Confirmed
Field must match another field.
Date
Field must be a valid date.
Different
Field must be different from another field.
Email
Field must be a valid email address.
In
Field must be in a list of values.
IsArray
Field must be an array.
IsBoolean
Field must be a boolean.
IsInteger
Field must be an integer.
IsString
Field must be a string.
Max
Field must have a maximum size/length/value.
Min
Field must have a minimum size/length/value.
NotIn
Field must not be in a list of values.
Nullable
Field is optional - only validate if present.
Numeric
Field must be numeric.
Regex_
Field must match a regex pattern.
Required
Field must be present and not empty.
RequiredIf
Field is required if another field equals a value.
Same
Field must be the same as another field.
Url
Field must be a valid URL.
ValidationError
A collection of validation errors.
Validator
Request validator.

Traits§

Rule
A validation rule that can be applied to a field.
Validatable
Trait for types that can validate themselves using declarative rules.

Functions§

accepted
Creates an accepted validation rule.
alpha
Creates an alpha validation rule.
alpha_dash
Creates an alpha_dash validation rule.
alpha_num
Creates an alpha_num validation rule.
array
Creates an array validation rule.
between
Creates a between validation rule.
boolean
Creates a boolean validation rule.
confirmed
Creates a confirmed validation rule.
date
Creates a date validation rule.
different
Creates a different validation rule.
email
Creates an email validation rule.
in_array
Creates an in_array validation rule.
integer
Creates an integer validation rule.
max
Creates a max validation rule.
min
Creates a min validation rule.
not_in
Creates a not_in validation rule.
nullable
Creates a nullable validation rule.
numeric
Creates a numeric validation rule.
regex
Creates a regex validation rule.
register_validation_translator
Register a translation function for validation messages.
required
Creates a required validation rule.
required_if
Creates a required_if validation rule.
same
Creates a same validation rule.
string
Creates a string validation rule.
url
Creates a URL validation rule.
validate
Convenience function to validate data with rules.

Type Aliases§

TranslatorFn
Callback signature for validation message translation.