first chapter
This commit is contained in:
56
main.py
56
main.py
@@ -1,30 +1,54 @@
|
|||||||
# example from root.cz
|
# example from root.cz
|
||||||
# https://www.root.cz/clanky/validace-dat-v-pythonu-s-vyuzitim-knihovny-pydantic/
|
# https://www.root.cz/clanky/validace-dat-v-pythonu-s-vyuzitim-knihovny-pydantic/
|
||||||
|
from models.address import Address
|
||||||
from models.User import User, UserNone, UserUnion, UserPositiveInt, UserFieldValidator, UserMaxLength
|
from models.character import CharacterModel, CharacterModelOptionalAddress
|
||||||
|
from models.user import User, UserNone, UserUnion, UserPositiveInt, UserFieldValidator, UserMaxLength
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
|
# Basic validation
|
||||||
print("Hello from pydantic-demo!")
|
print("Hello from pydantic-demo!")
|
||||||
print(f"{User(name="John", surname="Doe", age=42, registered=True)=}")
|
print(f"{User(name='John', surname='Doe', age=42, registered=True)=}")
|
||||||
print(f"{User(name="Adam", surname="Bernau", age=52, registered=True)=}")
|
print(f"{User(name='Adam', surname='Kameník', age=52, registered=True)=}")
|
||||||
print(f"{UserNone(name="Adam", surname="Bernau", age=None)=}")
|
print(f"{UserNone(name='Tomáš', surname='Macek', age=None)=}") # preferred variant
|
||||||
print(f"{UserUnion(name="Adam", surname="Bernau", age=None)=}")
|
print(f"{UserUnion(name='Adam', surname='Bernau', age=None)=}")
|
||||||
print(f"{UserUnion(name="Adam", surname="Bernau")=}")
|
print(f"{UserUnion(name='Adam', surname='Bernau')=}")
|
||||||
print(f"{UserPositiveInt(name="Adam", surname="Bernau")=}")
|
print(f"{UserPositiveInt(name='Adam', surname='Bernau')=}")
|
||||||
#print(f"{UserPositiveInt(name="Adam", surname="Bernau", age=-50)=}")
|
#print(f"{UserPositiveInt(name="Adam', surname='Bernau', age=-50)=}")
|
||||||
print(f"{UserFieldValidator(name="Adam", surname="Bernau", age=18)=}")
|
print(f"{UserFieldValidator(name='Adam', surname='Bernau', age=18)=}")
|
||||||
print(f"{UserMaxLength(name="Adam", surname="Bernau", age=18)=}")
|
print(f"{UserMaxLength(name='Adam', surname='Bernau', age=18)=}")
|
||||||
#print(f"{UserMaxLength(name="Adam", surname="Bernaukjghsmtlmgchs", age=18)=}")
|
#print(f"{UserMaxLength(name='Adam', surname='Bernaukjghsmtlmgchs', age=18)=}")
|
||||||
|
|
||||||
# JSON
|
# JSON
|
||||||
data = """
|
data = """
|
||||||
{"name": "John",
|
{"name": "Marek",
|
||||||
"surname": "Doe",
|
"surname": "Kdolský",
|
||||||
"age": 18
|
"age": 50
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
print(f"{UserMaxLength.model_validate_json(data)=}")
|
user1 = UserMaxLength.model_validate_json(data)
|
||||||
|
print(f"{user1=}")
|
||||||
|
print(f"{user1.model_dump_json()=}")
|
||||||
|
print(user1.model_dump_json(indent=4))
|
||||||
|
|
||||||
|
# Nested structure and JSON
|
||||||
|
character = CharacterModel(
|
||||||
|
role='Director',
|
||||||
|
user=User(name='John', surname='Doe', age=42, registered=True),
|
||||||
|
address=Address(street='Dlouhá', house_number=77, city='Prague'),
|
||||||
|
)
|
||||||
|
print(f"{character=}")
|
||||||
|
print(character.model_dump_json(indent=4))
|
||||||
|
|
||||||
|
|
||||||
|
character1 = CharacterModelOptionalAddress(
|
||||||
|
role='Director',
|
||||||
|
user=UserFieldValidator(name='John', surname='Doe', age=19, registered=True),
|
||||||
|
# address=Address(street='Dlouhá', house_number=77, city='Prague'),
|
||||||
|
)
|
||||||
|
print(f"Optional address {character1=}")
|
||||||
|
print(character1.model_dump_json(indent=4))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
9
models/address.py
Normal file
9
models/address.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class Address(BaseModel):
|
||||||
|
street: str
|
||||||
|
house_number: int | str
|
||||||
|
city: str
|
||||||
18
models/character.py
Normal file
18
models/character.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from models.address import Address
|
||||||
|
from models.user import User, UserFieldValidator
|
||||||
|
|
||||||
|
|
||||||
|
class CharacterModel(BaseModel):
|
||||||
|
role: str
|
||||||
|
user: User
|
||||||
|
address: Address
|
||||||
|
|
||||||
|
class CharacterModelOptionalAddress(BaseModel):
|
||||||
|
role: str
|
||||||
|
user: UserFieldValidator
|
||||||
|
address: Address | None = None
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Union, Optional
|
from typing import Union, Optional
|
||||||
from pydantic import BaseModel, PositiveInt, field_validator, Field
|
from pydantic import BaseModel, PositiveInt, field_validator, Field, ValidationError
|
||||||
|
|
||||||
|
|
||||||
class User(BaseModel):
|
class User(BaseModel):
|
||||||
@@ -38,24 +38,28 @@ class UserPositiveInt(BaseModel):
|
|||||||
class UserFieldValidator(BaseModel):
|
class UserFieldValidator(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
surname: str
|
surname: str
|
||||||
age: PositiveInt | None
|
age: int | None
|
||||||
registered: bool = False
|
registered: bool = False
|
||||||
|
|
||||||
@field_validator("age")
|
@field_validator("age")
|
||||||
def validate_age(cls, value):
|
def validate_age(cls, value):
|
||||||
|
if value < 0:
|
||||||
|
raise ValidationError("age must be positive")
|
||||||
if value < 18:
|
if value < 18:
|
||||||
raise ValueError("age must be at least 18")
|
raise ValidationError("age must be at least 18")
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
class UserMaxLength(BaseModel):
|
class UserMaxLength(BaseModel):
|
||||||
name: str = Field(..., max_length=10)
|
name: str = Field(..., max_length=10)
|
||||||
surname: str = Field(..., max_length=10)
|
surname: str = Field(..., max_length=12)
|
||||||
age: PositiveInt | None
|
age: int | None
|
||||||
registered: bool = False
|
registered: bool = False
|
||||||
|
|
||||||
@field_validator("age")
|
@field_validator("age")
|
||||||
def check_age(cls, value):
|
def check_age(cls, value):
|
||||||
|
if value < 0:
|
||||||
|
raise ValidationError("age must be positive")
|
||||||
if value < 18:
|
if value < 18:
|
||||||
raise ValueError("You are too young to register")
|
raise ValueError("You are too young to register")
|
||||||
return value
|
return value
|
||||||
Reference in New Issue
Block a user