‘conset’ function is used to constrain items in the set.
Below table summarizes the arguments of conset function.
|
Argument |
Type |
Description |
|
item_type |
Type[T] |
type of the list items |
|
Min_items |
int = None |
minimum number of items in the list |
|
max_items |
int = None |
maximum number of items in the list |
Example
class Employee(BaseModel):
id: int
name: str
age: int
hobbies: conset(str, min_items=2, max_items=4)
conset_demo_1.py
from pydantic import (
BaseModel,
conset,
ValidationError
)
class Employee(BaseModel):
id: int
name: str
age: int
hobbies: conset(str, min_items=2, max_items=4)
try:
emp1 = Employee(id = 1, name = 'Krishna', age = 23, hobbies = {'Cooking'})
except ValidationError as e:
print(e.json())
[
{
"loc": [
"hobbies"
],
"msg": "ensure this value has at least 2 items",
"type": "value_error.set.min_items",
"ctx": {
"limit_value": 2
}
}
]
Previous Next Home
No comments:
Post a Comment