Skip to content

Commit

Permalink
#6 Fix pattern inside of anyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
horejsek committed Apr 25, 2018
1 parent ffe222b commit 43b4899
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ install:
test:
python3 -m pytest tests

test-lf:
python3 -m pytest --last-fail tests

performance:
python3 performance.py

Expand Down
4 changes: 2 additions & 2 deletions fastjsonschema/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ def generate_max_length(self):

def generate_pattern(self):
with self.l('if isinstance({variable}, str):'):
self._compile_regexps['{}_re'.format(self._variable)] = re.compile(self._definition['pattern'])
with self.l('if not {variable}_re.search({variable}):'):
self._compile_regexps['{}_re'.format(self._definition['pattern'])] = re.compile(self._definition['pattern'])
with self.l('if not globals()["{}_re"].search({variable}):', self._definition['pattern']):
self.l('raise JsonSchemaException("{name} must match pattern {pattern}")')

def generate_format(self):
Expand Down
24 changes: 24 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,27 @@ def test_integration(asserter, value, expected):
]},
],
}, value, expected)


def test_any_of_with_patterns(asserter):
asserter({
'type': 'object',
'properties': {
'hash': {
'anyOf': [
{
'type': 'string',
'pattern': '^AAA'
},
{
'type': 'string',
'pattern': '^BBB'
}
]
}
}
}, {
'hash': 'AAAXXX',
}, {
'hash': 'AAAXXX',
})

0 comments on commit 43b4899

Please sign in to comment.