schema
schema copied to clipboard
Full refactoring of Schema
Hello
I really like the way Schema validates data, but I felt limited by some features, like the pretty limited Hook, JSON schema support, the fact that Schema systematically tries every key of a dict on every schema, and that most work is done at each validation instead of on initialization.
So I eventually rewrote it: https://github.com/aure-olli/schema
I staid as compatible as possible the previous version, and match (almost) all the previous tests, while adding a big bunch of new one.
For the minor incompatibilities:
- I removed the support for https://github.com/keleshev/schema/issues/139: I don't see why it is a problem,
Schema({Optiona('a'): 1, Optiona('a'): 2})can actually be useful, and silently removing one of them is clearly not any better. - The
__repr__ofSchemahas unfortunately more useless stuffs due to the precompilation, but this shouldn't be a problem (just had to update some tests). And the representation ofRegexis less clean. - I changed the callback of
Hook: it now takeskey, value, new, datain order to be able to editnew(useful for https://github.com/keleshev/schema/issues/204 for instance), which is still compatible with the tests and most examples written. - I authorize using
json_schemawithoutschema_id, can be useful for generating a piece of JSON schema or an Open API schema.
For the new features, there are many:
- Everything is as precompiled as possible, and
Dictis much more clever on which schemas to try with which key (depending on the value and the type of the key), as the test shows: https://github.com/aure-olli/schema/blob/35e114ab2be859b9a5de9d812fd8333c29f0cab6/test_schema.py#L1053 - Everything has the same base class:
BaseSchema - As said,
Hookis much more powerful, and can also define acatch(key, error, new, data)function for the case where the value is not matched. Optionalis now aHook(that shows its power), andCleanis similar but discards the key instead of saving it (they both can be used together)Regexcan now have directly a compiled pattern, and has aregex_liboption to use other libraries asregex(https://pypi.org/project/regex/).- And mostly,
json_schemahas totally been rewritten. It is now much more powerful and recursive, tries to do clever merging to compact the schema, can represent regex and comparable, ... and can be specialized for JSON schema or Open API by passingtarget='json_schema'ortarget='openapi'.
I tried to be as compatible as possible with the current version such that it can be merged. I think everybody would be glad to have this updated version. However, being a total refactoring, it clearly requires some discussions.
Thank you.
Thank you for this, it sounds extremely interesting. I would be open to merging some (or all) of the functionality, but it would have to be piecemeal as one big diff is too hard to review :(
Hello. Yeah I agree but this is a refactoring :/ I'll see what I can do about it once I'm satisfied with the results.
I know, it just touches so many things that it's hard to review together :/
Having OpenAPI generation is very interesting, that would avoir having to rewrite the same thing twice.