diff --git a/yamale/schema/schema.py b/yamale/schema/schema.py index 2b8af90..cee1f67 100644 --- a/yamale/schema/schema.py +++ b/yamale/schema/schema.py @@ -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. @@ -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): @@ -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) @@ -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) diff --git a/yamale/tests/fixtures/any_undefined.yaml b/yamale/tests/fixtures/any_undefined.yaml new file mode 100644 index 0000000..a3a3e61 --- /dev/null +++ b/yamale/tests/fixtures/any_undefined.yaml @@ -0,0 +1 @@ +thing: "a string :D" diff --git a/yamale/tests/fixtures/any_undefined_schema.yaml b/yamale/tests/fixtures/any_undefined_schema.yaml new file mode 100644 index 0000000..4407ff7 --- /dev/null +++ b/yamale/tests/fixtures/any_undefined_schema.yaml @@ -0,0 +1 @@ +thing: any(str(), include('Wtf')) diff --git a/yamale/tests/test_functional.py b/yamale/tests/test_functional.py index 97c8fd1..c78c8ad 100644 --- a/yamale/tests/test_functional.py +++ b/yamale/tests/test_functional.py @@ -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"} @@ -93,6 +95,7 @@ lists, maps, anys, + any_undefined, list_include, issue_22, issue_50, @@ -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