示例#1
0
def test_konval():
	test_schema = {
		u'name': IsName(),
		u'email': IsEmailAddress(),
		u'age': konval.types.IsType(int)
	}

	success_data = {
		u'name': u'Peter M. Elias',
		u'email': u'*****@*****.**',
		u'age': 37
	}

	result = konval.validate(test_schema, success_data)
	assert_true(result.is_valid())

	fail_data = {
		u'name': 123,
		u'email': 1243,
		u'age': 'fourteen'
	}

	result = konval.validate(test_schema, fail_data)
	assert_false(result.is_valid())
	assert_is_not_none(result.get_errors())
示例#2
0
def test_konval():
	test_schema = konval.Schema()
	test_schema.add_field('name', konval.IsName())
	test_schema.add_field('email', konval.web.IsEmailAddress(), default='pumpkin')
	test_schema.add_field('age', konval.base.types.IsType(int), required=True)

	success_data = {
		'name': 'Peter M. Elias',
		'email': '*****@*****.**',
		'age': 37
	}

	result = konval.validate(test_schema, success_data)
	assert_true(result.is_valid())

	random_object = type('random', (), {})
	result.write(random_object)

	assert_equal(random_object.name, u'Peter M. Elias')
	assert_equal(random_object.email, u'*****@*****.**')
	assert_equal(random_object.age, 37)

	fail_data = {
		'name': 123,
		'email': 1243,
		'age': 'fourteen'
	}

	result = konval.validate(test_schema, fail_data)
	assert_false(result.is_valid())
	assert_is_not_none(result.get_errors())

	missing_data = {
		'name': 'Peter M. Elias'
	}

	result = konval.validate(test_schema, missing_data)
	assert_false(result.is_valid())
	assert_equal(result.get_valid()['email'], 'pumpkin')
示例#3
0
文件: test.py 项目: agapow/py-konval
import konval
from konval.meta.standard import IsName

schema = {u'name': IsName()}

result = konval.validate(schema, {u'name': 'Peter M. Elias'})

print result.is_valid() # True

result = konval.validate(schema, {u'name': 'Not a real name...4342398!!*#'})

print result.is_valid() # False
print result.get_errors()