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

Closed
xunto opened this Issue Feb 12, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@xunto
Copy link

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


Authentication(password="asdd")

Checked on github master branch.

@samuelcolvin

This comment has been minimized.

Copy link
Owner

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 added the bug label Feb 28, 2018

@samuelcolvin samuelcolvin referenced this issue Mar 25, 2018

Open

Version 1 Release #143

7 of 8 tasks complete

samuelcolvin added a commit that referenced this issue Dec 28, 2018

@samuelcolvin samuelcolvin referenced this issue Dec 28, 2018

Merged

allow validate_always with default=None #344

5 of 5 tasks complete

samuelcolvin added a commit that referenced this issue Dec 29, 2018

allow validate_always with default=None (#344)
* 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