I am trying to parse a JSON object to pydantic validator and it's giving a compiler error.
pydantic.errors.ConfigError: Validators defined with incorrect fields: validate_grade
{
"grade": "A"
"subject": [
{
"physics": "PHY",
"chemistry": "CHE",
"classTime": {
"date": "2024-03-25",
"time": null
},
"labTime":null
}
}
]
}
The implementation code:
class Grade(BaseModel):
@validator("grade")
def validate_grade(cls, val):
if (val == None):
raise ValueError("grade is null")
class subject(BaseModel):
@validator("classTime", pre=True, always=True)
def validate_classTime(cls, value, values):
date_str = value.get('date', '')
time_str = value.get('time', '')
The above implementation is throwing the error after adding the section:
class Grade(BaseModel):
I also want to get the "grade" inside the class subject. I tried different options after referring to the similar implementations, but there is no luck.
There is no model that contains
gradeandsubjectfields.Output: