Package: schemalite
A minimal, yet powerful schema validation library for pythonFree software: MIT licenseDocumentation:https://schemalite.readthedocs.io.FeaturesBecause I started writing it before I came across Cerberus, Marshmallow and
Schema.While the other schema validation libraries have powerful DSLs, they also have
too big an API for simple needs. This library has only one concept I need to
keep in mind - A validator is a function that will return a tuple like (False,
“Some error message”) or (True, None). Thats all.A schema is adictwith 2 keys - “fields” and “validators”validatorsis a list of validator functions to apply on the input data as a
whole instead of at field level. It should again be a function which returns a
tuple as output, while accepting a single dictionary as input ( corresponding to
the whole input data )fields- is a dict with keys corresponding to the names of the keys of the
dictionary which is being validated. Each field is in turn a dict, which has one
or more of the following optional keysrequired- True/False. Alternatively it can also be a function which
accepts the input dictionary and outputs True/False as output. If not
specified, field is assumed to be not required.type- The type of the data the field is expecting. It can be any valid
pythonic type - int / str / unicode / date / datetime / Decimal / list / dict
( or anything else which is a pythontype). It can also be a list of types
in which case the data should be of any one of those types.validators- A list of validator functions. The function should accept 2
arguments - The value of the particular key being processed and the whole
dictionary itself (in case the validator needs access to the whole data
instead of that field alone to decide whether the value is valid). It has to
return a tuple. Either (True, None) or (False, “some error message”) ( The
error need not be a string. It can be any valid json serializable data
structure - a list or dict also)permitted_values- A list of permitted values for the field.Iftypeis list, you can send the following fields alsoi.list_item_type- Tells the type of each item in the list. It can also
be any Python type or a list of types. ii.list_item_schema- Iflist_item_typeis dict, then you can optionally providelist_item_schemaalso - to validate each dict in the list against another schemaIftypeis dict, then you can send the following fielddict_schema- The
schema to validate the dict against.At both field and schema level, all validators will be applied one after another
and their errors will be collected together in the output.To apply the validator, you can callvalidate_dict(dictionary, schema)( orvalidate_list_of_dicts(list_of_dicts, dict_schema))The output itself will be tuple of the same format as what we defined above for
validators.Example:Lets define 2 schemasperson_schema={"fields":{"name":{"required":True,"type":(str,unicode)},"gender":{"required":True,"type":(str,unicode),"permitted_values":("Male","Female")},"age":{"required":func_and_desc(lambdaperson:person['gender']=='Female',"Required if gender is female"),"type":int,"validators":[func_and_desc(lambdaage,person:(False,"Too old")ifage>40else(True,None),"Has to be less than 40")]},"access_levels":{"type":list,"list_item_type":int,"permitted_values_for_list_items":range(1,10)}},}org_schema={"fields":{"name":{"required":True,"type":(str,unicode)},"ceo":{"required":True,"type":dict,"dict_schema":person_schema},"members":{"required":True,"type":list,"list_item_type":dict,"list_item_schema":person_schema}},"validators":[func_and_desc(lambdaorg:(False,"Non member cannot be CEO")iforg["ceo"]notinorg["members"]else(True,None),"Non member cannot be CEO")],"allow_unknown_fields":True}And some data to validate against the schemaisaac={"gender":"Male","name":"Isaac","age":"new","access_levels":[1,4,60]}surya={"gender":"Male","name":"Surya","age":"h","city":"Chennai"}senthil={"gender":"Male","name":"Senthil"}mrx={"gender":"m","name":"x"}sharanya={"gender":"Female","name":"Sharanya","access_levels":[4,5,60]}Lets first validate some personsvalidate_dict(mrx,person_schema)Output is(False,{'FIELD_LEVEL_ERRORS':{'gender':{'PERMITTED_VALUES_ERROR':'Field data can be one of the following only: Male/Female'}}})Another personvalidate_dict(surya,person_schema)Output(False,{'FIELD_LEVEL_ERRORS':{'age':{'HAS_TO_BE_LESS_THAN_40':'Too old','TYPE_ERROR':'Field data should be of type int'}},'UNKNOWN_FIELDS':['city']})Now validating the same person, but allowing unknown fieldsvalidate_dict(surya,person_schema,allow_unknown_fields=True)Output(False,{'FIELD_LEVEL_ERRORS':{'age':{'HAS_TO_BE_LESS_THAN_40':'Too old','TYPE_ERROR':'Field data should be of type int'}}})Finally lets create an organization and validate itinkmonk={"name":"Inkmonk","ceo":isaac,"members":[surya,senthil,sharanya],"city":"Chennai"}validate_dict(inkmonk,org_schema)Output(False,{'FIELD_LEVEL_ERRORS':{'ceo':{'VALIDATION_ERRORS_FOR_OBJECT':{'FIELD_LEVEL_ERRORS':{'access_levels':{'VALIDATION_ERRORS_FOR_OBJECTS_IN_LIST':[None,None,{'PERMITTED_VALUES_ERROR':'Fielddatacanbeoneofthefollowingonly:1/2/3/4/5/6/7/8/9'}]},'age':{'HAS_TO_BE_LESS_THAN_40':'Tooold','TYPE_ERROR':'Fielddatashouldbeoftypeint'}}}},'members':{'VALIDATION_ERRORS_FOR_OBJECTS_IN_LIST':[{'FIELD_LEVEL_ERRORS':{'age':{'HAS_TO_BE_LESS_THAN_40':'Tooold','TYPE_ERROR':'Fielddatashouldbeoftypeint'}},'UNKNOWN_FIELDS':['city']},None,{'FIELD_LEVEL_ERRORS':{'access_levels':{'VALIDATION_ERRORS_FOR_OBJECTS_IN_LIST':[None,None,{'PERMITTED_VALUES_ERROR':'Fielddatacanbeoneofthefollowingonly:1/2/3/4/5/6/7/8/9'}]},'age':{'MISSING_FIELD_ERROR':'Requiredifgenderisfemale'}},'MISSING_FIELDS':['age']}]}},'SCHEMA_LEVEL_ERRORS':['NonmembercannotbeCEO'],'UNKNOWN_FIELDS':['city']})###Understanding the errors outputThe library is structured to provide an error output to any nested level of
granularity.At the outer most level, there are the following keys“FIELD_LEVEL_ERRORS” - Contains the errors mapped to each field“SCHEMA_LEVEL_ERRORS” - A list of errors found for the schema as a whole“UNKNOWN_FIELDS” - If the validation is configured to not allow unknown fields
and if the data had any, they will be listed here“MISSING_FIELDS” - List of all missing required fields.Inside ‘FIELD_LEVEL_ERRORS’, each field will have a dict of errors mapped to it.
The keys of the dict are the names of the errors and values are the error
strings. Example for an error dict for a field would be{‘TYPE_ERROR’: “This field should have type int only”}or{“PERMITTED_VALUES_ERROR”: “The object should have value high/low only”}If a particular field is of typedict, and ifdict_schemais defined, then
you can also expect to see a key namedVALIDATION_ERRORS_FOR_OBJECTinsideerrors[‘FIELD_LEVEL_ERRORS’][‘particular_field_name’]. In that caseerrors[‘FIELD_LEVEL_ERRORS’][‘particular_field_name’][‘VALIDATION_ERRORS_FOR_OBJECT’]will contain another errors object obtained by matching the data in this field
alone against another schema ( So that errors object will in turn have
FIELD_LEVEL_ERRORS, SCHEMA_LEVEL_ERRORS etc)If a particular field is of typelistand iflist_typeis defined, then if
there are validation errors for the objects in the list, you can expect to seeerrors[‘FIELD_LEVEL_ERRORS’][‘particular_field_name’][‘VALIDATION_ERRORS_FOR_OBJECTS_IN_LIST’].
This will be a list of error objects. If the field is a list of primitive types,
then you can expect each error object to have fields likeTYPE_ERRORorPERMITTED_VALUES_ERROR. If it is a list of objects of another schema ( defined
bylist_item_schema), then each item in the errors list would be an error
object got by validating against that schema - so it will haveFIELD_LEVEL_ERRORS,SCHEMA_LEVEL_ERRORSetc. ( While iterating, if one item
has no error, then instead of error object, it will have a null in the errors
list at that index.)CreditsThis package was created withCookiecutterand theaudreyr/cookiecutter-pypackageproject template.