Created
June 4, 2012 21:50
-
-
Save jbpotonnier/2871052 to your computer and use it in GitHub Desktop.
Very basic json validation (you more likely want something like kwalify or WTForm)
This file contains hidden or 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 exceptions import Exception | |
def is_valid(model, instance): | |
missing_fields = set(model) - set(instance) | |
if missing_fields: | |
raise MissingFields(missing_fields) | |
missing_types = set(instance) - set(model) | |
if missing_types: | |
raise MissingTypes(missing_types) | |
type_errors = [(k, model[k], type(v)) for (k, v) in instance.items() if type(v) is not model[k]] | |
if type_errors: | |
field, expected_type, actual_type = type_errors[0] | |
raise BadTypeException(field, expected_type, actual_type) | |
class BadTypeException(Exception): | |
def __init__(self, field, expected_type, actual_type): | |
self.field = field | |
self.expected_type = expected_type | |
self.actual_type = actual_type | |
def __str__(self): | |
return "{field} should have type {expected_type} but has type {actual_type}".format(field=self.field, | |
expected_type=self.expected_type.__name__, | |
actual_type=self.actual_type.__name__) | |
class MissingFields(Exception): | |
def __init__(self, missing_fields): | |
self.missing_fields = missing_fields | |
def __str__(self): | |
return "Fields {{{fields}}} are missing".format(fields=', '.join(self.missing_fields)) | |
class MissingTypes(Exception): | |
def __init__(self, missing_types): | |
self.missing_types = missing_types | |
def __str__(self): | |
return "Type descriptions for types {{{types}}} are missing".format |
This file contains hidden or 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 model import is_valid, BadTypeException, MissingFields, MissingTypes | |
from nose.tools import assert_equals, assert_raises | |
def test_is_valid(): | |
m = dict(x= int, y= int, name=str) | |
i = dict(x=12, y=13, name='foo') | |
is_valid(m, i) | |
def test_bad_type(): | |
m = dict(x=int, y=int, name=str) | |
i = dict(x=12, y='bar', name='foo') | |
with assert_raises(BadTypeException) as context: | |
is_valid(m, i) | |
assert_equals(str(context.exception), | |
"y should have type int but has type str") | |
def test_missing_type(): | |
m = dict(x= int, name=str) | |
i = dict(x=12, y='bar', name='foo') | |
with assert_raises(MissingTypes) as context: | |
is_valid(m, i) | |
assert_equals(str(context.exception), | |
"Type descriptions for types {y} are missing") | |
def test_missing_field(): | |
m = dict(x= int, name=str) | |
i = dict(x=12) | |
with assert_raises(MissingFields) as context: | |
is_valid(m, i) | |
assert_equals(str(context.exception), | |
"Fields {name} are missing") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment