'How to use validators in Strict Types of Pydantic like StrictStr?

Currently I have schema with field "name". I use constr to specify it

from pydantic import BaseModel, constr

class MySchema(BaseModel):
    name: constr(strict=True, min_length=1, max_length=50)

I want to use pydantic StrictStr type like this:

from pydantic import BaseModel, StrictStr, Field

class MySchema(BaseModel):
    name: StrictStr = Field(min_length=1, max_length=50)

But it raises error:

E   ValueError: On field "name" the following field constraints are set but not enforced: max_length, min_length.
E   For more details see https://pydantic-docs.helpmanual.io/usage/schema/#unenforced-field-constraints

In docs, as i understand, it advices to use use raw attribute name like maxLength isntead of max_length(like exclusiveMaximum for int) but this constraints are not enforced so validations are not applied.

My question is: How I can use StrictStr type for name field and apply native validations like min_length, max_length?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source