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

Inconsistent field_name when validating schema #2170

Open
matejsp opened this issue Aug 17, 2023 · 3 comments
Open

Inconsistent field_name when validating schema #2170

matejsp opened this issue Aug 17, 2023 · 3 comments
Labels

Comments

@matejsp
Copy link

matejsp commented Aug 17, 2023

There is inconsistent field_name meaning when validating fields:
raising ValidationError: it expected data_key as field_name
@marshmallow.validates: it expects field name

And the same issue for:

            raise ValidationError(
                {
                    "field_a": ["field_a must be greater than field_b"],
                    "field_b": ["field_a must be greater than field_b"],
                }
            )

Given the following code:

import marshmallow
from marshmallow import fields

class ApiKeySchema(marshmallow.Schema):
    ip_addresses = fields.String(required=True, load_from='ipAddresses', data_key='ipAddresses')

    @marshmallow.validates_schema(skip_on_field_errors=False)
    def validate_restricted_permission2(self, data, **kwargs) -> None:
        raise marshmallow.ValidationError({'ip_addresses': 'Some random error 2.'})

    @marshmallow.validates_schema(skip_on_field_errors=False)
    def validate_restricted_permission(self, data, **kwargs) -> None:
        if marshmallow.__version__.startswith('2.'):
            raise marshmallow.ValidationError('Some random error.', field_names=['ip_addresses'])
        else:
            raise marshmallow.ValidationError('Some random error.', field_name='ip_addresses')

    @marshmallow.validates(field_name='ip_addresses')
    def validate_ip_addresses(self, data) -> None:
        raise marshmallow.ValidationError('Some random field error.')


if __name__ == '__main__':
    print(marshmallow.__version__)
    schema = ApiKeySchema(strict=True) if marshmallow.__version__.startswith('2.') else ApiKeySchema()
    try:
        schema.load({'ipAddresses': 'bla'})
    except marshmallow.ValidationError as e:
        print(e.messages)
2.15.3
{'ip_addresses': ['Some random field error.', 'Some random error.'], '_schema': [{'ip_addresses': 'Some random error 2.'}]}

2.21.0
{'ipAddresses': ['Some random field error.'], 'ip_addresses': ['Some random error.', 'Some random error 2.']}

3.20.1
{'ipAddresses': ['Some random field error.'], 'ip_addresses': ['Some random error.', 'Some random error 2.']}

how it should be:
{'ipAddresses': ['Some random field error.', 'Some random error.', 'Some random error 2.']}

Changing both field_name to data_key values then it complains about non existing field on line: @marshmallow.validates(field_name='ipAddresses')

@lafrech lafrech added the bug label Aug 17, 2023
@lafrech
Copy link
Member

lafrech commented Aug 17, 2023

Thanks for reporting. Looks like a bug indeed. data_key should be used consistently as error key.

@niviPy
Copy link

niviPy commented Sep 26, 2023

Can I work on this?

@lafrech
Copy link
Member

lafrech commented Sep 26, 2023

Sure. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants