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

Post root_validator only called when all field validations succeeded #1049

Closed
PPakalns opened this issue Nov 29, 2019 · 2 comments
Closed

Post root_validator only called when all field validations succeeded #1049

PPakalns opened this issue Nov 29, 2019 · 2 comments
Labels
feature request help wanted Pull Request welcome

Comments

@PPakalns
Copy link

Feature Request

Please complete:

  • OS: Linux, Ubuntu
  • Python version import sys; print(sys.version): 3.7
  • Pydantic version import pydantic; print(pydantic.VERSION): 1.1.1

If field validation fails there often is no need to call root validator as it will fail too because fields are not available.

I suggest that new parameter is added to root_validator decorator that disables call of root_validator if field validation failed.

@root_validator(pre=False, call_on_field_failure=False)

See relevant quote from documentation about current behavior:

Field validation will not occur if pre=True root validators raise an error. As with field validators, "post" (i.e. pre=False) root validators will be called even if field validation fails; the values argument will be a dict containing the values which passed field validation and field defaults where applicable.
From https://pydantic-docs.helpmanual.io/usage/validators/

Thank you all for developing this library!

@samuelcolvin
Copy link
Member

samuelcolvin commented Nov 29, 2019

Shouldn't be too hard to implement, PR welcome.

Would involve a change here:

https://github.com/samuelcolvin/pydantic/blob/151143deb4eaf6a5e2dce4532659e5ee9e2e4713/pydantic/main.py#L855-L860

call_after_failures argument would need to have default True at least until v2.

@aviramha
Copy link
Contributor

Implemented on #1050. I thought about using the Validator class, but that'd require many changes (right now in the flow @samuelcolvin mentioned the validator config are removed and only functions are left).
The alternative was to add a variable to the function itself, which is what I chose to do.
Another approach which we'll require more coding will change model.post_root_validators and the pre one contain the f_cls instead of only functions.
@samuelcolvin What do you think? Should I go for the other approach or this one suffices?

alexdrydew pushed a commit to alexdrydew/pydantic that referenced this issue Dec 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request help wanted Pull Request welcome
Projects
None yet
Development

No branches or pull requests

3 participants