-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Fix field validation on Base64Bytes and Base64Str #9263
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2344,9 +2344,11 @@ def __get_pydantic_json_schema__( | |
return field_schema | ||
|
||
def __get_pydantic_core_schema__(self, source: type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema: | ||
base_schema: Any = handler(source) | ||
del base_schema['type'] | ||
return core_schema.with_info_after_validator_function( | ||
function=self.decode, | ||
schema=core_schema.bytes_schema(), | ||
schema=core_schema.bytes_schema(**base_schema), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't |
||
serialization=core_schema.plain_serializer_function_ser_schema(function=self.encode), | ||
) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5508,6 +5508,42 @@ class Model(BaseModel): | |
} | ||
|
||
|
||
def test_base64_with_valid_min_length() -> None: | ||
class Model(BaseModel): | ||
base64_value: Base64Bytes = Field(min_length=3) | ||
|
||
value = b'Zm9v' | ||
m = Model.model_construct(base64_value=value) | ||
assert m.base64_value == value | ||
assert Model.model_json_schema() == { | ||
'properties': { | ||
'base64_value': { | ||
'format': 'base64', | ||
'minLength': 3, | ||
'title': 'Base64 Value', | ||
'type': 'string', | ||
} | ||
}, | ||
'required': ['base64_value'], | ||
'title': 'Model', | ||
'type': 'object', | ||
} | ||
Comment on lines
+5511
to
+5530
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a test for non url safe base64 encoding too |
||
|
||
|
||
def test_base64_with_invalid_min_length() -> None: | ||
"""Check that an error is raised when the length of the base64 | ||
value is less or more than the min_length and max_length""" | ||
|
||
class Model(BaseModel): | ||
base64_value: Base64Bytes = Field(min_length=3, max_length=5) | ||
|
||
with pytest.raises(ValidationError): | ||
Model(**{'base64_value': b''}) | ||
|
||
with pytest.raises(ValidationError): | ||
Model(**{'base64_value': b'123456'}) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
('field_type', 'input_data'), | ||
[ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it necessary to remove the type from the base schema?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great question!