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".
==================
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
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 marshmallow_models-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 560cd6e6b9b0096e181125814295d140ff928da6e6a928cc7e3854d9e0a8c33a |
|
MD5 | 0a05e3a00cea45de088ff57efe7662dc |
|
BLAKE2b-256 | 15251031dfa3ce534794195e468f8cbbe88d515f60622daca8aba8d792a0c9b2 |