-
-
Notifications
You must be signed in to change notification settings - Fork 75
Open
Labels
Description
It would be great if there was an easy way to register custom types. For example, I find it fairly common that I need to add the ability to allow datetime objects to pass through validation rather than just a string formatted as a date.
While this is clearly achievable if one were clever enough to discern all the details of schema compilation, it would be nice if there was a concise API for this instead. Maybe something like:
validator = compile(schema, types={"datetime": lambda x: isinstance(x, datetime)})
validator(datetime.now())Similarly, but also less importantly, jsonschema also made it possible to extend schemas with custom syntax. One could imagine a magic $apply key in a schema which would call a function referenced by it's import path:
schema = {
"type": "array",
"$apply": "path.to.filter_none"
}
def apply_import_function(key, value, data):
# key: "$apply"
# value: "path.to.filter_none"
# data: the thing being validated
...
validator = compile(schema, handlers={"$apply": apply_import_function})
assert validator([1, 2, None, 3, None]) == [1, 2, 3]