Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier way to extend validation with native Python types/functions #95

Open
rmorshea opened this issue Mar 28, 2020 · 0 comments
Open

Comments

@rmorshea
Copy link

rmorshea commented Mar 28, 2020

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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants