10BC0 dataclass validation by samuelcolvin · Pull Request #334 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
adding example in docs
  • Loading branch information
samuelcolvin committed Dec 27, 2018
commit 5f00110475111da6645ff23812d03ad6b0b6b0d3
14 changes: 14 additions & 0 deletions docs/examples/ex_nested_dataclasses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pydantic import UrlStr
from pydantic.dataclasses import dataclass

@dataclass
class NavbarButton:
href: UrlStr

@dataclass
class Navbar:
button: NavbarButton

navbar = Navbar(button=('https://example.com',))
print(navbar)
# > Navbar(button=NavbarButton(href='https://example.com'))
13 changes: 11 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ dataclasses

.. note::

New in version ``v0.14.0``.
New in version ``v0.14``.

If you don't want to use pydantic's ``BaseModel`` you can instead get the same data validation on standard
`dataclasses <https://docs.python.org/3/library/dataclasses.html>`_ (introduced in python 3.7).
Expand All @@ -126,7 +126,16 @@ created by the standard library ``dataclass`` decorator.
``pydantic.dataclasses.dataclass``'s arguments are the same as the standard decorator, except one extra
key word argument ``config`` which has the same meaning as :ref:`Config <config>`.

Currently validators don't work on validators, if it's something you want please create an issue on github.

Since version ``v0.17`` nested dataclasses are support both in dataclasses and normal models.

.. literalinclude:: examples/ex_nested_dataclasses.py

(This script is complete, it should run "as is")

Dataclasses attributes can be populated by tuples, dictionaries or instances of that dataclass.

Currently validators don't work with dataclasses, if it's something you want please create an issue on github.

Choices
.......
Expand Down
0