You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
multiple checker functions for a given format #1496
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:
importjsonschemaasjsformatter=js.FormatChecker()
@formatter.checks("date-time")defmy_date_time(instance):
# custom logic verifying whether the string field tagged as `format: date-time` is of this formatreturnTrue
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:
importyaml, jsonimportjsonschemaasjsfromdatetimeimportdatetimeasdtformatter=js.FormatChecker()
date_time_default_check=formatter.checkers["date-time"][0]
@formatter.checks("date-time")defmy_date_time(instance):
ifnotdate_time_default_check(instance):
returnFalsenow=dt.now()
if (now-dt.fromisoformat(instance)).days>365:
returnFalsereturnTrue
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?
Activity