Skip to content

Commit

Permalink
Merge ecbbaa0 into bbef24f
Browse files Browse the repository at this point in the history
  • Loading branch information
comagnaw committed Sep 27, 2015
2 parents bbef24f + ecbbaa0 commit 0d0f195
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/Validation Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ Only applies to map. This enables more finegrained control over how the matching

Currently supported rules is

- `any` `1 to all` number of regex must match.
- `any` One or more of the regex must match.
- `all` All defined regex must match each key.

Example:
Expand Down
13 changes: 7 additions & 6 deletions pykwalify/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def _validate_mapping(self, value, rule, path, errors, done=None):
if any(regex_mappings):
sub_regex_result = []

# Found atleast one that matches a mapping regex
# Found at least one that matches a mapping regex
for mm in regex_mappings:
if mm[1]:
log.debug(u" + Matching regex patter: {}".format(mm[0]))
Expand All @@ -486,16 +486,17 @@ def _validate_mapping(self, value, rule, path, errors, done=None):
sub_regex_result.append(False)

if rule._matching_rule == "any":

if any(sub_regex_result):
log.debug(u" + Matched atleast one regex")
log.debug(u" + Matched at least one regex")
else:
log.debug(u"No regex matched")
errors.append(SchemaError.SchemaErrorEntry(
msg=u"Key '{key}' does not match any regex '{regex}'. Path: '{path}'",
path=path,
value=value.encode('unicode_escape'),
value=value,
key=k,
regex=" ".join([mm[0]._map_regex_rule for mm in regex_mappings])))
regex="' or '".join([mm[0]._map_regex_rule for mm in regex_mappings])))
elif rule._matching_rule == "all":
if all(sub_regex_result):
log.debug(u" + Matched all regex rules")
Expand All @@ -504,9 +505,9 @@ def _validate_mapping(self, value, rule, path, errors, done=None):
errors.append(SchemaError.SchemaErrorEntry(
msg=u"Key '{key}' does not match all regex '{regex}'. Path: '{path}'",
path=path,
value=value.encode('unicode_escape'),
value=value,
key=k,
regex=" ".join([mm[0]._map_regex_rule for mm in regex_mappings])))
regex="' and '".join([mm[0]._map_regex_rule for mm in regex_mappings])))
else:
log.debug(u" + No mapping rule defined")
elif r is None:
Expand Down
2 changes: 1 addition & 1 deletion pykwalify/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def init_matching_rule(self, v, rule, path):
log.debug(u"{} {}".format(v, rule))

# Verify that the provided rule is part of one of the allowed one
allowed = ["any"]
allowed = ["any", "all"]
# ["none", "one", "all"] Is currently awaiting proper implementation
if v not in allowed:
raise RuleError(
Expand Down
11 changes: 11 additions & 0 deletions tests/files/fail/21f.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
data:
foobar1: 1
foobar2: 2
foobar3: 3
schema:
type: map
mapping:
regex;(^foobar[1-2]$):
type: int
errors:
- "Key 'foobar3' does not match any regex '^foobar[1-2]$'. Path: ''"
14 changes: 14 additions & 0 deletions tests/files/fail/22f.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
data:
foobar1: 1
foobar2: 2
bar3: 3
schema:
type: map
matching-rule: 'any'
mapping:
regex;(^foobar):
type: int
regex;([1-2]$):
type: int
errors:
- "Key 'bar3' does not match any regex '^foobar' or '[1-2]$'. Path: ''"
14 changes: 14 additions & 0 deletions tests/files/fail/23f.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
data:
foobar1: 1
foobar2: 2
foobar3: 3
schema:
type: map
matching-rule: 'all'
mapping:
regex;(^foobar.*$):
type: int
regex;(^.*[1-2]$):
type: int
errors:
- "Key 'foobar3' does not match all regex '^.*[1-2]$' and '^foobar.*$'. Path: ''"
6 changes: 6 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,12 @@ def test_core_files(self):
("17f.yaml", SchemaError),
# Test multiple nested sequence values with error in level 2 with 'any' matching rule
("18f.yaml", SchemaError),
# Test keyword regex using default matching-rule 'any'
("21f.yaml", SchemaError),
# Test keyword regex using declared matching-rule 'any'
("22f.yaml", SchemaError),
# Test keyword regex using declared matching-rule 'all'
("23f.yaml", SchemaError),
]

# Add override magic to make it easier to test a specific file
Expand Down
2 changes: 1 addition & 1 deletion tests/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_matching_rule(self):
# Test that exception is raised when a invalid matching rule is used
with pytest.raises(RuleError) as r:
Rule(schema={"type": "map", "matching-rule": "foobar", "mapping": {"regex;.+": {"type": "seq", "sequence": [{"type": "str"}]}}})
assert str(r.value) == "<RuleError: error code 4: Specified rule in key: foobar is not part of allowed rule set : ['any']: Path: '/'>"
assert str(r.value) == "<RuleError: error code 4: Specified rule in key: foobar is not part of allowed rule set : ['any', 'all']: Path: '/'>"
assert r.value.error_key == 'matching_rule.not_allowed'

def test_allow_empty_map(self):
Expand Down

0 comments on commit 0d0f195

Please sign in to comment.