I have a returned result from a webservice, whose values come as they are (see example). The keys are not Optional and must be included.
2 validation errors for Result:
- cause -
str type expected (type=type.error.str) - urls -
str type expected (type=type.error.str)
{ 'code': 0, 'codetext': 'blablabla', 'status': None, 'cause': None, 'urls': None }
class Result(BaseModel):
code: int
codetext: str
status: str = ""
cause: str = ""
urls: str = ""
@validator("status", "cause", "urls")
def replace_none(cls, v):
return v or "None"
First questions is, what is the correct way to handle the None values coming as answer from the webservice and set to 'None' (with single-quotation marks and None as String) and secondly, why are there only 2 and not 3 errors?
Nones? If you want to accept them as input, you should mark them optional.To your second question, at least pydantic 1.9.0 errors on all fields (with a different, clearer error:none is not an allowed value (type=type_error.none.not_allowed)nullas a value". (I'm assuming that theNonecomes from a JSONnullvalue.)