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

Fix field validation on Base64Bytes and Base64Str #9263

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion pydantic/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Comment on lines +2347 to +2348
Copy link
Contributor

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question!

return core_schema.with_info_after_validator_function(
function=self.decode,
schema=core_schema.bytes_schema(),
schema=core_schema.bytes_schema(**base_schema),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't _convert_schema be more appropriate here, instead of unpacking and recreating, perform the union?

serialization=core_schema.plain_serializer_function_ser_schema(function=self.encode),
)

Expand Down
36 changes: 36 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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'),
[
Expand Down
Loading