Skip to content

Instantly share code, notes, and snippets.

@dsludwig
Created November 26, 2015 23:15
Show Gist options
  • Save dsludwig/1a0cdb40f89d907bb318 to your computer and use it in GitHub Desktop.
Save dsludwig/1a0cdb40f89d907bb318 to your computer and use it in GitHub Desktop.
Schematics with relaxed validation
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