Skip to content

Commit

Permalink
feat: support multi document yaml files
Browse files Browse the repository at this point in the history
integrated from python-jsonschema#223
  • Loading branch information
martinmigasiewicz-tomtom committed May 21, 2024
1 parent cf1014d commit f72210d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/check_jsonschema/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ def _build_result(self) -> CheckResult:
if isinstance(data, ParseError):
result.record_parse_error(path, data)
else:
validator = self.get_validator(path, data)
passing = True
for err in validator.iter_errors(data):
result.record_validation_error(path, err)
passing = False
data_list = data if isinstance(data, list) else [data]
for data in data_list:
validator = self.get_validator(path, data)
for err in validator.iter_errors(data):
result.record_validation_error(path, err)
passing = False
if passing:
result.record_validation_success(path)
return result
Expand Down
3 changes: 3 additions & 0 deletions src/check_jsonschema/instance_loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import io
import types
import typing as t

from check_jsonschema.cli.param_types import CustomLazyFile
Expand Down Expand Up @@ -51,6 +52,8 @@ def iter_files(self) -> t.Iterator[tuple[str, ParseError | t.Any]]:
except ParseError as err:
data = err
else:
if isinstance(data, types.GeneratorType):
data = list(data)
data = self._data_transform(data)
finally:
file.close()
Expand Down
9 changes: 9 additions & 0 deletions src/check_jsonschema/parsers/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ def load(stream: t.IO[bytes]) -> t.Any:
try:
data = impl.load(stream_bytes)
except ruamel.yaml.YAMLError as e:
if isinstance(
e, ruamel.yaml.composer.ComposerError
) and "expected a single document in the stream" in str(e):
try:
data = impl.load_all(stream_bytes)
except ruamel.yaml.YAMLError as e:
lasterr = e
else:
break
lasterr = e
else:
break
Expand Down
5 changes: 4 additions & 1 deletion src/check_jsonschema/schema_loader/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io
import json
import sys
import types
import typing as t

import ruamel.yaml
Expand All @@ -28,7 +29,9 @@ def _run_load_callback(schema_location: str, callback: t.Callable) -> dict:
# only local loads can raise the YAMLError, but catch for both cases for simplicity
except (ValueError, ruamel.yaml.error.YAMLError) as e:
raise SchemaParseError(schema_location) from e
if not isinstance(schema, dict):
if isinstance(schema, types.GeneratorType):
schema = list(schema)
if not isinstance(schema, dict) and not isinstance(schema, list):
raise SchemaParseError(schema_location)
return schema

Expand Down

0 comments on commit f72210d

Please sign in to comment.