Skip to content

Commit f72210d

Browse files
feat: support multi document yaml files
integrated from python-jsonschema#223
1 parent cf1014d commit f72210d

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

src/check_jsonschema/checker.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ def _build_result(self) -> CheckResult:
6868
if isinstance(data, ParseError):
6969
result.record_parse_error(path, data)
7070
else:
71-
validator = self.get_validator(path, data)
7271
passing = True
73-
for err in validator.iter_errors(data):
74-
result.record_validation_error(path, err)
75-
passing = False
72+
data_list = data if isinstance(data, list) else [data]
73+
for data in data_list:
74+
validator = self.get_validator(path, data)
75+
for err in validator.iter_errors(data):
76+
result.record_validation_error(path, err)
77+
passing = False
7678
if passing:
7779
result.record_validation_success(path)
7880
return result

src/check_jsonschema/instance_loader.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import io
4+
import types
45
import typing as t
56

67
from check_jsonschema.cli.param_types import CustomLazyFile
@@ -51,6 +52,8 @@ def iter_files(self) -> t.Iterator[tuple[str, ParseError | t.Any]]:
5152
except ParseError as err:
5253
data = err
5354
else:
55+
if isinstance(data, types.GeneratorType):
56+
data = list(data)
5457
data = self._data_transform(data)
5558
finally:
5659
file.close()

src/check_jsonschema/parsers/yaml.py

+9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ def load(stream: t.IO[bytes]) -> t.Any:
6464
try:
6565
data = impl.load(stream_bytes)
6666
except ruamel.yaml.YAMLError as e:
67+
if isinstance(
68+
e, ruamel.yaml.composer.ComposerError
69+
) and "expected a single document in the stream" in str(e):
70+
try:
71+
data = impl.load_all(stream_bytes)
72+
except ruamel.yaml.YAMLError as e:
73+
lasterr = e
74+
else:
75+
break
6776
lasterr = e
6877
else:
6978
break

src/check_jsonschema/schema_loader/readers.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io
44
import json
55
import sys
6+
import types
67
import typing as t
78

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

0 commit comments

Comments
 (0)