Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Skipped validation on an unsupported regular expression in patternProperties #214

Merged
merged 1 commit into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

- Skipped validation on an unsupported regular expression in `patternProperties`. [#213](https://github.com/Stranger6667/jsonschema-rs/issues/213)

## [0.8.2] - 2021-05-03

### Performance
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

- Skipped validation on an unsupported regular expression in `patternProperties`. [#213](https://github.com/Stranger6667/jsonschema-rs/issues/213)

## [0.6.2] - 2021-05-03

## Changed
Expand Down
85 changes: 53 additions & 32 deletions jsonschema/src/keywords/additional_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,40 +976,43 @@ pub(crate) fn compile(
if let Some(patterns) = parent.get("patternProperties") {
if let Value::Object(obj) = patterns {
// Compile all patterns & their validators to avoid doing work in the `patternProperties` validator
let compiled_patterns = compile_patterns(obj, context).ok()?;
match schema {
Value::Bool(true) => None, // "additionalProperties" are "true" by default
Value::Bool(false) => {
if let Some(properties) = properties {
dynamic_map!(
AdditionalPropertiesWithPatternsNotEmptyFalseValidator,
properties,
compiled_patterns,
context
)
} else {
Some(AdditionalPropertiesWithPatternsFalseValidator::compile(
compiled_patterns,
))
if let Ok(compiled_patterns) = compile_patterns(obj, context) {
match schema {
Value::Bool(true) => None, // "additionalProperties" are "true" by default
Value::Bool(false) => {
if let Some(properties) = properties {
dynamic_map!(
AdditionalPropertiesWithPatternsNotEmptyFalseValidator,
properties,
compiled_patterns,
context
)
} else {
Some(AdditionalPropertiesWithPatternsFalseValidator::compile(
compiled_patterns,
))
}
}
}
_ => {
if let Some(properties) = properties {
dynamic_map!(
AdditionalPropertiesWithPatternsNotEmptyValidator,
properties,
schema,
compiled_patterns,
context
)
} else {
Some(AdditionalPropertiesWithPatternsValidator::compile(
schema,
compiled_patterns,
context,
))
_ => {
if let Some(properties) = properties {
dynamic_map!(
AdditionalPropertiesWithPatternsNotEmptyValidator,
properties,
schema,
compiled_patterns,
context
)
} else {
Some(AdditionalPropertiesWithPatternsValidator::compile(
schema,
compiled_patterns,
context,
))
}
}
}
} else {
Some(Err(CompilationError::SchemaError))
}
} else {
Some(Err(CompilationError::SchemaError))
Expand Down Expand Up @@ -1067,7 +1070,7 @@ fn compile_patterns(

#[cfg(test)]
mod tests {
use crate::tests_util;
use crate::{tests_util, JSONSchema};
use serde_json::{json, Value};
use test_case::test_case;

Expand Down Expand Up @@ -1437,4 +1440,22 @@ mod tests {
tests_util::is_not_valid(&schema, instance);
tests_util::expect_errors(&schema, instance, expected)
}

#[test]
fn unsupported_regex_does_not_compile() {
// See GH-213
let schema = json!({
"type": "object",
"properties": {
"eo:cloud_cover": {
"type": "number",
},
},
"patternProperties": {
"^(?!eo:)": {}
},
"additionalProperties": false
});
assert!(JSONSchema::compile(&schema).is_err())
}
}