Add string schema inputs to validator_for#1074
Add string schema inputs to validator_for#1074bollwyvl wants to merge 2 commits intoStranger6667:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1074 +/- ##
=======================================
Coverage 94.49% 94.49%
=======================================
Files 81 81
Lines 22610 22610
=======================================
Hits 21366 21366
Misses 1244 1244 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Thanks @bollwyvl !
Yes, it would add a few branches, but I think it is fine for convenience.
could you, please, elaborate on this? how such a workflow would look like? |
|
Thanks for the review! Sorry about the commit message fails. Please feel free to rebuild this in whatever ways needed!
From the From the Thinking about it as a method on a validator: from jsonschema_rs import validator_for, ValidationError
validator = validator_for(b`{"type": "number", "format": "integer"}`, as_type=int)
good_instance = validator.loads(b"1")
try:
bad_instance = validator.loads(b"a")
except ValidationError:
...Even if the above was purely "cosmetic," just to inform a PEP-484 type checker, it would work with techniques for getting hints from schema, such as jsonschem-gentypes, which could be checked at test time with e.g. |
|
Thats cool! When you mentioned "parse, not validate" I was thinking about parsing incoming bytes with json schema rules, but it would be a completely different compilation flow where the biggest issue would be random access for applicators ( But purely Python side looks interesting, I'll think about it |
Thanks for
jsonschema-rs!To align with the first chunk of Python Usage, this adds
stras an acceptableschemavalue tovalidator_foras well as the also-usefulvalidator_cls_for.I tried these interactively with the current latest PyPI releases. The
validatefunction does not accept a string, which might be nice for consistency.As for avoiding stdlib
json.parse(and even string decoding) in a few more places: I don't have therustchops to dig much further, but it would be very nice if:bytes, and/or potentiallyreadable file-like objectsparse, notvalidate" incoming JSON str/bytesas_typeto refine theAnyreturn valueThanks again!