Skip to content

Latest commit

 

History

History
59 lines (41 loc) · 1.29 KB

File metadata and controls

59 lines (41 loc) · 1.29 KB

Regular Expression

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,
}

Types

String

Validate the string matches the specified regular expression.

Options

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,
}