jellyschema/validator/types/
email.rs

1use lazy_static::lazy_static;
2use regex::Regex;
3use serde_json::Value;
4
5use crate::validator::{scope::ScopedSchema, state::ValidationState, types::validate_as_string_with_regex};
6
7lazy_static! {
8    // ajv v6.7.0 compatible
9    // https://github.com/epoberezkin/ajv/blob/v6.7.0/lib/compile/formats.js
10    static ref EMAIL_REGEX: Regex =
11        Regex::new(r"^(?i)[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$").unwrap();
12}
13
14pub fn validate_as_email(scope: &ScopedSchema, data: &Value) -> ValidationState {
15    validate_as_string_with_regex(scope, data, &EMAIL_REGEX)
16}