mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
e4b285a0cd
* support custom root types * update HISTORY.rst for costom root types * fix invalid arguments case * remove the keyword argument of "__root__" in __init__ add a keyword argument of "__root__" in parse_obj fix documents create a method for cheking valid field name * fix parse_obj * Update docs/index.rst Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Apply suggestions from code review Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * add type cheking for custom root type * Update docs/index.rst Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Update index.rst
28 lines
563 B
Python
28 lines
563 B
Python
from typing import List
|
|
import json
|
|
from pydantic import BaseModel
|
|
from pydantic.schema import schema
|
|
|
|
class Pets(BaseModel):
|
|
__root__: List[str]
|
|
|
|
print(Pets(__root__=['dog', 'cat']))
|
|
# > Pets __root__=['dog', 'cat']
|
|
|
|
print(Pets.parse_obj(__root__=['dog', 'cat']))
|
|
# > Pets __root__=['dog', 'cat']
|
|
|
|
print(Pets.schema())
|
|
# > {'title': 'Pets', 'type': 'array', 'items': {'type': 'string'}}
|
|
|
|
pets_schema = schema([Pets])
|
|
print(json.dumps(pets_schema, indent=2))
|
|
|
|
# {
|
|
# "definitions": {
|
|
# "Pets": {
|
|
# "title": "Pets",
|
|
# "type": "array",
|
|
# ...
|
|
|