Pydantic: How to throw an error for an empty input value?

Created on 25 Nov 2019  路  2Comments  路  Source: samuelcolvin/pydantic

Question

Hello! Does any common way to throw an error for an empty input value?

Example:

class Foo(pydantic.BaseModel):
    bar: str

# Wanna throw an error in this case
Foo(bar="")

Thank you for your work.

question

Most helpful comment

You can use constr as a type.

from pydantic import constr, BaseModel


class Model(BaseModel):
    bar: constr(min_length=1)


Model(bar="")

https://pydantic-docs.helpmanual.io/usage/types/#constrained-types

All 2 comments

You can use constr as a type.

from pydantic import constr, BaseModel


class Model(BaseModel):
    bar: constr(min_length=1)


Model(bar="")

https://pydantic-docs.helpmanual.io/usage/types/#constrained-types

or you can use validators

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jasonkuhrt picture jasonkuhrt  路  21Comments

sm-Fifteen picture sm-Fifteen  路  45Comments

dmfigol picture dmfigol  路  38Comments

chopraaa picture chopraaa  路  18Comments

jaheba picture jaheba  路  25Comments