Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

always-list-field-1.0.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

always_list_field-1.0.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file always-list-field-1.0.0.tar.gz.

File metadata

  • Download URL: always-list-field-1.0.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for always-list-field-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1f6da0dc49632d91cb168ffe1b0207e26951c1c190cf678540251ec51ffeef08
MD5 e2b04db07503bd96dbfba33c569626c3
BLAKE2b-256 67bed9df8f7757092da73a8d6bf6879ffeb0a448d0ad87b91ed2cd1806089c88

See more details on using hashes here.

File details

Details for the file always_list_field-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for always_list_field-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0c945836626dff43cc782ce28c858086e80740184af8aca750e88dae27e6602
MD5 50d06f151ba74290ea626a6f6b33567d
BLAKE2b-256 be1034ec1ab8044ca479606e1a7a6158a04ad36e2bd1bc6dbf2e6acd9320c160

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page