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

Validator isn't called when Optional is set #132

xunto opened this issue Feb 12, 2018 · 1 comment

Validator isn't called when Optional is set #132

xunto opened this issue Feb 12, 2018 · 1 comment


Copy link

@xunto xunto commented Feb 12, 2018

Hi there. I think there is a bug. What I'm trying to do is to check if atleast one of two properties (login or dn) exists. But I if set them optional, validator is never called.

This example doesn't raise exception.

from typing import Optional

import pydantic
from pydantic import validator

class Authentication(pydantic.BaseModel):
    login: Optional[str]
    dn: Optional[str]

    password: str

    @validator('login', "dn", always=True, pre=True)
    def auth_specified(cls, value, values, **kwargs):
        if not value and not {"login", "dn"}.intersection(values.keys()):
            raise ValueError("No auth specified")
        return value


Checked on github master branch.

Copy link

@samuelcolvin samuelcolvin commented Feb 14, 2018

Thanks for reporting. I think this is the same problem I was trying to fix with #118.

The problem is, it took a big rewrite to fix. I'll have another go and see if I can find a way of fixing this without adversely effecting performance or current functionality.


@samuelcolvin samuelcolvin mentioned this issue Mar 25, 2018
8 tasks
samuelcolvin added a commit that referenced this issue Dec 29, 2018
* allow validate alwasy with None, fix #132

* prevent whole validators being called on subitems

* improve Optional validators edge cases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants