Skip to content

Instantly share code, notes, and snippets.

@jtemporal
Created October 14, 2018 01:46
Show Gist options
  • Save jtemporal/9938952b24b38f83338838e3648da5d6 to your computer and use it in GitHub Desktop.
Save jtemporal/9938952b24b38f83338838e3648da5d6 to your computer and use it in GitHub Desktop.
v7
from schematics.exceptions import ValidationError
from schematics.models import Model
from schematics.types import StringType, ListType, ModelType, IntType
from udtypes import SizedNumberType
class Cachorrinho(Model):
nome = StringType(required=True, deserialize_from="name",
serialized_name="name")
cor = StringType(required=True, deserialize_from="fur",
serialized_name="fur")
class Pessoa(Model):
def linguagens_validas(value):
linguagens_disponiveis = ["python", "r", "go", "rum"]
result = [a for a in value if a not in linguagens_disponiveis]
if type(value) is list:
return value
if value.lower() not in linguagens_disponiveis:
raise ValidationError(f'Language {value} not available!')
nome = StringType(required=True, deserialize_from="name",
serialized_name="name")
idade = SizedNumberType(serialize_when_none=False, deserialize_from="age",
serialized_name="age")
linguagens = ListType(StringType, serialize_when_none=False,
deserialize_from="lang", serialized_name="blabla",
validators=[linguagens_validas])
cachorrinhos = ListType(ModelType(Cachorrinho), serialize_when_none=False,
deserialize_from="pets", serialized_name="pets")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment