Skip to main content

Simple and flexible schema definition.

Project description

# Gesiel - Simple and Flexible Schema Definition



Gesiel allows the definition of powerful schemas in python.

- No dependencies.
- Plain python objects.
- It works.


### Simple Usage
```python
from gesiel import fields, Schema
from gesiel.validators import Length, Email

class Person(Schema):
name = fields.String()
age = fields.Int(required=True)
email = fields.String(validators=[Email()])
dead = fields.Bool(default=False)
description = fields.String(validators=[Length(5, 150)])

p = Person.from_dict({'name': 'Gesiel', 'age': 29, 'email':'gesiel@email.com'})
p.description = 'Test description'
assert p.to_dict() == {
'name': 'Gesiel',
'age': 29,
'email': 'gesiel@email.com',
'dead': False,
'description': 'Test description'
}
```



### Custom fields
```python
class Code(Int):
def load(self, value):
code = super(Code, self).load(value)
return code*2

class Person(Schema):
code = Code()

assert Person.from_dict({'code': "25"}).code == 50
```

### Validators
```python
from gesiel.exceptions import InvalidSchema, ValidationError

def validate_name(name):
if name[0] != "f":
raise ValidationError('Name does not starts with f')

class Person(Schema):
name = fields.String(validators=[Length(2, 6), validate_name])

with pytest.raises(InvalidSchema):
Person.from_dict({'name': "gesiel"})

with pytest.raises(InvalidSchema):
Person.from_dict({'name': "g"})

assert Person.from_dict({'name': "fesiel"}).name == "fesiel"
```



### Skip None
```python
class Person(Schema):
name = fields.String(required=True)
age = fields.Int()

p = Person.from_dict({'name': None, 'age':23})
assert p.to_dict() == {'name': None, 'age': 23}
assert p.to_dict(skip_none=True) == {'age': 23}

with pytest.raises(InvalidSchema, match='Missing required field'):
Person.from_dict({'name': None}, skip_none=True)
```


### Post load
```python
class Person(Schema):
name = fields.String()
lastname = fields.String()

def post_load(self, context):
context['full_name'] = context['name'] + " " + context['lastname']
return context


assert Person.from_dict({'name': 'Gesiel', 'lastname': 'Souza'}).full_name == 'Gesiel Souza'
```


### Custom language
```python
from gesiel.messages import Messages, Config, EnglishLanguage

c = Config({Messages.MISSING_REQUIRED_FIELD: 'Campo obrigatório'})

class Person(Schema):
name = fields.String(required=True)

with pytest.raises(InvalidSchema, match='Campo obrigatório') as e:
Person.from_dict({})

c.language = EnglishLanguage
with pytest.raises(InvalidSchema, match='Missing required field') as e:
Person.from_dict({})
```


### Merge
```python
class Person(Schema):
name = fields.String()
last_name = fields.String()
age = fields.Int()
dead = fields.Bool(default=False)

assert Person.from_dict({'name': None, 'age': 20, 'last_name': None}).merge({'age': 35, 'last_name': 'Souza', 'dead': None}, skip_none=True).to_dict() == {'name': None, 'age': 35, 'last_name': 'Souza', 'dead': False}

```

## Important:
- Still in alpha.
- Full docs are in progress.

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

gesiel-0.1.6.tar.gz (4.1 kB view details)

Uploaded Source

File details

Details for the file gesiel-0.1.6.tar.gz.

File metadata

  • Download URL: gesiel-0.1.6.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for gesiel-0.1.6.tar.gz
Algorithm Hash digest
SHA256 ffce4fdc8222b3e2d3805391e53797004d3c9bea3e792ee0a27e77920f103682
MD5 27a22022eb3f40a982c2911777217258
BLAKE2b-256 e55df6d0afcc75b1a72a35336d64d9c7b1a6261b3c47a7dcfd973bd6a1fe82a9

See more details on using hashes here.

Supported by

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