python-jsonschema / jsonschema

An implementation of the JSON Schema specification for Python
https://python-jsonschema.readthedocs.io
MIT License
4.62k stars 581 forks source link

Automatically use correct format_checker for versioned Validators #861

Closed TiborVoelcker closed 2 years ago

TiborVoelcker commented 3 years ago

(Not sure if this is already a feature, and I just could not get it to work.)

I want my validator automatically detected using the "$schema" keyword, to leave it customizable. But I would also need the formats to be validated. I know I could use validate(instance, schema, format_checker=draft202012_format_checker). But I would like to just use the corresponding format_checker. Meaning, if my "$schema" keyword leads to using the Draft202012Validator, I would want to automatically use the draft202012_format_checker. If I use Draft7Validator, use the draft7_format_checker, etc.

My proposal would be to accept format_checker=True, to then use the corresponding Format Validator.

Julian commented 3 years ago

Thanks, this is indeed something I was aware of (and support adding) -- though the way I'd want to do so would be more similar to how TYPE_CHECKER is an attribute on validators.

So, in your example, you'd use:

Validator = validator_for(schema)
Validator(schema, format_checker=Validator.FORMAT_CHECKER).validate(instance)

with the addition of the FORMAT_CHECKER attribute.

Certainly willing to see a PR adding it!

TiborVoelcker commented 3 years ago

Alright, I will see what I can do. I am very busy at the moment tho, so I will try to look into it when I have the time. Great package BTW!

Julian commented 3 years ago

Thanks, sounds great, and appreciated!

TiborVoelcker commented 2 years ago

Hi! Sorry for lettings this sit for such a long time. I found some time a while ago, but was completely lost when looking through the code. Now I found time again, and was determined to succeed. I tried to get an overview, but I am still a bit overwhelmed. But if I assessed it correctly, the change needed for this is very minor. So I did make a PR #905.

BTW: I could not figure out how to assign you to the PR, or link this issue with the PR.

Julian commented 2 years ago

Closed in #905.