Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][FIX] extendable_fastapi: Coercion must be possible #375

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions extendable_fastapi/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class StrictExtendableBaseModel(
revalidate_instances="always",
validate_assignment=True,
extra="forbid",
strict=True,
):
"""
An ExtendableBaseModel with strict validation.
Expand All @@ -24,6 +23,4 @@ class StrictExtendableBaseModel(
(default is "never")
* validate_assignment=True: revalidate the model when the data is changed (default is False)
* extra="forbid": Forbid any extra attributes (default is "ignore")
* strict=True: raise an error if a value's type does not match the field's type
annotation (default is False; Pydantic attempts to coerce values to the correct type)
"""
13 changes: 4 additions & 9 deletions extendable_fastapi/tests/test_strict_extendable_base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,9 @@ def test_StrictModel_extra_forbidden(self):
with self.assertRaises(ValidationError):
self.StrictModel(x=1, z=3, d=None)

def test_Model_strict_false(self):
# Coerce str->date is allowed
m = self.Model(x=1, d=None)
def test_StrictModel_strict_false(self):
# Coerce str->date is allowed to enable coercion from JSON
# by FastAPI
m = self.StrictModel(x=1, d=None)
m.d = "2023-01-01"
self.assertTrue(m.model_validate(m))

def test_StrictModel_strict_true(self):
# Coerce str->date is forbidden
m = self.StrictModel(x=1, d=None)
with self.assertRaises(ValidationError):
m.d = "2023-01-01"