Junior — Middle
Have you had experience using the Pydantic library for data validation?
sobes.tech AI
Answer from AI
Yes, I have used the Pydantic library for data validation and serialization in Python projects. It allows describing data structures with models, automatically checking types and values, and transforming data into the required format. For example:
from pydantic import BaseModel, ValidationError
class User(BaseModel):
id: int
name: str
email: str
try:
user = User(id='123', name='Ivan', email='ivan@example.com')
print(user)
except ValidationError as e:
print(e)
In this example, Pydantic automatically converts the string '123' into the number 123 and checks the data's correctness.