Search
 
SCRIPT & CODE EXAMPLE
 

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)
Comment

PREVIOUS NEXT
Code Example
Javascript :: js brightness filter 
Javascript :: javascript add to a dictionary 
Javascript :: () = javascript 
Javascript :: javascript basic programs 
Javascript :: npm read email 
Javascript :: Javascript Scrape content from a website source code 
Javascript :: map method in javascript 
Javascript :: blur javascript 
Javascript :: printing in javascript 
Javascript :: angular number validation 
Javascript :: Filtering an array for unique values 
Javascript :: javascript self executing function 
Javascript :: map && arrow function in javascript 
Javascript :: smooth scroll jquery 
Javascript :: check if file exists javascript 
Javascript :: object.assign in node.js 
Javascript :: js code 
Javascript :: how to use break in javascript 
Javascript :: variable name as a string in Javascript function 
Javascript :: javascript promise methods 
Javascript :: is there an api for netflix shows 
Javascript :: what is console working for in js 
Javascript :: discord.js give role command 
Javascript :: pattern printing in javascript 
Javascript :: hasOwnProperty.call js 
Javascript :: variavel javascript 
Javascript :: find element and find elements 
Javascript :: top bar in react js 
Javascript :: what would (int) (Math.random()) output 
Javascript :: bounce of two circles javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =