Can you validate against schema defined in .yaml? #1043
Replies: 1 comment
|
Yes. Load the YAML into Python objects first. For your external For a fixed set of files, you can keep using from pathlib import Path
import yaml
from jsonschema import Draft202012Validator, validate
from referencing import Registry
from referencing.jsonschema import DRAFT202012
schema = yaml.safe_load(Path('schema.yaml').read_text(encoding='utf-8'))
referenced = yaml.safe_load(Path('some_file.yaml').read_text(encoding='utf-8'))
registry = Registry().with_resource(
'some_file.yaml',
DRAFT202012.create_resource(referenced),
)
validate(instance, schema, cls=Draft202012Validator, registry=registry)Here I tested this with jsonschema 4.26.0, referencing 0.37.0, and PyYAML 6.0.3: the external YAML reference resolved, valid data passed, and invalid data produced errors at the nested referenced property and The official referencing guide also shows a retrieval callback for loading YAML references dynamically. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Is there a possibility to use:
Instead of this:
{ "type": "array", "items": { "type": "object", "properties": { "type": { "$ref": "some_file.json#/components/schemas/SomeOtherSchema" }, "ids": { "type": "array", "items": { "type": "string" } } }, "required": [ "type" ] } }How do I proceed with that? I tend to use validate()
All reactions