I have a marshmallow schema validation like this:
JavaScript
x
3
1
class MyFilterSchema(Schema):
2
ids = fields.List(fields.Str(validate=non_empty), required=True)
3
Then in my endpoint I call the schema validation: MyFilterSchema().load(flask.request.args)
Now I try to call the HTTP GET endpoint which is using this validation. But I get 'ids': ['Not a valid list.']
I tried different ways:
JavaScript
1
4
1
/myendpoint?ids=1,2,3
2
/myendpoint?ids=1&ids=2
3
/myendpoint?ids=[1,2]
4
but no luck. How must the endpoint be called that marshmallow recognizes my GET parameter as list?
Advertisement
Answer
One way is to use a custom implementation of a validation field instead of the integrated list field.
JavaScript
1
9
1
class DelimitedListField(fields.List):
2
def _deserialize(self, value, attr, data, **kwargs):
3
try:
4
return value.split(",")
5
except AttributeError:
6
raise exceptions.ValidationError(
7
f"{attr} is not a delimited list it has a non string value {value}."
8
)
9
This can be used in the schema as follow:
JavaScript
1
3
1
class MyFilterSchema(Schema):
2
ids = DelimitedListField(fields.Str(validate=non_empty), required=True)
3
and would accept calls in the format:
JavaScript
1
2
1
/myendpoint?ids=1,2,3
2