Created
November 26, 2015 23:15
-
-
Save dsludwig/1a0cdb40f89d907bb318 to your computer and use it in GitHub Desktop.
Schematics with relaxed validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from schematics import types | |
from schematics import models | |
from schematics import exceptions | |
class PermissiveType(object): | |
def to_native(self, value, context=None): | |
try: | |
return super(PermissiveType, self).to_native(value, context) | |
except exceptions.BaseError: | |
return value | |
def to_primitive(self, value, context=None): | |
try: | |
return super(PermissiveType, self).to_primitive(value, context) | |
except exceptions.BaseError: | |
return value | |
class IntType(PermissiveType, types.IntType): | |
pass | |
class StringType(PermissiveType, types.StringType): | |
pass | |
class Model(models.Model): | |
def __getattr__(self, attr): | |
return self.__dict__['_initial'].get(attr, None) | |
class Foo(Model): | |
val = IntType() | |
name = StringType() | |
x = Foo({'val': 'asdf', 'bar': 'xyz', 'name': 123.0}, strict=False) | |
print(x.val) | |
print(x.bar) | |
print(x.name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment