Skip to content

Run @validates for a field with a dotted attribute - #3087

Open
andrewstellman wants to merge 1 commit into
marshmallow-code:devfrom
andrewstellman:validates-dotted-attribute
Open

andrewstellman wants to merge 1 commit into
marshmallow-code:devfrom
andrewstellman:validates-dotted-attribute

Conversation

@andrewstellman

Copy link
Copy Markdown

A @validates method never runs for a field with a dotted attribute:

class S(Schema):
    name = fields.Str(attribute="a.b")

    @validates("name")
    def validate_name(self, value, **kwargs):
        raise ValidationError("Invalid value.")

S().load({"name": "x"})  # {'a': {'b': 'x'}}, no error

The loaded value is stored nested under a, but _invoke_field_validators looked it up with the literal key "a.b", got a KeyError and skipped the validator. It now reads the value along the dotted path. The test in tests/test_decorators.py covers many=False and many=True.

I sent this to the Tidelift security contact first, and they asked for a public PR.

Found with the Quality Playbook, an AI code-review tool I maintain; I reproduced it and reviewed the fix myself.

A field with attribute="a.b" is loaded into a nested dict by set_value,
but _invoke_field_validators looked the value up with the literal
attribute string, got a KeyError and skipped the validator. Read the
value along the dotted path instead.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant