I think this is more of a discussion rather than a bug.
I've looked in multiple places and did not find an agreed-upon common solution to what seems like quite a trivial problem:
I'd like to extend a built-in format checkers with an additional, application-related, logic. It seems that the only thing I can do at the moment is to register a custom format checking function which overwrites the default checks:
import jsonschema as js
formatter = js.FormatChecker()
@formatter.checks("date-time")
def my_date_time(instance):
# custom logic verifying whether the string field tagged as `format: date-time` is of this format
return True
However this is unwanted as I just want to extend the existing checks with my logic, not to rewrite it fully. I want to e.g. additionally validate that the date-time is not too stale. The only workaround I've come up with is sth like this:
import yaml, json
import jsonschema as js
from datetime import datetime as dt
formatter = js.FormatChecker()
date_time_default_check = formatter.checkers["date-time"][0]
@formatter.checks("date-time")
def my_date_time(instance):
if not date_time_default_check(instance):
return False
now = dt.now()
if (now - dt.fromisoformat(instance)).days > 365:
return False
return True
It seems to me that a really intuitive way to solve such problem should be the abillity to register additional custom functions to existing checks per format.
Meaning the formatter.checks functions should append new functions rather than replace them.
This can be made further optional in order not be backward incompatible.
The overall behaviour would be something close to how pydantic resolves it's validator methods.
Are there any obvious contraindications against such approach?
I think this is more of a discussion rather than a bug.
I've looked in multiple places and did not find an agreed-upon common solution to what seems like quite a trivial problem:
I'd like to extend a built-in format checkers with an additional, application-related, logic. It seems that the only thing I can do at the moment is to register a custom format checking function which overwrites the default checks:
However this is unwanted as I just want to extend the existing checks with my logic, not to rewrite it fully. I want to e.g. additionally validate that the date-time is not too stale. The only workaround I've come up with is sth like this:
It seems to me that a really intuitive way to solve such problem should be the abillity to register additional custom functions to existing checks per format.
Meaning the
formatter.checksfunctions should append new functions rather than replace them.This can be made further optional in order not be backward incompatible.
The overall behaviour would be something close to how pydantic resolves it's validator methods.
Are there any obvious contraindications against such approach?