| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use crate::{ |
| 4 | compiler, |
| 5 | error::ValidationError, |
| 6 | keywords::CompilationResult, |
| 7 | options::PatternEngineOptions, |
| 8 | paths::{LazyLocation, Location}, |
| 9 | regex::{RegexEngine, RegexError}, |
| 10 | types::JsonType, |
| 11 | validator::{Validate, ValidationContext}, |
| 12 | }; |
| 13 | use serde_json::{Map, Value}; |
| 14 | |
| 15 | pub(crate) struct PatternValidator<R> { |
| 16 | regex: Arc<R>, |
| 17 | location: Location, |
| 18 | } |
| 19 | |
| 20 | impl<R: RegexEngine> Validate for PatternValidator<R> { |
| 21 | fn validate<'i>( |
| 22 | &self, |
| 23 | instance: &'i Value, |
| 24 | location: &LazyLocation, |
| 25 | _ctx: &mut ValidationContext, |
| 26 | ) -> Result<(), ValidationError<'i>> { |
| 27 | if let Value::String(item) = instance { |
| 28 | match self.regex.is_match(item) { |
| 29 | Ok(is_match) => { |
| 30 | if !is_match { |
| 31 | return Err(ValidationError::pattern( |
| 32 | self.location.clone(), |
| 33 | location.into(), |
| 34 | instance, |
| 35 | self.regex.pattern().to_string(), |
| 36 | )); |
| 37 | } |
| 38 | } |
| 39 | Err(e) => { |
| 40 | return Err(ValidationError::backtrack_limit( |
| 41 | self.location.clone(), |
| 42 | location.into(), |
| 43 | instance, |
| 44 | e.into_backtrack_error() |
| 45 | .expect("Can only fail with the fancy-regex crate"), |
| 46 | )); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | Ok(()) |
| 51 | } |
| 52 | |
| 53 | fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool { |
| 54 | if let Value::String(item) = instance { |
| 55 | return self.regex.is_match(item).unwrap_or(false); |
| 56 | } |
| 57 | true |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | #[inline] |
| 62 | pub(crate) fn compile<'a>( |
| 63 | ctx: &compiler::Context, |
| 64 | _: &'a Map<String, Value>, |
| 65 | schema: &'a Value, |
| 66 | ) -> Option<CompilationResult<'a>> { |
| 67 | match schema { |
| 68 | Value::String(item) => match ctx.config().pattern_options() { |
| 69 | PatternEngineOptions::FancyRegex { .. } => { |
| 70 | let Ok(regex) = ctx.get_or_compile_regex(item) else { |
| 71 | return Some(Err(invalid_regex(ctx, schema))); |
| 72 | }; |
| 73 | Some(Ok(Box::new(PatternValidator { |
| 74 | regex, |
| 75 | location: ctx.location().join("pattern"), |
| 76 | }))) |
| 77 | } |
| 78 | PatternEngineOptions::Regex { .. } => { |
| 79 | let Ok(regex) = ctx.get_or_compile_standard_regex(item) else { |
| 80 | return Some(Err(invalid_regex(ctx, schema))); |
| 81 | }; |
| 82 | Some(Ok(Box::new(PatternValidator { |
| 83 | regex, |
| 84 | location: ctx.location().join("pattern"), |
| 85 | }))) |
| 86 | } |
| 87 | }, |
| 88 | _ => Some(Err(ValidationError::single_type_error( |
| 89 | Location::new(), |
| 90 | ctx.location().clone(), |
| 91 | schema, |
| 92 | JsonType::String, |
| 93 | ))), |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | fn invalid_regex<'a>(ctx: &compiler::Context, schema: &'a Value) -> ValidationError<'a> { |
| 98 | ValidationError::format(Location::new(), ctx.location().clone(), schema, "regex") |
| 99 | } |
| 100 | |
| 101 | #[cfg(test)] |
| 102 | mod tests { |
| 103 | use crate::{tests_util, PatternOptions}; |
| 104 | use serde_json::json; |
| 105 | use test_case::test_case; |
| 106 | |
| 107 | #[test_case("^(?!eo:)", "eo:bands", false)] |
| 108 | #[test_case("^(?!eo:)", "proj:epsg", true)] |
| 109 | fn negative_lookbehind_match(pattern: &str, text: &str, is_matching: bool) { |
| 110 | let text = json!(text); |
| 111 | let schema = json!({"pattern": pattern}); |
| 112 | let validator = crate::validator_for(&schema).unwrap(); |
| 113 | assert_eq!(validator.is_valid(&text), is_matching); |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn location() { |
| 118 | tests_util::assert_schema_location(&json!({"pattern": "^f"}), &json!("b"), "/pattern"); |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | #[ignore = "fancy-regex 0.16 no longer fails for this test case"] |
| 123 | fn test_fancy_regex_backtrack_limit_exceeded() { |
| 124 | let schema = json!({"pattern": "(?i)(a|b|ab)*(?=c)"}); |
| 125 | let validator = crate::options() |
| 126 | .with_pattern_options(PatternOptions::fancy_regex().backtrack_limit(1)) |
| 127 | .build(&schema) |
| 128 | .expect("Schema should be valid"); |
| 129 | |
| 130 | let instance = json!("abababababababababababababababababababababababababababab"); |
| 131 | |
| 132 | let error = validator.validate(&instance).expect_err("Should fail"); |
| 133 | assert_eq!( |
| 134 | error.to_string(), |
| 135 | "Error executing regex: Max limit for backtracking count exceeded" |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn test_regex_engine_validation() { |
| 141 | let schema = json!({"pattern": "^[a-z]+$"}); |
| 142 | let validator = crate::options() |
| 143 | .with_pattern_options(PatternOptions::regex()) |
| 144 | .build(&schema) |
| 145 | .expect("Schema should be valid"); |
| 146 | |
| 147 | let valid = json!("hello"); |
| 148 | assert!(validator.is_valid(&valid)); |
| 149 | let invalid = json!("Hello123"); |
| 150 | assert!(!validator.is_valid(&invalid)); |
| 151 | } |
| 152 | } |