Skip to content

Support YAML 1.1 #574

@chronus7

Description

@chronus7

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 version1. 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.

Footnotes

  1. https://yaml.org/spec/1.1/#id895631, https://yaml.org/spec/1.2.2/#681-yaml-directives

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions