Skip to main content

Enum field for Marshmallow

Project description

# marshmallow-enum

Enum field for use with Marshmallow.

## Installation

```bash
pip install --user marshmallow_enum
```

If you're on a version before 3.4, you'll also need to install `enum34`.

## Using The Field

To make use of the field, you must have an existing Enum:

```python
from enum import Enum


class StopLight(Enum):
green = 1
yellow = 2
red = 3
```

Then, declare it as a field in a schema:

```python
from marshmallow import Schema
from marshmallow_enum import EnumField


class TrafficStop(Schema):
light_color = EnumField(StopLight)
```

By default, the field will load and dump based on the _name_ given to an enum value.

```python
schema = TrafficStop()
schema.dump({'light_color': EnumField.red}).data
# {'light_color': 'red'}

schema.load({'light_color': 'red'}).data
# {'light_color': EnumField.red}
```

### Customizing loading and dumping behavior

To customize how an enum is serialized or deserialized, there are three options:

- Setting `by_value=True`. This will cause both dumping and loading to use the value of the enum.
- Setting `load_by=EnumField.VALUE`. This will cause loading to use the value of the enum.
- Setting `dump_by=EnumField.VALUE`. This will cause dumping to use the value of the enum.

If either `load_by` or `dump_by` are unset, they will follow from `by_value`.

Additionally, there is `EnumField.NAME` to be explicit about the load and dump behavior, this
is the same as leaving both `by_value` and either `load_by` and/or `dump_by` unset.

### Custom Error Message

A custom error message can be provided via the `error` keyword argument. It can accept three
format values:

- `{input}`: The value provided to the schema field
- `{names}`: The names of the individual enum members
- `{values}`: The values of the individual enum members

Previously, the following inputs were also available but are deprecated and will be removed in 1.6:

- `{name}`
- `{value}`
- `{choices}`


Version 1.4
===========

* Deprecate the `{choices}`, `{name}` and `{value}` format keys for custom error messages
* Allow configure loading and dumping independently


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

marshmallow-enum-1.4.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

marshmallow_enum-1.4-py2.py3-none-any.whl (4.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file marshmallow-enum-1.4.tar.gz.

File metadata

File hashes

Hashes for marshmallow-enum-1.4.tar.gz
Algorithm Hash digest
SHA256 68bf64631b44979c7e25dcf8a335f3dda021583e2ea225c6624f62df29a61e38
MD5 8f49b0a02413170aefd035b523ffe9fa
BLAKE2b-256 e48bda5b8b5fa745db7594dc0e86647395e01764b9732ca92940a8699bc91419

See more details on using hashes here.

File details

Details for the file marshmallow_enum-1.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for marshmallow_enum-1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 82de7d525abb7b37d5a1f902604d89bbe33e9a237198827ca8d2c9a3b9bb9f9e
MD5 7d4889e9a3c0cdc4fc5102cc2b558065
BLAKE2b-256 3dbf578363459f82c7767bf89c4c6cf9cf37962f03b98dcbc49be6544df87bc2

See more details on using hashes here.

Supported by

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