I have a pydantic BaseModel which looks like:
class BookCreation(BaseModel):
book_name: str
book_size: Optional[str]
book_author: str
Now, when the user passes the an input with inconsistent case as below:
{
"book_name": "new-book",
"book_SIZE": "23",
"book_author": "test value"
}
Now this inputs is treated as if it has only two valid attributes i.e, book_name and book_author and book_size is assigned to None.
How do I ensure that the input changes to snake_case when I parse the input?
You can user alias generators for this. See the following example:
Which prints:
Depending on your use case you can adapt the
to_lowerfunction to handle whatever convention you might want to support.I hope this helps!