Skip to content
Advertisement

Pydantic validation errors with None values

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:

  • causestr type expected (type=type.error.str)
  • urlsstr type expected (type=type.error.str)
JavaScript
JavaScript

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?

Advertisement

Answer

You can mark fields as required but optional by declaring the field optional and using ellipsis (...) as its value. Here is a complete, simplified example with tests to verify it works (execute with pytest <file>):

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement