santhosh-tekuri/jsonschema

Add `Draft` to type `Schema`

Closed this issue · 8 comments

Hi, I would like to retrieve the draft that was used to compile a schema.
Currently, such a property does not exist on the type Schema.

For example: schema.Draft would return an instance of type Draft.

Would it be possible to add this?

@mogottsch at compile time we know which draft is used to compile but it is thrown away after compilation.

this was intentional. Since during validation I want to avoid draft version checks. we can store the draft used in schema as field during compilation.

could you please tell me your use case, why you need schema.Draft

We use this library to validate certain requirements on JSON schemas (repo). One of those requirements is to use a certain draft.

I was expecting to find the draft used for compilation in the schema, but if you think that's out of scope I understand.

got it. I just want to remind that multiple drafts can be used in a schema.

for example a schema with draft2020 can refer to external schema with draft7. So If you want to ensure that single draft is used in schema, you may have to walk Schema recursively. and you have to take care that you do not end up in infinite loop since schema's may have circular references.

Thanks for pointing that out. I didn't know that.

So will you implement the schema.Draft property?
If not feel free to close this issue and thanks for your help!

I did not implement it because I don't need it during validation and also to keep schema compilation fast enough.
I am ok to add if someone finds compelling use case for it.
it should be straight forward to implement. will look into it...

done. check aa1fe28, abecfb7

use v5.2.0

BTW, you can use Schema.Location to avoid infinite recursion. maintain list of schemas visited using Schema.Location, and if schema is already visited then skip it.

That's great. Thanks so much!