Skip to content
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

Support for other file formats in the CLI #582

Closed
nightwatchcyber opened this issue Jul 11, 2019 · 10 comments
Closed

Support for other file formats in the CLI #582

nightwatchcyber opened this issue Jul 11, 2019 · 10 comments
Labels
Enhancement Some new desired functionality

Comments

@nightwatchcyber
Copy link

Would you consider adding support for validating other file formats (YAML, TOML, INI, etc) via JSON Schema? This would be similar to the functionality provided by this NodeJS project:
https://github.com/json-schema-everywhere/pajv

It is possible to add this functionality via the "anymarkup" module:
https://github.com/bkabrda/anymarkup

I can provide a pull request with these changes

@Julian
Copy link
Member

Julian commented Jul 26, 2019

Hi! Thanks for the idea and offer --

So, I do this personally with remarshal.

E.g. by running jsonschema -i <(yml2json schema.yml) etc

I am... not completely against having that more native (though yeah for me what I do already works quite OK), but I have reservations about "magic". It's easy (as anymarkup says itself) to have ambiguous say INI vs TOML, so it's introducing room for complexity and incorrectness there.

Happy to hear your thoughts on that if you have any.

@Julian Julian added the Enhancement Some new desired functionality label Jul 26, 2019
@thebjorn
Copy link

(not OP) I would be happy to have this for only yaml to start with since it doesn't introduce ambiguity, schemas are much nicer to write in yaml (and our own config files are in yaml and needs validation). Since yaml is a subset of json there shouldn't be much, if any, magic need :-)

remarshal is a great tool, but your example is not quite so convenient on windows (or when both schema and instance are yaml).

@Julian
Copy link
Member

Julian commented Jul 30, 2019

That all makes sense to me! Definitely OK with that much.

@ssbarnea
Copy link
Contributor

@julesjulian I was looking to create a YAML linter that would use data from http://schemastore.org/json/ (which applies to yaml too) but without having native support for yaml I am kinda stuck.

Once this is covered, I can start implementing the tool which should be able to detect schema to use, use internal cache of schemas, lint all files from a repo. If you think that this could be desirable for your tool, I don't mind extending it, otherwise I would make another tool that only calls jsonschema for validating the these files.

@Julian
Copy link
Member

Julian commented Aug 19, 2019

@ssbarnea that's.. interesting, I don't think I'd seen that before.

I think I'm not opposed to adding support for that if it's reasonably small to implement.

Can we tackle that separately though? I.e., one ticket (this one) for just switching to parsing via YAML, and a second one for adding support for schema store to the CLI?

@gsemet
Copy link

gsemet commented Dec 17, 2019

I would be interested in having a transparent yaml loader with $ref resolution. It would require to restrict to the YAML syntax that is 1:1 transposable to json.

aparcar added a commit to aparcar/jsonschema that referenced this issue Oct 24, 2020
"JSON Schema" schemas are not only written in JSON but also in YAML, a
popular example is the Linux Kernel[0]. While it is possible to convert
any input file from YAML to JSON on the fly[1], the automatic resolving
of schema references is not possible.

To allow YAML files for the CLI tool of jsonschema, check the file
suffix and parse the referenced file as YAML if it ends on `yaml` or
`yml`. In case the Python library `pyyaml` is not installed or any other
suffix is found, parsing defaults to JSON as before.

[0]: https://github.com/torvalds/linux/tree/master/Documentation/devicetree/bindings
[1]: python-jsonschema#582 (comment)

Signed-off-by: Paul Spooren <mail@aparcar.org>
aparcar added a commit to aparcar/jsonschema that referenced this issue Oct 24, 2020
"JSON Schema" schemas are not only written in JSON but also in YAML, a
popular example is the Linux Kernel[0]. While it is possible to convert
any input file from YAML to JSON on the fly[1], the automatic resolving
of schema references is not possible.

To allow YAML files for the CLI tool of jsonschema, check the file
suffix and parse the referenced file as YAML if it ends on `yaml` or
`yml`. In case the Python library `pyyaml` is not installed or any other
suffix is found, parsing defaults to JSON as before.

[0]: https://github.com/torvalds/linux/tree/master/Documentation/devicetree/bindings
[1]: python-jsonschema#582 (comment)

Signed-off-by: Paul Spooren <mail@aparcar.org>
aparcar added a commit to aparcar/jsonschema that referenced this issue Oct 24, 2020
"JSON Schema" schemas are not only written in JSON but also in YAML, a
popular example is the Linux Kernel[0]. While it is possible to convert
any input file from YAML to JSON on the fly[1], the automatic resolving
of schema references is not possible.

To allow YAML files for the CLI tool of jsonschema, check the file
suffix and parse the referenced file as YAML if it ends on `yaml` or
`yml`. In case the Python library `pyyaml` is not installed or any other
suffix is found, parsing defaults to JSON as before.

[0]: https://github.com/torvalds/linux/tree/master/Documentation/devicetree/bindings
[1]: python-jsonschema#582 (comment)

Signed-off-by: Paul Spooren <mail@aparcar.org>
aparcar added a commit to aparcar/jsonschema that referenced this issue Oct 24, 2020
"JSON Schema" schemas are not only written in JSON but also in YAML, a
popular example is the Linux Kernel[0]. While it is possible to convert
any input file from YAML to JSON on the fly[1], the automatic resolving
of schema references is not possible.

To allow YAML files for the CLI tool of jsonschema, check the file
suffix and parse the referenced file as YAML if it ends on `yaml` or
`yml`. In case the Python library `pyyaml` is not installed or any other
suffix is found, parsing defaults to JSON as before.

[0]: https://github.com/torvalds/linux/tree/master/Documentation/devicetree/bindings
[1]: python-jsonschema#582 (comment)

Signed-off-by: Paul Spooren <mail@aparcar.org>
aparcar added a commit to aparcar/jsonschema that referenced this issue Oct 24, 2020
"JSON Schema" schemas are not only written in JSON but also in YAML, a
popular example is the Linux Kernel[0]. While it is possible to convert
any input file from YAML to JSON on the fly[1], the automatic resolving
of schema references is not possible.

To allow YAML files for the CLI tool of jsonschema, check the file
suffix and parse the referenced file as YAML if it ends on `yaml` or
`yml`. In case the Python library `pyyaml` is not installed or any other
suffix is found, parsing defaults to JSON as before.

[0]: https://github.com/torvalds/linux/tree/master/Documentation/devicetree/bindings
[1]: python-jsonschema#582 (comment)

Signed-off-by: Paul Spooren <mail@aparcar.org>
aparcar added a commit to aparcar/jsonschema that referenced this issue Oct 24, 2020
"JSON Schema" schemas are not only written in JSON but also in YAML, a
popular example is the Linux Kernel[0]. While it is possible to convert
any input file from YAML to JSON on the fly[1], the automatic resolving
of schema references is not possible.

To allow YAML files for the CLI tool of jsonschema, check the file
suffix and parse the referenced file as YAML if it ends on `yaml` or
`yml`. In case the Python library `pyyaml` is not installed or any other
suffix is found, parsing defaults to JSON as before.

[0]: https://github.com/torvalds/linux/tree/master/Documentation/devicetree/bindings
[1]: python-jsonschema#582 (comment)

Signed-off-by: Paul Spooren <mail@aparcar.org>
@ssbarnea
Copy link
Contributor

I think that for start trying using the pyyaml loader when file extension is either .yml or .yaml should count as easy and safe to implement.

We could even make the dependency on pyyaml soft and enable the feature only when it is installed.

@ioggstream
Copy link

While I'm not sure of the "anymarkup", given that YAML is widely used when developing json-schema and OpenAPI3 files, even a simple YAML loader (eg. yaml.safe_load) would be great.

I think that a conservative solution could even rely on a specify CLI parameter (eg. --yaml) that will do something like

jsonschema/cli.py

class Outputter():
   def from_arguments(...):
      if ... yaml in arguments ... :
            ... set loader ...
      
...
def run(...):

   schema_loader = json.load
   if ... yaml in arguments ... :
       schema_loader = yaml.safe_load

Probably a way to specify the loader in a single place could work too (eg. outputter.schema_loader )

If you are interested, I'm happy to PR :)

@sirosen
Copy link
Member

sirosen commented Feb 21, 2022

Providing this feature is one of the main goals for check-jsonschema.

It provides json and yaml support by default, and if pyjson5 or json5 is installed, it will support JSON5 format files as well. If TOML is really wanted, I'd probably add tomli support following the same path laid out by JSON5. I'm less enthusiastic about INI support (though configparser makes it easy), but could be convinced if users really want it.

YAML for schema files is only supported for local files. Because a RefResolver won't understand YAML, it didn't seem very safe to try to load remote schemas as YAML. I'm happy to revisit or discuss this behavior.

I'd love to hear from any of the users in this thread if check-jsonschema does (or does not!) meet their needs.

I am the author of check-jsonschema and we are currently discussing making it the official CLI for jsonschema. For details, you can follow #910. If check-jsonschema has a behavior which you believe should be changed to handle your use-case, please open an issue!

@sirosen
Copy link
Member

sirosen commented May 24, 2022

We're going to move forward and begin calling check-jsonschema the official CLI. I'm therefore going to close this as a wontfix.

@sirosen sirosen closed this as not planned Won't fix, can't repro, duplicate, stale May 24, 2022
Julian added a commit that referenced this issue Aug 6, 2022
0015d5242 Merge pull request #583 from santhosh-tekuri/defs
e14c68bb4 Replace '$defs' with 'definitions' in drafts 6,7
c4341bd0a Merge pull request #582 from santhosh-tekuri/defs
4ae2c9b93 Replace '$defs' with 'definitions' in draft 6,7

git-subtree-dir: json
git-subtree-split: 0015d5242d146248cb47850a08012b4829462d3b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Some new desired functionality
Projects
None yet
Development

No branches or pull requests

7 participants