Skip to content

Validator isn't called when Optional is set #132

@xunto

Description

@xunto

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions