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

ValueError when using @validates decorator and passing only to schema. #320

Closed
carlos-alberto opened this Issue Nov 4, 2015 · 2 comments

Comments

Projects
None yet
3 participants
@carlos-alberto

carlos-alberto commented Nov 4, 2015

When providing a custom validator for a field, then instantiating the Schema with the only parameter, and not including the custom validated field, there is a ValueError raised. Not sure if this is intended behaviour but it seems like the custom validator should only be invoked if the field it applies to is passed in?

An example is shown below:

In [4]: from marshmallow import Schema, validates, fields

In [5]:   1 class MySchema(Schema):
          2     a = fields.Str()
          3     b = fields.Str()
          4
          5     @validates('a')
          6     def validate_a(self, value):
          7         return value

In [6]: schema = MySchema(only=('b',)).load({'b': 'data'})
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-ee09f1164833> in <module>()
----> 1 schema = MySchema(only=('b',)).load({'b': 'data'})

~/.virtualenvs/connect/lib/python3.4/site-packages/marshmallow/schema.py in load(self, data, many)
    532         .. versionadded:: 1.0.0
    533         """
--> 534         result, errors = self._do_load(data, many, postprocess=True)
    535         return UnmarshalResult(data=result, errors=errors)
    536

~/.virtualenvs/connect/lib/python3.4/site-packages/marshmallow/schema.py in _do_load(self, data, many, postprocess)
    592         else:
    593             errors = {}
--> 594         self._invoke_field_validators(data=result, many=many)
    595         errors = self._unmarshal.errors
    596         # Run schema-level migration

~/.virtualenvs/connect/lib/python3.4/site-packages/marshmallow/schema.py in _invoke_field_validators(self, data, many)
    751                 field_obj = self.fields[field_name]
    752             except KeyError:
--> 753                 raise ValueError('"{0}" field does not exist.'.format(field_name))
    754
    755             if many:

ValueError: "a" field does not exist.
@sloria

This comment has been minimized.

Member

sloria commented Nov 5, 2015

This does appear to be a bug. I would welcome a PR fixing this. Otherwise, I'll look into fixing this over the weekend.

@eprikazc

This comment has been minimized.

Contributor

eprikazc commented Nov 5, 2015

I have created #322

@sloria sloria added the bug label Nov 5, 2015

@sloria sloria closed this Nov 8, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment