Questions tagged [pydantic]

Pydantic is a library for data validation and settings management based on Python type hinting and variable annotations. You can use Pydantic for defining schemas of complex structures in Python.

Pydantic is a library for data validation and settings management based on Python type hinting (PEP484) and variable annotations (PEP526). It allows for defining schemas in Python for complex structures.

1612 questions
0
votes
1 answer

How to call validator if values is initially missed in object to parse, and return value based on another value, which is not part of object?

I'd like to parse object, which initially is not corresponded to the model, means it's fields not match target schema. So I'd like to inference model values, based on other values of the raw object. In the following scenario I'd like to give foo the…
Anton Ovsyannikov
  • 1,010
  • 1
  • 12
  • 30
0
votes
0 answers

FastApi uvicorn server not connecting to localhost

I am trying FASTAPI with the code below and localhost is not reachable with 127.0.0.1:8000 or localhost:8000 also. Tried setting with different port numbers also. At the end it says NameError: name '_TypeAliasForm' is not defined This is on…
J.Krishna
  • 1,010
  • 2
  • 15
  • 21
0
votes
1 answer

How to include $id fields in Pydantic.schema()

According to json-schema.org, it is best practice to include the $id field with objects. I'm struggling with how to get this at the top level, for example; class MySchema(BaseModel): id: str = Field(default="http://my_url/my_schema.json",…
SteveJ
  • 3,034
  • 2
  • 27
  • 47
0
votes
1 answer

Request validation

i wanna validate data in request i have dictionary (a and b are use cases, 1234 are sub use cases) d ={'a':[1,2],'b':[3,4]} and request @router.post("/documents") from typing import Literal, List, Optional, Dict @router.post("/documents") async…
0
votes
1 answer

How shall i define create sub-optional models in Optional[] working with typing and pydantic libs for FastAPI python?

Hi i'm a beginer in FastAPi, getting this error as TypeError: typing.Union[pydantic.main.stats, NoneType] is not a generic class. How shall i create sub-optional models? these are my imports. from typing import Optional,List from pydantic import…
0
votes
0 answers

The best way to validate recursive data with Pydantic and FastAPI

So, I have these enums and models: class FilterType(str, Enum): SIMPLE = "simple" COMPOUND = "compound" class Operator(str, Enum): AND = "and" OR = "or" class FilterAction(str, Enum): INCLUDE = "include" EXCLUDE =…
Jahongir Rahmonov
  • 13,083
  • 10
  • 47
  • 91
0
votes
1 answer

Pydantic way of having user defined object

I am new to Pydantic and I am not sure what am I doing wrong here. I have defined my class and it inherits from BaseModel. class EffectiveCoverage(BaseModel): start_date: str end_date: str env_path: Optional[str] = "" supply: Any =…
Icarus
  • 3
  • 1
0
votes
0 answers

Validate custom root types with Pydantic

I'm trying to validate custom root types with pydantic as followed. My idea is to get a root validator for discriminated union of custom classes or literals. Any idea how to get it working ? # authorization.py from typing import Union, Literal,…
mlisthenewcool
  • 539
  • 3
  • 14
0
votes
0 answers

FastAPI throws JSON serializable error on using Field and constr together

FastAPI throws TypeError: Object of type 'type' is not JSON serializable error on creating openapi.json when using a BaseModel which includes Field and a constr object as default. A MRE: from fastapi import FastAPI from pydantic import BaseModel,…
Yash Nag
  • 1,096
  • 12
  • 16
0
votes
0 answers

Branching/variability of a config file with pydantic

I want to perform validation of the config file based on the selected DB. The config file has a key db_type which can be sqlite or postgresql. And depending on the selected database, there should be a sqlite or postgresql key, the value of which…
AtachiShadow
  • 381
  • 4
  • 13
0
votes
1 answer

How to use `__get__validator__(cls)` method of Pydantics

I am using Pydantic to validate my payload. I have created a class like this - class PayloadValidator(BaseModel): """ This model validates the payload structure and each attribute of payload. """ tenantId: str emailId:…
Jeet Patel
  • 1,140
  • 18
  • 51
0
votes
1 answer

How to specify a BaseSettings Config's env_file based on a field from the same class?

I want to implement this logic from pydantic import BaseSettings class Settings(BaseSettings): ENVIRONMENT: str = 'local' SECRET_KEY: str = 'somekey' class Config: env_file = ".env.development" if Settings.ENVIRONMENT…
Alex
  • 562
  • 1
  • 6
  • 25
0
votes
0 answers

Dynamically Creating nested models in Pydantic

I'm trying to parse AWS GuardDuty Json data, however some nested datafields are finding specific. Is there are way of doing something like this in pydantic: from pydantic import Basemodel from pydantic.main import create_model class…
0
votes
1 answer

How validate few fields with Pydantic in huge nested json?

I have dict-like object, like: data = { # A lot of data here 'json_data_feed': {'address': {'name': 'home_sweet_home'} } # A lot of data here } And i want to create Pydantic model with few fields. Im…
0
votes
1 answer

How to validate entries using Pydantic validator?

I am new to pydantic. What are some techniques I can use in Pydantic to sanitize my data and how to run proper validation checks on it? Can you please review my code and identify any errors in my code? from pydantic import BaseModel from datetime…
1 2 3
99
100