Skip to main content

valico/json_dsl/validators/
regex.rs

1use serde_json::Value;
2
3use super::super::errors;
4
5impl super::Validator for fancy_regex::Regex {
6    fn validate(&self, val: &Value, path: &str) -> super::ValidatorResult {
7        let string = strict_process!(val.as_str(), path, "The value must be a string");
8
9        match self.is_match(string) {
10            Ok(true) => Ok(()),
11            Ok(false) => Err(vec![Box::new(errors::WrongValue {
12                path: path.to_string(),
13                detail: Some("Value is not matched by required pattern".to_string()),
14            })]),
15            Err(e) => Err(vec![Box::new(errors::WrongValue {
16                path: path.to_string(),
17                detail: Some(format!("Error evaluating regex '{self}': {e}")),
18            })]),
19        }
20    }
21}