Skip to content

Commit

Permalink
Merge pull request #247 from 23andMe/hard-fail-undefined-includes
Browse files Browse the repository at this point in the history
#236 Hard fail on undefined includes
  • Loading branch information
cblakkan authored Apr 24, 2024
2 parents 56eb58b + 4c525eb commit dc81fd6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
14 changes: 11 additions & 3 deletions yamale/schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
from .. import validators as val


class FatalValidationError(Exception):
def __init__(self, error):
super().__init__()
self.error = error


class Schema(object):
"""
Makes a Schema object from a schema dict.
Expand Down Expand Up @@ -45,7 +51,10 @@ def _parse_schema_item(self, path, expression, validators):

def validate(self, data, data_name, strict):
path = DataPath()
errors = self._validate(self._schema, data, path, strict)
try:
errors = self._validate(self._schema, data, path, strict)
except FatalValidationError as e:
errors = [e.error]
return ValidationResult(data_name, self.name, errors)

def _validate_item(self, validator, data, path, strict, key):
Expand Down Expand Up @@ -91,7 +100,6 @@ def _validate(self, validator, data, path, strict):

if isinstance(validator, val.Include):
errors += self._validate_include(validator, data, path, strict)

elif isinstance(validator, (val.Map, val.List)):
errors += self._validate_map_list(validator, data, path, strict)

Expand Down Expand Up @@ -146,7 +154,7 @@ def _validate_map_list(self, validator, data, path, strict):
def _validate_include(self, validator, data, path, strict):
include_schema = self.includes.get(validator.include_name)
if not include_schema:
return [("Include '%s' has not been defined." % validator.include_name)]
raise FatalValidationError("Include '%s' has not been defined." % validator.include_name)
strict = strict if validator.strict is None else validator.strict
return include_schema._validate(include_schema._schema, data, path, strict)

Expand Down
1 change: 1 addition & 0 deletions yamale/tests/fixtures/any_undefined.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
thing: "a string :D"
1 change: 1 addition & 0 deletions yamale/tests/fixtures/any_undefined_schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
thing: any(str(), include('Wtf'))
7 changes: 7 additions & 0 deletions yamale/tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

anys = {"schema": "any.yaml", "bad": "any_bad.yaml", "good": "any_good.yaml"}

any_undefined = {"schema": "any_undefined_schema.yaml", "bad": "any_undefined.yaml"}

list_include = {"schema": "list_include.yaml", "good": "list_include_good.yaml"}

issue_22 = {"schema": "issue_22.yaml", "good": "issue_22_good.yaml"}
Expand Down Expand Up @@ -93,6 +95,7 @@
lists,
maps,
anys,
any_undefined,
list_include,
issue_22,
issue_50,
Expand Down Expand Up @@ -195,6 +198,10 @@ def test_bad_anys():
assert count_exception_lines(anys["schema"], anys["bad"]) == 5


def test_undefined_include():
assert count_exception_lines(any_undefined["schema"], any_undefined["bad"]) == 1


def test_bad_regexes():
assert count_exception_lines(regexes["schema"], regexes["bad"]) == 4

Expand Down

0 comments on commit dc81fd6

Please sign in to comment.