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

Validation of required nested field is not consistent with previous version #2208

Open
matejsp opened this issue Nov 22, 2023 · 0 comments
Open

Comments

@matejsp
Copy link

matejsp commented Nov 22, 2023

If you have a nested form on web or if you want to do validation it would be prefered to list the errors inside nested schema not just that nested item is missing. This way you can map the errors to a input form more preciesly.

In marhsmallow 2 you would get what is missing in required nested schema.

{'corpo_info': {'company_name': ['Missing data for required field.'], 'address': ['Missing data for required field.']}}

Now you get only:

{'corpo_info': ['Missing data for required field.']}

I prefer previous behaviour.

import marshmallow
from marshmallow import fields


class BeneficiaryCorpoInfo(marshmallow.Schema):
    company_name = fields.Str(load_only=True, allow_none=True, required=True)
    address = fields.Str(load_only=True, allow_none=True, required=True)


class BeneficiaryInfoDataSchema(marshmallow.Schema):

    corpo_info = fields.Nested(
        BeneficiaryCorpoInfo, required=True, load_only=True, allow_none=True
    )


try:
    if marshmallow.__version__.startswith('2.'):
        BeneficiaryInfoDataSchema(strict=True).load({})
    else:
        BeneficiaryInfoDataSchema().load({})
except marshmallow.ValidationError as e:
    print(e.messages)


BeneficiaryInfoDataSchema(strict=True).validate('')
# marhsmallow 2: {'corpo_info': {'company_name': ['Missing data for required field.'], 'address': ['Missing data for required field.']}}
# marshmallow 3: {'corpo_info': ['Missing data for required field.']}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant