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

Unable to use subschemas for validation (fragment/anchor) #931

Closed
ssbarnea opened this issue Apr 8, 2022 · 13 comments
Closed

Unable to use subschemas for validation (fragment/anchor) #931

ssbarnea opened this issue Apr 8, 2022 · 13 comments

Comments

@ssbarnea
Copy link
Contributor

ssbarnea commented Apr 8, 2022

Most JSON Schema validators allow use of a subschema to validate a file but I was not able to find a way to do that using python-jsonschema.

To example it, a subschema reference is something like http://example.com/app.schema.json#/definitions/foo. That URI tells the loader that the schema to be used is not at root of the JSON object, and instead is as a sublevel.

You can see a live example at https://github.com/SchemaStore/schemastore/blob/master/src/api/json/catalog.json#L222 or https://github.com/ansible/schemas#schema-bundle where you can see two subschema urls.

Keep in mind that that the subschema is not just a sub-json document as it self in can reference definitions from the entire file, so you cannot just handle the subdocument as a full schema.

Is there a way to do this with python-jsonschema?

PS. I know for sure that ajv works fine with subschemas, and also vscode json and yaml extensions. All of these can load and use schemas using the fragment URIs.

@MatthewNielsen27

This comment was marked as off-topic.

@ssbarnea
Copy link
Contributor Author

ssbarnea commented May 13, 2022

@MatthewNielsen27 I still fail to see how can I use an anchored url to validate an instance. Everywhere I see a schema being used, is just a JSON data, without and reference.

@MatthewNielsen27

This comment was marked as off-topic.

@ssbarnea
Copy link
Contributor Author

I managed to unblock myself by writing a script the splits our combined schema in separated files, ones that are compatible with this library. See https://github.com/ansible/schemas/blob/main/src/update-schemas.py#L65-L86

So basically now users have two options: either use the combined schema with anchors or use standalone ones. I was lucky because I used a relatively flat design. All reused definitions are at the root level /definitions.

I still need to add some code that strips definitions that are not needed for current file, but that is just and optimization for reducing the file-size.

@Julian
Copy link
Member

Julian commented May 13, 2022

I still honestly don't really feel I have enough to look into this, and @MatthewNielsen27 I'm not really sure you're talking about the same issue, but it's hard to tell. Whether $id encountered in a $def is supposed to be locatable is a pure spec question (one IIRC the answer to is "yes", so if you observe different behavior and confirm that that's incorrect that's certainly reasonable to file as an issue)

All the rest here is tied up with things not related to the specification or this library, so while I want to help, I can't do that yet until I know what thing is using this library and how -- right now the ansible schemas are not really schemas, they're the VSCode thing. How can I reproduce what you're seeing @ssbarnea, even if the only instructions you have right now involve VSCode or whatever tool is using the files you're talking about?

@ssbarnea
Copy link
Contributor Author

@Julian Hi and thanks for the reply.

All the files from https://github.com/ansible/schemas/tree/main/f are valid JSON Schemas. If you spotted something that makes them not be such, please let me know, I am quite curious. All of them have an $id that points to themselves.

What I was trying to explain that is not working with current validator is the ability to use an anchored URL to validate a document. That is something that works with other validators but it does not with our python library.

To give a specific example, https://github.com/ansible/schemas/blob/main/f/ansible-tasks.json should perform the same validation as https://github.com/ansible/schemas/blob/main/f/ansible.json#/definitions/tasks.

This first one is an URL without an anchor and that works with jsonschema, but the other one does not.

I will try to write a longer article about how to use various tools when developing schemas that should explain it better. I hope the weekend will allow me to do it. I really need to do this because that info is useful for others too. Still, if you want a hands-on experience I would also be happy to show you how I do various stuff on a meet/zoom session, we might both learn new tricks.

@Julian
Copy link
Member

Julian commented May 13, 2022

All the files from https://github.com/ansible/schemas/tree/main/f are valid JSON Schemas. If you spotted something that makes them not be such, please let me know, I am quite curious. All of them have an $id that points to themselves.

Yes, all of these certainly seem valid at a glance -- what I'm saying is what do you mean by:

This first one is an URL without an anchor and that works with jsonschema, but the other one does not.

What code are you running to do this, or what tool are you using to do it?

@MatthewNielsen27

This comment was marked as off-topic.

@Julian

This comment was marked as off-topic.

@MatthewNielsen27

This comment was marked as off-topic.

@Julian

This comment was marked as off-topic.

@MatthewNielsen27

This comment was marked as off-topic.

@Julian Julian added Needs Simplification An issue which is in need of simplifying the example or issue being demonstrated for diagnosis. Waiting for Author A PR waiting for author changes or issue waiting on reporter feedback labels Jun 7, 2022
@Julian
Copy link
Member

Julian commented Jul 26, 2022

Happy to look at this if/when you've got something I can use to reproduce whatever behavior you're referring to!

@Julian Julian closed this as completed Jul 26, 2022
@Julian Julian removed Needs Simplification An issue which is in need of simplifying the example or issue being demonstrated for diagnosis. Waiting for Author A PR waiting for author changes or issue waiting on reporter feedback labels Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants