Skip to content

Tag: pydantic

How to do with Pydantic regex validation?

I’m trying to write a validator with usage of Pydantic for following strings (examples): 1.1.0, 3.5.6, 1.1.2, etc.. I’m failing with following syntax: Can anyone help me out what regex syntax should look like? Answer The error you are facing is due to type annotation. As per this is not yet fixed, you can try using pydantic.Field and then

How to search graph database using Python?

I want to search graphs inside Python without the burden of having to learn a new query language. I extensively use pydantic library and I wonder if the two can work together. Answer You can use GQLAlchemy. It is an open source object graph mapper, and it is created using the pydantic library. You can check the implementation at GiHub

Return all extra passed to pydantic model

I’m trying to get a list of all extra fields not defined in the schema. I saw solution posted here but it ignores any nested models. Optimal solution would create a variable in the Pydantic model with extras that I could access after new object with passed data is created but not sure if this is even possible. Here is

Create a pydantic model with dynamic keys

I want to create a Pydantic model for this structure: My first attempt was But this raises an exception: If i change Dict to dict, i don’t get the exception, but the resulting object yields an empty dict: what am i doing wrong? Answer You have a typo in model declaration. Use a colon instead of the equal sign. Then

How to model an empty dictionary in pydantic

I’m working with a request of a remote webhook where the data I want to validate is either there, or an empty dictionary. I would like it to run through the model validator if it’s there but also not choke if it’s an empty dictionary. input 1: input 2: input 3: How would I model something like this in Pydantic

Pydantic created at and updated at fields

I’m new to using Pydantic and I’m using it to set up the models for FastAPI to integrate with my postgres database. I want to make a model that has an updated_at and created_at field which store the last datetime the model was updated and the datetime the model was created. I figured created_at could be something like this: How