Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

schema

from schema import Schema, And, Use, Optional, SchemaError

'''
pip install schema required
name required must be a string and strip leading and trailing whitespaces
age required must be a int and greater than or equal to 18 and less than or equal to 99
gender is optional and must be string and with the choice of male or female converting the gender to lowercase
'''
schema = Schema([{'name': And(str, str.strip),
                  'age':  And(Use(int), lambda n: 18 <= n <= 99),
                  Optional('gender'): And(str, Use(str.lower), lambda s: s in ('male', 'female'))}])

data = [{'name': 'Codecaine', 'age': '21', 'gender': 'Female'},
        {'name': 'Sam', 'age': '42'},
        {'name': 'Sacha', 'age': '20', 'gender': 'male'}]

try:
    validated = schema.validate(data)
    print(validated)
except SchemaError as err:
    print(err)
Source by github.com #
 
PREVIOUS NEXT
Tagged: #schema
ADD COMMENT
Topic
Name
9+1 =