Skip to content

Referencing a YAML schema from a YAML schema fails #80

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

Closed
JeanFred opened this issue Apr 20, 2022 · 2 comments
Closed

Referencing a YAML schema from a YAML schema fails #80

JeanFred opened this issue Apr 20, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@JeanFred
Copy link

Hey there,

I have been trying to write a YAML schema referencing another YAML schema [1].

This results in:

Failure resolving $ref within schema

RefResolutionError: Expecting value: line 1 column 1 (char 0)
  in "/usr/local/lib/python3.8/dist-packages/check_jsonschema/checker.py", line 73
  >>> errors = self._build_error_map()

  caused by

  JSONDecodeError: Expecting value: line 1 column 1 (char 0)
    in "/usr/local/lib/python3.8/dist-packages/jsonschema/validators.py", line 816
    >>> document = self.resolve_remote(url)

    caused by

    StopIteration: 0
      in "/usr/lib/python3.8/json/decoder.py", line 353
      >>> obj, end = self.scan_once(s, idx)

      caused by

      KeyError: 'file:///path/to/B.schema.yml'
        in "/usr/local/lib/python3.8/dist-packages/jsonschema/validators.py", line 813
        >>> document = self.store[url]

It does make sense, as resolving the reference schema is handled by jsonschema, which has no support for YAML − took me a while to get there though :)
Hence I’m not sure whether much can be done in check-jsonschema (perhaps some documentation clarification and/or error message hint)

[1] Something along the lines of the following − exact syntax might be still be off as I’m still figuring things out ^_^

$schema: "https://json-schema.org/draft/2020-12/schema"
description: A schema
type: object

$ref: "B.schema.yml#properties"

required:
  - foo
$schema: "https://json-schema.org/draft/2020-12/schema"
description: B
type: object
properties:
  foo:
    type: string
  bar:
    type: integer
@sirosen
Copy link
Member

sirosen commented Apr 20, 2022

It does make sense, as resolving the reference schema is handled by jsonschema, which has no support for YAML − took me a while to get there though :)

Yep, you've understood what's happening.

This suggests to me that it might be in order for check-jsonschema to spit a warning out when loading a schema defined in YAML. Not everyone would be able to figure it out on their own.

I'll think about the appropriate behavior here more.

It is possible to customize how jsonschema loads schema references (see RefResolver.resolve_remote), but I'd rather not meddle with that stuff if possible.
I'm not hostile towards the use of YAML for schema definitions, but I don't think it's a great idea because you'll run into issues like this. After all, the spec is "JSON Schema", not YAML Schema. 😉
That said, I'll also think about whether or not support for local YAML schema references makes sense.

In the meantime, if you want multiple schemas as YAML with references, the best I can recommend is using a tool like remarshall to setup a worfklow that handles this.

Imagine

for x in schemas/*.yml; do yaml2json "$x" > "$x.json"; done

and write your schema to reference B.yml.json.


I'm leaving this open until at least I have a warning or other behavior to help people out in place.

@JeanFred
Copy link
Author

Oooh, just noticed in the 0.24.0 release notes:

$ref usage may now refer to YAML, TOML, or JSON5 files, or any other non-JSON format supported by check-jsonschema. The file type is inferred only from the file extension in these cases and defaults to JSON if there is no recognizable extension.

See also #289 & #295

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants