Skip to content

Commit

Permalink
fix: The format validator incorrectly rejecting supported regex pat…
Browse files Browse the repository at this point in the history
…terns

Ref: #230
  • Loading branch information
Stranger6667 committed Jun 17, 2021
1 parent 550ed82 commit e246305
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- The `format` validator incorrectly rejecting supported regex patterns. [#230](https://github.com/Stranger6667/jsonschema-rs/issues/230)

## [0.9.0] - 2021-05-07

### Added
Expand Down
4 changes: 4 additions & 0 deletions bindings/python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- The `format` validator incorrectly rejecting supported regex patterns. [#230](https://github.com/Stranger6667/jsonschema-rs/issues/230)

## [0.9.0] - 2021-05-07

### Added
Expand Down
13 changes: 11 additions & 2 deletions jsonschema/src/keywords/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{
compilation::{context::CompilationContext, JSONSchema},
error::{error, no_error, CompilationError, ErrorIterator, ValidationError},
keywords::CompilationResult,
keywords::{pattern, CompilationResult},
paths::InstancePath,
validator::Validate,
Draft,
Expand Down Expand Up @@ -267,7 +267,7 @@ impl Validate for RegexValidator {
validate!("regex");
fn is_valid(&self, _: &JSONSchema, instance: &Value) -> bool {
if let Value::String(item) = instance {
Regex::new(item).is_ok()
pattern::convert_regex(item).is_ok()
} else {
true
}
Expand Down Expand Up @@ -381,4 +381,13 @@ mod tests {
let compiled = JSONSchema::compile(&schema).unwrap();
assert!(compiled.is_valid(&instance))
}

#[test]
fn ecma_regex() {
// See GH-230
let schema = json!({"format": "regex", "type": "string"});
let instance = json!("^\\cc$");
let compiled = JSONSchema::compile(&schema).unwrap();
assert!(compiled.is_valid(&instance))
}
}
2 changes: 1 addition & 1 deletion jsonschema/src/keywords/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl ToString for PatternValidator {
}

// ECMA 262 has differences
fn convert_regex(pattern: &str) -> Result<fancy_regex::Regex, fancy_regex::Error> {
pub(crate) fn convert_regex(pattern: &str) -> Result<fancy_regex::Regex, fancy_regex::Error> {
// replace control chars
let new_pattern = CONTROL_GROUPS_RE.replace_all(pattern, replace_control_group);
let mut out = String::with_capacity(new_pattern.len());
Expand Down

0 comments on commit e246305

Please sign in to comment.