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 all 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
22 changes: 18 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,12 @@ 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 = kwargs.get('schema_kwargs', {})
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 +190,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