from pyschema import Schema from six import print_ as print_out s = Schema({ 'type': 'string', 'display_name': 'Allow None Test', 'allow_none': True }) print_out("NULL ALLOWED") print_out("------------") print_out("Should pass: "******"\n".join(s.validate(None))) print_out("Should pass: "******"\n".join(s.validate('String'))) print_out("Should fail: " + "\n".join(s.validate(10))) print_out("NULL NOT ALLOWED") print_out("----------------") from pyschema import Schema s = Schema({ 'type': 'string', 'display_name': 'Allow None Test', }) print_out("Should fail: " + "\n".join(s.validate(None))) print_out("Should pass: "******"\n".join(s.validate('String'))) print_out("Should fail: " + "\n".join(s.validate(10)))
from pyschema import Schema from six import print_ as print_out s = { 'type': 'string', 'display_name': 'Drama' } print_out(s) _s = Schema(s) _s.validate('Drama') print_out(s)
from pyschema import Schema from six import print_ as print_out s = Schema({ 'known_children': { 'a': { 'type': 'number', 'display_name': 'A NUM' }, 'b': { 'type': 'string', 'display_name': 'B STRING' } }, 'display_name': 'ANY MAP', 'value_schema': { 'type': 'any', 'display_name': 'ANY VALUE' }, 'allow_unknown_children': True }) print_out("Invalid B " + "\n".join(s.validate({'a': 10, 'b': 10}))) print_out("Valid" + "\n".join(s.validate({'a': 10, 'b': 'string'}))) print_out("Valid Any " + "\n".join(s.validate({'a': 10, 'z': 10}))) print_out("Valid Any " + "\n".join(s.validate({'x': 10, 'z': 10})))
from pyschema import Schema from six import print_ as print_out s = Schema({ 'display_name': 'Names', 'known_children': { 'a': None, 'b': None, }, 'value_schema': { 'type': 'string', 'display_name':'A or B' }, 'allow_unknown_children': False }) print_out('Must Pass: '******'\n'.join(s.validate({'a': 'Something'}))) print_out('Must FAIL: '+'\n'.join(s.validate({'z':'ERROR'}))) print_out(s.document())
'display_name':'Names'} }, 'number_of_names': { 'display_name': 'Name Count Limit', 'type': 'number', 'minimum_value': 2 }, 'notify_via_mail': { 'display_name': 'Mail Preference', 'type': 'boolean' } }, 'mandatory_children': ['listed_names'] }) print_out("Realized schema\n---------------") pprint(s.realize()) print_out("\n\n") print_out("Sample Validation Errors: 1\n---------------") pprint(s.validate({ 'listed_names': 10 })) print_out("\n\n") print_out("Sample Validation Errors: 2\n---------------") pprint(s.validate({ 'listed_names': [10, 'invalid-name'] })) print_out("\n\n")
ret_value = {} for k, v in iteritems(name_map): ret_value[k] = { 'display_name': k, 'type': 'list', 'value_schema': { 'display_name': 'Pick a ' + k, 'type': 'string', 'allowed_values': functools.partial(list_from_file, v) } } return ret_value schema = Schema({ 'type': 'map', 'display_name': 'Complex Test', 'known_children': name_list }) pprint(schema.realize()) print_out("\n\n") print_out("Expecting a type error\n---------------") pprint(schema.validate({'names': [], 'places': 'Lakkavaram'})) print_out("\n\n") print_out("No Errors\n----------------") pprint(schema.validate({'names': ['john'], 'places': ['Lakkavaram']})) print_out("\n\n")
from pyschema import Schema from six import print_ as print_out s = Schema({ 'display_name': 'Names', 'known_children': { 'a': None, 'b': None, }, 'value_schema': { 'type': 'string', 'display_name': 'A or B' }, 'allow_unknown_children': False }) print_out('Must Pass: '******'\n'.join(s.validate({'a': 'Something'}))) print_out('Must FAIL: ' + '\n'.join(s.validate({'z': 'ERROR'}))) print_out(s.document())
def name_list(): ret_value = {} for k, v in iteritems(name_map): ret_value[k] = { 'display_name': k, 'type': 'list', 'value_schema': { 'display_name': 'Pick a '+k, 'type': 'string', 'allowed_values': functools.partial(list_from_file, v) } } return ret_value schema = Schema({ 'type': 'map', 'display_name': 'Complex Test', 'known_children': name_list }) pprint(schema.realize()) print_out("\n\n") print_out("Expecting a type error\n---------------") pprint(schema.validate({'names':[], 'places':'Lakkavaram'})) print_out("\n\n") print_out("No Errors\n----------------") pprint(schema.validate({'names':['john'], 'places':['Lakkavaram']})) print_out("\n\n")
from pyschema import Schema from six import print_ as print_out def even_only(a): return ["size must be even"] if len(a) % 2 else [] s = Schema({ 'type': 'string', 'display_name': 'EvenStevens', 'custom_validation': even_only }) print_out("EVEN STEVENS") print_out("------------") print_out("Should pass: "******"\n".join(s.validate('String'))) print_out("Should fail: " + "\n".join(s.validate('basestr'))) s = Schema({ 'type': 'list', 'display_name': 'EvenStevens List', 'custom_validation': even_only, 'value_schema': { 'type': 'string', 'display_name': 'One of the stevens' } }) print_out("EVEN STEVENS ENLISTED") print_out("---------------------")
from pyschema import Schema from six import print_ as print_out def even_only(a): return ["size must be even"] if len(a) % 2 else [ ] s = Schema({ 'type': 'string', 'display_name': 'EvenStevens', 'custom_validation': even_only }) print_out("EVEN STEVENS") print_out("------------") print_out("Should pass: "******"\n".join(s.validate('String'))) print_out("Should fail: "+"\n".join(s.validate('basestr'))) s = Schema({ 'type': 'list', 'display_name': 'EvenStevens List', 'custom_validation': even_only, 'value_schema': { 'type': 'string', 'display_name': 'One of the stevens' } }) print_out("EVEN STEVENS ENLISTED") print_out("---------------------") print_out("Should pass: "******"\n".join(s.validate(['String', 'basestr']))) print_out("Should fail: "+"\n".join(s.validate(('basestr',))))
from pprint import pprint from six import print_ as print_out s = Schema({ 'type': 'list', 'value_schema': { 'type': 'number', 'display_name': 'Num' }, 'display_name': 'The List', 'unique': True, 'minimum_size': 2, 'maximum_size': 5 }) print_out("VALID: " + "".join(s.validate([5, 6]))) print_out("INVALID Few Entries: " + "".join(s.validate([6]))) print_out("INVALID Too Many: " + "".join(s.validate([5, 6, 7, 8, 9, 1, 2, 3, 4]))) print_out("INVALID Duplicate: " + "".join(s.validate([5, 6, 5, 6]))) s1 = Schema({ 'type': 'list', 'value_schema': { 'type': 'number', 'display_name': 'Num' }, 'display_name': 'The List', 'unique': False, 'minimum_size': 2, 'maximum_size': 5
from pyschema import Schema from pprint import pprint from six import print_ as print_out s = Schema({ 'type': 'list', 'value_schema': {'type': 'number', 'display_name': 'Num'}, 'display_name': 'The List', 'unique': True, 'minimum_size': 2, 'maximum_size': 5 }) print_out("VALID: "+"".join(s.validate([5,6]))) print_out("INVALID Few Entries: "+"".join(s.validate([6]))) print_out("INVALID Too Many: "+"".join(s.validate([5,6,7,8,9,1,2,3,4]))) print_out("INVALID Duplicate: "+"".join(s.validate([5,6,5,6]))) s1 = Schema({ 'type': 'list', 'value_schema': {'type': 'number', 'display_name': 'Num'}, 'display_name': 'The List', 'unique': False, 'minimum_size': 2, 'maximum_size': 5 }) print_out("VALID Duplicate: "+"".join(s1.validate([5,6,5,6]))) pprint(s1.realize())
from pyschema import Schema from six import print_ as print_out s = {'type': 'string', 'display_name': 'Drama'} print_out(s) _s = Schema(s) _s.validate('Drama') print_out(s)