Skip to content

Commit

Permalink
Merge pull request #86 from cogwirrel/unstable
Browse files Browse the repository at this point in the history
Fix bug where regexes are matched as strings when 'required'
  • Loading branch information
Grokzen committed Jan 6, 2017
2 parents 2c8e3c0 + 068183d commit a4924b0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/authors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Code
- Markbaas (https://github.com/markbaas)
- Gonditeniz (https://github.com/gonditeniz)
- Comagnaw (https://github.com/comagnaw)

- Cogwirrel (https://github.com/cogwirrel)


Testing
Expand Down
1 change: 1 addition & 0 deletions docs/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ New keywords:

Bug fixes:

- Fixed a bug where regexes marked as 'required' in a map were matched as strings, rather than regexes.
- Fixed a bug where the type validation did not work when schema specefied a sequence of map objects. It now outputs a proper `...is not a dict...` error instead.
- Fixed a bug in *unique* validation when a key that it tried to lookup in the data would not exists.
Now it just ignores that it did not find any value becuase a missing value do not impact validation.
Expand Down
17 changes: 16 additions & 1 deletion pykwalify/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,23 @@ def _validate_mapping(self, value, rule, path, done=None):
)

for k, rr in m.items():
# Find out if this is a regex rule
is_regex_rule = False
required_regex = ""
for regex_rule in rule.regex_mappings:
if k == "regex;({})".format(regex_rule.map_regex_rule) or k == "re;({})".format(regex_rule.map_regex_rule):
is_regex_rule = True
required_regex = regex_rule.map_regex_rule

# Check for the presense of the required key
is_present = False
if not is_regex_rule:
is_present = k in value
else:
is_present = any([re.search(required_regex, v) for v in value])

# Specifying =: as key is considered the "default" if no other keys match
if rr.required and k not in value and k != "=":
if rr.required and not is_present and k != "=":
self.errors.append(SchemaError.SchemaErrorEntry(
msg=u"Cannot find required key '{key}'. Path: '{path}'",
path=path,
Expand Down
17 changes: 16 additions & 1 deletion tests/files/fail/test_mapping.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,19 @@ errors:
# :pattern_unmatch : 2:1:[/email] 'foo(at)mail.com': not matched to pattern /@/.
# :type_unmatch : 3:1:[/age] 'twenty': not a integer.
# :enum_notexist : 4:1:[/blood] 'ab': invalid blood value.
# :type_unmatch : 5:1:[/birth] 'Jul 01, 1985': not a date.
# :type_unmatch : 5:1:[/birth] 'Jul 01, 1985': not a date.
---
name: fail-mapping-9
desc: Test that regexes can be 'required'
data:
hello: Hi
person: Fred
schema:
type: map
mapping:
regex;(person[1-9]):
required: True
errors:
- "Cannot find required key 'regex;(person[1-9])'. Path: ''"
- "Key 'hello' does not match any regex 'person[1-9]'. Path: ''"
- "Key 'person' does not match any regex 'person[1-9]'. Path: ''"
11 changes: 11 additions & 0 deletions tests/files/success/test_mapping.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,14 @@ schema:
sequence:
- type: map
allowempty: True
---
name: mapping18
desc: Test that regexes can be 'required'
data:
person1: Jack
person2: Fred
schema:
type: map
mapping:
regex;(person[1-9]):
required: True

0 comments on commit a4924b0

Please sign in to comment.