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

Support kwargs in marshmallow validator #516

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CHANGELOG
3.7.0 (unreleased)
==================

- Nothing changed yet.
- Add support for passing keyword arguments to marshmallow validator.


3.6.1 (2019-11-13)
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Cornice:
* Gabriela Surita <[email protected]>
* Gael Pasgrimaud <[email protected]>
* George V. Reilly <[email protected]>
* George Mamalakis <[email protected]>
* Graham Higgins <[email protected]>
* G.Tjebbes <[email protected]>
* Guillaume Gauvrit <[email protected]>
Expand Down
24 changes: 20 additions & 4 deletions cornice/validators/_marshmallow.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def _validator(request, schema=None, deserializer=None, **kwargs):
The content of the location is deserialized, validated and stored in
the ``request.validated`` attribute.

Keyword arguments to be included when initialising the marshmallow
schema can be passed as a dict in ``kwargs['schema_kwargs']`` variable.

.. note::

If no schema is defined, this validator does nothing.
Expand All @@ -46,10 +49,14 @@ def _validator(request, schema=None, deserializer=None, **kwargs):
if schema is None:
return

schema = _instantiate_schema(schema)
# see if the user wants to set any keyword arguments for their schema
schema_kwargs = {}
if 'schema_kwargs' in kwargs:
schema_kwargs = kwargs.get('schema_kwargs', {})
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry this code suggestion thing didn't work as I expected. You can remove the above lines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, you lost me in this. The truth is that I accepted this change without seeing it too deeply, I thought it was in a different spot. Now I'm a bit confused, has it been merged with my code or not?

Copy link
Contributor

Choose a reason for hiding this comment

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

Just that these 3 lines can be replaced by the last one

schema_kwargs = kwargs.get('schema_kwargs', {})

Copy link
Contributor Author

@mamalos mamalos Dec 5, 2019

Choose a reason for hiding this comment

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

Is it OK now? I'm not familiar with these github's features (merging suggested changes and editing files within github), that's why I'm asking.

So, I've edited the file within github to only contain schema_kwargs = kwargs.get('schema_kwargs', {}) and I suppose it's OK now, but I'm not sure I saw the checks running again.

EDIT:
I've clicked on the checks and the logs show they have run successfully after I made the last changes within github, so I suppose we're OK.

schema = _instantiate_schema(schema, **schema_kwargs)

class ValidatedField(marshmallow.fields.Field):
def _deserialize(self, value, attr, data):
def _deserialize(self, value, attr, data, **kwargs):
schema.context.setdefault('request', request)
deserialized = schema.load(value)
# marshmallow 2.x returns a tuple, 3/x will always throw
Expand Down Expand Up @@ -185,8 +192,17 @@ def validator(request, schema=None, deserializer=None, **kwargs):
request.validated.update(deserialized)


def _instantiate_schema(schema):
def _instantiate_schema(schema, **kwargs):
"""
Returns an object of the given marshmallow schema.

:param schema: The marshmallow schema class with which the request should
be validated
:param kwargs: The keyword arguments that will be provided to the
marshmallow schema's constructor
:return: The object of the marshmallow schema
"""
if not inspect.isclass(schema):
raise ValueError('You need to pass Marshmallow class instead '
'of schema instance')
return schema()
return schema(**kwargs)
22 changes: 10 additions & 12 deletions tests/validationapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,6 @@ class Meta:
unknown = EXCLUDE
username = marshmallow.fields.String()

class MSignupGroupSchema(marshmallow.Schema):
class Meta:
strict = True
unknown = EXCLUDE
username = marshmallow.fields.String()

def __init__(self, *args, **kwargs):
kwargs['many'] = True
marshmallow.Schema.__init__(self, *args, **kwargs)

import random

class MNeedsContextSchema(marshmallow.Schema):
Expand All @@ -383,8 +373,15 @@ def m_bound_post(request):
def signup_post(request):
return request.validated

@m_group_signup.post(
schema=MSignupGroupSchema, validators=(marshmallow_body_validator,))
# callback that returns a validator with keyword arguments for marshmallow
# schema initialisation. In our case it passes many=True to the desired
# schema
def get_my_marshmallow_validator_with_kwargs(request, **kwargs):
kwargs['schema'] = MSignupSchema
kwargs['schema_kwargs'] = {'many': True}
return marshmallow_body_validator(request, **kwargs)

@m_group_signup.post(validators=(get_my_marshmallow_validator_with_kwargs,))
mamalos marked this conversation as resolved.
Show resolved Hide resolved
def m_group_signup_post(request):
return {'data': request.validated}

Expand Down Expand Up @@ -511,6 +508,7 @@ class Meta:
def m_form(request):
return request.validated


def includeme(config):
config.include("cornice")
config.scan("tests.validationapp")
Expand Down