valico/json_schema/validators/
pattern.rs

1use serde_json::Value;
2
3use super::super::errors;
4use super::super::scope;
5
6#[allow(missing_copy_implementations)]
7pub struct Pattern {
8    pub regex: fancy_regex::Regex,
9}
10
11impl super::Validator for Pattern {
12    fn validate(
13        &self,
14        val: &Value,
15        path: &str,
16        _scope: &scope::Scope,
17        _: &super::ValidationState,
18    ) -> super::ValidationState {
19        let string = nonstrict_process!(val.as_str(), path);
20
21        if self.regex.is_match(string).unwrap_or(false) {
22            super::ValidationState::new()
23        } else {
24            val_error!(errors::Pattern {
25                path: path.to_string()
26            })
27        }
28    }
29}