mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
Add support for mapping types as custom root (#958)
* Add support for mapping types as custom root * Incorporate feedback * Add changes * Incorporate feedback * Add docs and tests * Fix linting issue * Incorporate more feedback * Add more specific match
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
from typing import List, Dict
|
||||
from pydantic import BaseModel, ValidationError
|
||||
|
||||
class Pets(BaseModel):
|
||||
__root__: List[str]
|
||||
|
||||
print(Pets.parse_obj(['dog', 'cat']))
|
||||
print(Pets.parse_obj({'__root__': ['dog', 'cat']})) # not recommended
|
||||
|
||||
class PetsByName(BaseModel):
|
||||
__root__: Dict[str, str]
|
||||
|
||||
print(PetsByName.parse_obj({'Otis': 'dog', 'Milo': 'cat'}))
|
||||
try:
|
||||
PetsByName.parse_obj({'__root__': {'Otis': 'dog', 'Milo': 'cat'}})
|
||||
except ValidationError as e:
|
||||
print(e)
|
||||
Reference in New Issue
Block a user