mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
f55515820a
* renaming .json -> .model_dump_json * renaming .dict -> .model_dump * renaming .__fields__ -> .model_fields * renaming .schema -> .model_json_schema * renaming .construct -> .model_construct * renaming .parse_obj -> .model_validate * make linters happy * add changes md-file Co-authored-by: Samuel Colvin <s@muelcolvin.com>
22 lines
498 B
Python
22 lines
498 B
Python
from typing import List, Dict
|
|
from pydantic import BaseModel, ValidationError
|
|
|
|
|
|
class Pets(BaseModel):
|
|
__root__: List[str]
|
|
|
|
|
|
print(Pets.model_validate(['dog', 'cat']))
|
|
print(Pets.model_validate({'__root__': ['dog', 'cat']})) # not recommended
|
|
|
|
|
|
class PetsByName(BaseModel):
|
|
__root__: Dict[str, str]
|
|
|
|
|
|
print(PetsByName.model_validate({'Otis': 'dog', 'Milo': 'cat'}))
|
|
try:
|
|
PetsByName.model_validate({'__root__': {'Otis': 'dog', 'Milo': 'cat'}})
|
|
except ValidationError as e:
|
|
print(e)
|