Package: schema-checker
Schema checkerAnother schema validator :)Features:Can validate any dict object (not only json)Very customizebleTutorialBase validation function:defvalidate(obj:dict,schema:dict)->dict:...whereobj - some object
schema - schema_checker
schema ::= type of this object : list/dict/str/int/float (can be tuple of types) or "const"/"enum"
  OR
schema ::= dict - {
  type         : type of this object : "list/tuple/dict/str/int/float or "const"
  "value"      : need for obj type of
                   - list/tuple - is schema for all elements in list
                   - dict - dict[key -> schema]
                   - const - some value to be compared with using method
                   - enum - list/set/dict/tuple to check if obj __contains__ in "value"
  "any_key"     : need for obj type of dict - schema for all keys (ignores if value is set)
  "default"    : default value if this object does not exists (if callable will be called)
  "filter"     : any of
                   - Callable[value -> bool] - if false then raise error
                   - Iterable[Callable[value -> bool]] - if any of them return false then raise error
  "pre_call"   : any of
                   - Callable[value -> value] - will be called before checking type and call filter's functions
                   - Iterable[Callable[value -> value]] - will call all of them
  "post_call"  : any of
                   - Callable[value -> value] - will be called after checking type and call filter's functions
                   - Iterable[Callable[value -> value]] - will call all of them
  "blank"      : raise error if value is blank
  "max_length" : extra check of length (len)
  "min_length" : extra check of length (len)
  "unexpected" : allow unexpected keys (for dict)
  "errmsg"     : will be in ValueError in case of error on this level
}Extrasdecorator_constructordef decorator_constructor(getter, setter)gettermust:take same args as the function that'll be decoratedreturn dict for the schema validatorsettermust:take 3 args: validated dict, source positional args as tuple, sourse keyword args as dictreturn tuple and dict for positional and keywords args for the function tha'll be decoratedreturns parameterized decorator, that expects schemakw_validatordef kw_validator(schema)Validate only keyword args and ignores all positional
This decorator is the result of decorator_constructorpos_validatordef pos_validator(schema)Validate only positional args and ignores all keywords
This decorator is the result of decorator_constructorargs_validatordef args_validator(pos_schema: Dict[str, Any], kw_schema: Dict[str, Any]):Validate both positional and keywords argsExamplesfromdatetimeimportdatetime,timedeltafromschema_checkerimportvalidate,kw_validator# validate that obj is strvalidate(obj='12345',schema={'type':str},)# result: '12345'validate(obj=12345,schema={'type':str},)# raise ValueError# check if value is int and less then 5validate(obj={'some_key':10},schema={'type':dict,'value':{'some_key':{'type':int,'filter':lambdax:x<5,},}},)# raise ValueError# convert obj to datetime and compare with today's datevalidate(obj='10.12.19',schema={'type':datetime,'pre_call':lambdax:datetime.strptime(x,'%d.%m.%y'),'filter':lambdax:(datetime.today()-timedelta(year=1))<=x<=datetime.today(),},)# result: datetime.datetime(2019, 12, 10, 0, 0)@kw_validator({'type':dict,'values':{'a':str}})deffunc(a):returnafunc(123)# okfunc('123')# okfunc(a='123')# okfunc(a=123)# raise ValueError