python-jsonschema / python-jsonschema/check-jsonschema

Support YAML 1.1

Open
#574 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
341
Forks
71
Avg merge
15d 20h
Merged PRs (30d)
4

Description

It seems check-jsonschema currently does not support any setting to explicitly parse input as YAML 1.1. I understand that the default is YAML 1.2.x, but YAML also supports directives to specifically define the file's YAML version[^0]. This directive seems not to be respected by check-jsonschema.

I looked a bit into it: ruamel.yaml generally supports this directive and parses the file accordingly. But this highly depends on the options with which it is invoked. If I got it right, check-jsonschema invokes ruamel.yaml.YAML only with typ="safe" and pure=False or pure=True. pure=True would actually be helpful, as can be seen in the table below, but it is not necessarily used, as it is only selected as a fallback. For it to be applied, parsers.yaml.impl2loader.load requires the first implementation (pure=False) to throw an exception when loading the file, but a file might still be valid YAML 1.2 even though YAML 1.1 was targeted. In that case, the returned data will correspond to YAML 1.2 parsing rules which will not match the users expectations.

As a concrete example, consider the following:

  • A simple schema, expecting a single boolean value:

    {"type": "boolean"}
    
  • A YAML file without %YAML directive intended to be interpreted as YAML 1.1, unmarked.yaml:

    on
    
  • A YAML file with %YAML 1.1 directive intended to be interpreted as YAML 1.1, marked.yaml:

    %YAML 1.1
    ---
    on
    

I tested these files with the various options, that ruamel.yaml allows to be passed. The results can be seen in the following table, where each column denotes the typ and pure parameters applied. invalid means, that the value has been parsed as a string and as such does not adhere to the schema. valid means, that the value has (correctly) been parsed as boolean and thus matches the schema.

file rt, False rt, True safe, False safe, True unsafe, False unsafe, True base, False base, True
unmarked.yaml invalid invalid invalid invalid invalid invalid invalid invalid
marked.yaml valid valid invalid valid invalid valid invalid valid

I am not confident enough to say which of these options is the best to use (or why the current ones are selected), but considering the involved library actually supports this use case, I would be grateful, if it could also be supported by check-jsonschema.

[^0]: https://yaml.org/spec/1.1/#id895631, https://yaml.org/spec/1.2.2/#681-yaml-directives

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at parsers.yaml.impl2loader.load and inspect how it invokes ruamel.yaml.YAML with typ="safe" and the pure fallback. Use the marked.yaml and unmarked.yaml examples from the issue to verify behavior; done means a YAML 1.1 directive is respected while ordinary YAML 1.2 parsing remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.