-
Notifications
You must be signed in to change notification settings - Fork 50
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
Unified validation check for dependency fields #1348
Conversation
Why would we only warn instead of error? It seems to me that if previous fields failed validation we should error with the validation error of that field. |
Also I think this PR should go to develop so that we patch this asap. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @dbochkov-flexcompute, I think this will be great to have. A couple minor comments and also there are probably many instances here and there where we can implement it already, especially in Simulation
. For example these lines.
I wonder if, more generally, we can assume that if any values.get(key)
or values[key]
is present in a field validator, that we should probably @check_previous_fields_validation
for that key
.
Another pretty minor comment, perhaps the name of the decorator could be more specific, like
skip_if_validation_failed(fields: List[str])
because check
is maybe a bit ambiguous as to what the decorator is doing. (Just a minor suggestion).
def _validator(cls, val, values): | ||
"""New validator function.""" | ||
for field in required_fields: | ||
if field not in values: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just to confirm, if field
fails validation, it's just not put in values
? (as opposed to put into values with None
?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, just double checked with pydantic documentation https://docs.pydantic.dev/1.10/usage/validators/:
If validation fails on another field (or that field is missing) it will not be included in values, hence if 'password1' in values and ... in this example.
Another idea for the name of this validator, maybe more generally could be skip_if_fields_none(fields: List[str]) |
Just to be clear, the failed field still throws its own error. But I'm ok either way, the difference is very subtle. If we error, then pydantic will collect this additional error and overall we get the following output:
Note that pydantic error list contains two errors ( If we just pop a warning, we get
Note that in this case pydantic error list contains just one error ( This doesn't really make much difference, but I think when we explicitly error a field ("A") that was not validated just because a previous field ("B") failed validation, this might make an impression that something is wrong with setting up field "A", while in reality it could be that just resolving issues with filed "B" is enough. It might be more relevant for GUI were failed fields are marked visually. |
Oh ok that makes sense thanks! |
c0adac9
to
df5a83b
Compare
df5a83b
to
ea2497f
Compare
revised this PR, bigger changes:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me!
thanks @dbochkov-flexcompute |
ea2497f
to
b10bb94
Compare
After playing a little bit with it, managed to implement what we wanted as a convenient decorator. The usage as follows: instead of doing something like
we can just do
this check whether dependency fields have passed validation, and, if not, it skips (not errors) the current validator and prints something like