Skip to content

Support multiple documents in yaml files #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/check_jsonschema/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ def _build_result(self) -> CheckResult:
if isinstance(data, ParseError):
result.record_parse_error(path, data)
else:
validator = self.get_validator(path, data)
for err in validator.iter_errors(data):
result.record_validation_error(path, err)
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)
return result

def _run(self) -> None:
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 pathlib
import types
import typing as t

from .parsers import ParseError, ParserSet
Expand Down Expand Up @@ -32,5 +33,7 @@ def iter_files(self) -> t.Iterator[tuple[pathlib.Path, ParseError | t.Any]]:
except ParseError as err:
data = err
else:
if isinstance(data, types.GeneratorType):
data = list(data)
data = self._data_transform(data)
yield (path, data)
1 change: 0 additions & 1 deletion src/check_jsonschema/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def get(
self, path: pathlib.Path, default_filetype: str
) -> t.Callable[[t.BinaryIO], t.Any]:
filetype = path_to_type(path, default_type=default_filetype)

if filetype in self._by_tag:
return self._by_tag[filetype]

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.BinaryIO) -> 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
@@ -1,6 +1,7 @@
from __future__ import annotations

import json
import types
import typing as t

import ruamel.yaml
Expand All @@ -19,7 +20,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