Ensure that your schema field will be a list
Project description
marshmallow-always-list-field
This is a small package that will ensure that your marshmallow will alway contain list.
Some times you want to ensure that your marshmallow schema will always return a list, even if the input is a single item. Just to make an API response consistent.
Installation
pip install marshmallow-always-list-field
Usage
from marshmallow_always_list_field import AlwaysListField
class MySchema(Schema):
my_list = AlwaysListField(fields.String())
If input is:
{
"my_list": "foo"
}
it will result with:
{
"my_list": ["foo"]
}
This will work with nested fields as well.
If nested field is:
class NestedSchema(Schema):
my_list = AlwaysListField(fields.String())
class MySchema(Schema):
nested = fields.Nested(NestedSchema)
and input is:
{
"nested": {
"my_list": "foo"
}
}
result will be:
{
"nested": {
"my_list": ["foo"]
}
}
Additionally you can do something like this:
class NestedSchema(Schema):
data = fields.String()
class SampleSchema(Schema):
nested = AlwaysListField(fields.Nested(NestedSchema))
assert result == {"nested": [{"data": "hello"}]}
and input is:
{
"nested": {
"data": "hello"
}
}
result will be:
{
"nested": [{"data": "hello"}]
}
Development
pip install -r requirements.txt
Testing
pytest
License
MIT
Author
Dominik Szymanski
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for always_list_field-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0c945836626dff43cc782ce28c858086e80740184af8aca750e88dae27e6602 |
|
MD5 | 50d06f151ba74290ea626a6f6b33567d |
|
BLAKE2b-256 | be1034ec1ab8044ca479606e1a7a6158a04ad36e2bd1bc6dbf2e6acd9320c160 |