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
Return multiple validation errors at once #16
Comments
That would be a nice level of introspection, but it would come at a pretty decent performance cost. We'd need to validate input before passing the values in for initialization. Part of typical's speed on coercion is that it coerces the value on |
In light of your response at #15 (comment), I'm not sure this issue is still relevant, since one could do validation in a preprocessing step and gather the errors (I assume that whatever library which handles JSON schema could spit out multiple errors, but not sure). Perhaps the |
Just thought about it a bit more: using constrained types means that typical also performs validation, while coercing, right? But I think you could make a constrained type that is not representable in a JSON schema, and therefore typical would raise validation errors when coercing despite the data passing JSON schema validation. Therefore, I still think it would be useful to be able to gather multiple validation errors from typical itself (somehow). If you think it's in scope to implement this, then maybe it could be a (non-default) configuration flag where you know that enabling it will make typical perform worse? |
You read my mind. It's a bit of expansion in scope for the constraints validator, but work is ongoing in the |
@syastrov - I haven't adding error-chaining, but I've made the validation error quite a bit more informative: >>> @typic.klass(strict=True)
... class N:
... a: str
... b: int
... c: int
...
>>> N.validate(dict(a="a", b="a", c="b"))
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Users/god/PycharmProjects/typic/typic/serde/des.py", line 440, in des
return __d(__v(val))
File "/Users/god/PycharmProjects/typic/typic/constraints/common.py", line 100, in validate
valid, value = self.validator(value)
File "<typical generated validator_1f2e84ae80ec8e3f-4>", line 9, in validator_1f2e84ae80ec8e3f
valid, val = validator_1f2e84ae80ec8e3f_item_validator(val, addtl)
File "<typical generated validator_1f2e84ae80ec8e3f_item_validator-4>", line 8, in validator_1f2e84ae80ec8e3f_item_validator
rety = validator_1f2e84ae80ec8e3f_item_validator_items_validators[x](y, field='.'.join((valtname, x)))
File "/Users/god/PycharmProjects/typic/typic/constraints/common.py", line 105, in validate
) from None
typic.constraints.error.ConstraintValueError: N.b: value <'a'> fails constraints: (type=int, nullable=False, coerce=False) |
@seandstewart Thanks for the follow up. For me, not supporting this is maybe the only technical reason why I wouldn't switch to typical (from pydantic; there are other non-technical reasons, like that typical is not that widely used yet afaik). So I may send a PR your way if I get the chance. |
Thanks for the honesty there. Error-chaining should be possible. I'd put it at a medium-heavy lift, as the current system is really designed to short-circuit ASAP with an informative error. It's not impossible to add a branch for this, but definitely a day or two's worth of work, which is why I'm scheduling it for 2.1. |
Not sure if it's possible, but it would be useful if you could get a list of e.g.
ValueError
s, rather than just the first one that failed when there are multiple issues.It would make
typical
much more useful for validation purposes.Perhaps it could be configured somehow which behavior you want, since there could be a performance hit, I suppose.
Example:
The text was updated successfully, but these errors were encountered: