Skip to content

Commit

Permalink
Fixed pattern regexps with a space
Browse files Browse the repository at this point in the history
  • Loading branch information
horejsek committed Apr 15, 2019
1 parent 0f7bd0e commit b335a40
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.txt
@@ -1,3 +1,8 @@
=== 2.10 (2019-04-15)

* Fix pattern regexps with a space.


=== 2.9 (2019-03-04)

* Use of urllib instead of requests for smaller memory usage.
Expand Down
4 changes: 2 additions & 2 deletions fastjsonschema/draft04.py
Expand Up @@ -225,7 +225,7 @@ def generate_pattern(self):
safe_pattern = pattern.replace('"', '\\"')
end_of_string_fixed_pattern = DOLLAR_FINDER.sub(r'\\Z', pattern)
self._compile_regexps[pattern] = re.compile(end_of_string_fixed_pattern)
with self.l('if not REGEX_PATTERNS["{}"].search({variable}):', safe_pattern):
with self.l('if not REGEX_PATTERNS[{}].search({variable}):', repr(pattern)):
self.l('raise JsonSchemaException("{name} must match pattern {}")', safe_pattern)

def generate_format(self):
Expand Down Expand Up @@ -460,7 +460,7 @@ def generate_pattern_properties(self):
with self.l('if {variable}_is_dict:'):
self.create_variable_keys()
for pattern, definition in self._definition['patternProperties'].items():
self._compile_regexps['{}'.format(pattern)] = re.compile(pattern)
self._compile_regexps[pattern] = re.compile(pattern)
with self.l('for {variable}_key, {variable}_val in {variable}.items():'):
for pattern, definition in self._definition['patternProperties'].items():
with self.l('if REGEX_PATTERNS["{}"].search({variable}_key):', pattern):
Expand Down
2 changes: 1 addition & 1 deletion fastjsonschema/version.py
@@ -1 +1 @@
VERSION = '2.9'
VERSION = '2.10'
3 changes: 3 additions & 0 deletions tests/test_security.py
Expand Up @@ -35,6 +35,9 @@ def test_not_generate_code_from_definition(schema):
({'properties': {
'validate(10)': {'type': 'string'},
}}, {'validate(10)': '10'}),
({'patternProperties': {
'validate(10)': {'type': 'string'},
}}, {'validate(10)': '10'}),
])
def test_generate_code_with_proper_variable_names(asserter, schema, value):
asserter({
Expand Down
12 changes: 12 additions & 0 deletions tests/test_string.py
Expand Up @@ -62,6 +62,18 @@ def test_pattern(asserter, value, expected):
'pattern': '^[ab]*[^ab]+(c{2}|d)$',
}, value, expected)


@pytest.mark.parametrize('pattern', [
' ',
'\\x20',
])
def test_pattern_with_space(asserter, pattern):
asserter({
'type': 'string',
'pattern': pattern,
}, ' ', ' ')


exc = JsonSchemaException('data must be a valid regex')
@pytest.mark.parametrize('value, expected', [
('[a-z]', '[a-z]'),
Expand Down

0 comments on commit b335a40

Please sign in to comment.