Expand description
Validation attributes for facet.
This crate provides validation attributes that can be used with the #[facet(...)] syntax.
Validators are run during deserialization, providing errors with spans that point to the
problematic JSON location.
§Example
ⓘ
use facet::Facet;
#[derive(Facet)]
pub struct Product {
#[facet(validate::min_length = 1, validate::max_length = 100)]
pub title: String,
#[facet(validate::min = 0)]
pub price: i64,
#[facet(validate::email)]
pub contact_email: String,
#[facet(validate::custom = validate_currency)]
pub currency: String,
}
fn validate_currency(s: &str) -> Result<(), String> {
match s {
"USD" | "EUR" | "GBP" => Ok(()),
_ => Err(format!("invalid currency code: {}", s)),
}
}§Built-in Validators
| Validator | Syntax | Applies To |
|---|---|---|
min | validate::min = 0 | numbers |
max | validate::max = 100 | numbers |
min_length | validate::min_length = 1 | String, Vec, slices |
max_length | validate::max_length = 100 | String, Vec, slices |
email | validate::email | String |
url | validate::url | String |
regex | validate::regex = r"..." | String |
contains | validate::contains = "foo" | String |
custom | validate::custom = fn_name | any |
Enums§
- Attr
- Validation attributes for facet fields.
Functions§
- is_
valid_ email - Validates that a string is a valid email address.
- is_
valid_ url - Validates that a string is a valid URL.
- matches_
pattern - Validates that a string matches a regex pattern.
Type Aliases§
- Validator
Fn - A function that validates a field value. Takes a type-erased pointer and returns
Ok(())if valid, orErr(message)with a description of the validation failure.