Skip to main content

Object models with validation and serialization using Marshmallow fields and validators.

Project description

Marshmallow Models
==================


Inspired by [Schematics](https://github.com/schematics/schematics).

Whereas Marshmallow is an excellent serialization/deserialization
and validation library, it wasn't intended to be a class or type
definition library, which Schematics was.

This library provides a Schematics-like Model but
using Marshmallow's Fields and validation.


Usage
-----

Models are defined like Schemas, but whereas a Schema is instantiated
with parameters and then used to schema.dump(data) or schema.load(data),
or schema.validate(data),
Models are instantiated, attributes may be assigned to them, and then
they can be .dump()'d, .dumps()'d or .validate()'d.

```python
from marshmallow_models import Model
from marshmallow.fields import String, Integer

class PersonModel(Model):
name = String(required=True, default='Anonymous')
age = Integer(required=True)

person = PersonModel()

person.name = 'Tester'
person.age = 100

# or equivalently:
person = PersonModel({'name': 'Tester', 'age': 100})

# or equivalently:
person = PersonModel(name='Tester', age=100)

# throws marshmallow.exceptions.ValidationError if invalid
person.validate()

person.dump().data # {'name': 'Tester', 'age': 100}
```

Configuration
-------------

Marshmallow Models support the "class Meta" configuration method,
and also support defining the "class Meta" using the alias
"class Options".


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_models-0.1.0.tar.gz (3.4 kB view hashes)

Uploaded Source

Built Distribution

marshmallow_models-0.1.0-py2.py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 2 Python 3

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