Note
Requires the regex feature.
Validate a string matches a regular expression using the regex crate.
# extern crate fortifier;
# extern crate regex;
#
use std::sync::LazyLock;
use fortifier::Validate;
use regex::Regex;
static COUNTRY_CODE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[A-Z]{2}").expect("valid regex"));
#[derive(Validate)]
struct User {
#[validate(regex = &COUNTRY_CODE_REGEX)]
country_code: String,
}Validate the string matches the specified regular expression.
The regular expression to match against.
The recommended approach for global regular expressions is to use a static LazyLock.
# extern crate fortifier;
# extern crate regex;
#
use std::sync::LazyLock;
use fortifier::Validate;
use regex::Regex;
static COUNTRY_CODE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[A-Z]{2}").expect("valid regex"));
#[derive(Validate)]
struct User {
#[validate(regex(expression = &COUNTRY_CODE_REGEX))]
country_code: String,
}