Package: schemable
Schemable is a schema parsing and validation library that let’s you define schemas simply using dictionaries, lists, types, and callables.LinksProject:https://github.com/dgilland/schemableDocumentation:https://schemable.readthedocs.ioPyPI:https://pypi.python.org/pypi/schemable/TravisCI:https://travis-ci.org/dgilland/schemableFeaturesSimple schema definitions usingdict,list, andtypeobjectsComplex schema definitions usingAny,All,As, and predicatesDetailed validation error messagesPartial data loading on validation failureStrict and non-strict parsing modesPython 3.4+QuickstartInstall using pip:pip install schemableDefine a schema usingdictandlistobjects:fromschemableimportSchema,All,Any,As,Optional,SchemaErroruser_schema=Schema({'name':str,'email':All(str,lambdaemail:len(email)>3and'@'inemail),'active':bool,'settings':{Optional('theme'):str,Optional('language',default='en'):str,Optional('volume'):int,str:str},'aliases':[str],'phone':All(str,As(lambdaphone:''.join(filter(str.isdigit,phone))),lambdaphone:10<=len(phone)<=15),'addresses':[{'street_addr1':str,Optional('street_addr2',default=None):Any(str,None),'city':str,'state':str,'country':str,'zip_code':str}]})Then validate and load by passing data touser_schema():# Fail!result=user_schema({'name':'Bob Smith','email':'bob.example.com','active':1,'settings':{'theme':False,'extra_setting1':'val1','extra_setting2':True},'phone':1234567890,'addresses':[{'street_addr1':'123 Lane','city':'City','state':'ST','country':'US','zip_code':11000}]})print(result)# SchemaResult(#     data={'name': 'Bob Smith',#           'settings': {'extra_setting1': 'val1',#                        'language': 'en'}#           'addresses': [{'street_addr1': '123 Lane',#                          'city': 'City',#                          'state': 'ST',#                          'country': 'US',#                          'street_addr2': None}]},#     errors={'email': "bad value: <lambda>('bob.example.com') should evaluate to True",#             'active': 'bad value: type error, expected bool but found int',#             'settings': {'theme': 'bad value: type error, expected str but found bool',#                          'extra_setting2': 'bad value: type error, expected str but found bool'},#             'phone': 'bad value: type error, expected str but found int',#             'addresses': {0: {'zip_code': 'bad value: type error, expected str but found int'}},#             'aliases': 'missing required key'})# Fail!result=user_schema({'name':'Bob Smith','email':'bob@example.com','active':True,'settings':{'theme':False,'extra_setting1':'val1','extra_setting2':'val2'},'phone':'123-456-789','addresses':[{'street_addr1':'123 Lane','city':'City','state':'ST','country':'US','zip_code':'11000'}]})print(result)# SchemaResult(#     data={'name': 'Bob Smith',#           'email': 'bob@example.com',#           'active': True,#           'settings': {'extra_setting1': 'val1',#                        'extra_setting2': 'val2',#                        'language': 'en'},#           'addresses': [{'street_addr1': '123 Lane',#                          'city': 'City',#                          'state': 'ST',#                          'country': 'US',#                          'zip_code': '11000',#                          'street_addr2': None}]},#     errors={'settings': {'theme': 'bad value: type error, expected str but found bool'},#             'phone': "bad value: <lambda>('123456789') should evaluate to True",#             'aliases': 'missing required key'})Or can raise an exception on validation failure instead of returning results:# Fail strictly!try:user_schema({'name':'Bob Smith','email':'bob@example.com','active':True,'settings':{'theme':False,'extra_setting1':'val1','extra_setting2':'val2'},'phone':'123-456-789','addresses':[{'street_addr1':'123 Lane','city':'City','state':'ST','country':'US','zip_code':'11000'}]},strict=True)exceptSchemaErrorasexc:print(exc)# Schema validation failed: \# {'settings': {'theme': 'bad value: type error, expected str but found bool'}, \# 'phone': "bad value: <lambda>('123456789') should evaluate to True", \# 'aliases': 'missing required key'}Access the parsed data after successful validation:# Pass!result=user_schema({'name':'Bob Smith','email':'bob@example.com','active':True,'settings':{'theme':'dark','extra_setting1':'val1','extra_setting2':'val2'},'phone':'123-456-7890','aliases':[],'addresses':[{'street_addr1':'123 Lane','city':'City','state':'ST','country':'US','zip_code':'11000'}]})print(result)# SchemaResult(#     data={'name': 'Bob Smith',#           'email': 'bob@example.com',#           'active': True,#           'settings': {'theme': 'dark',#                        'extra_setting1': 'val1',#                        'extra_setting2': 'val2',#                        'language': 'en'},#           'phone': '1234567890',#           'aliases': [],#           'addresses': [{'street_addr1': '123 Lane',#                          'city': 'City',#                          'state': 'ST',#                          'country': 'US',#                          'zip_code': '11000',#                          'street_addr2': None}]},#     errors={})For more details, please see the full documentation athttps://schemable.readthedocs.io.Changelogv0.5.0 (2018-08-17)Don’t load partial data from a nested schema if it was created withstrict=True(e.g.Schema({'key':Schema({...},strict=True)})).v0.4.1 (2018-08-14)Fix previous fix for case where schema results could havedataorerrorswith schema classes as keys.Ensure thatSelect('key',<iteratee>)doesn’t call<iteratee>if'key'was not found in the source data.v0.4.0 (2018-08-14)Fix case where schema object with anOptional(key)would result inSchemaResult.errors[Optional(key)]. Ensure thatSchemaResult.errors[key]is set instead.IgnoreKeyErrorwhen usingSchema({'key':Select('other_key')})when'other_key'isn’t present in the source object. Return a missing key error instead.v0.3.1 (2018-07-31)If a validate callable raises an exception, use its string representation as the schema error message. Previously, a custom error message stating that the callable should evaluate to true was used when validator returned falsey and when it raised an exception. That message is now only returned when the validator doesn’t raise but returns falsey.v0.3.0 (2018-07-27)Add schema helpers:SelectUseInclude execption class name in error message returned byAs.Always return adictwhen parsing from dictionary schemas instead of trying to use the source data’s type as an initializer. (breaking change)v0.2.0 (2018-07-25)RenameCollectiontoList. (breaking change)RenameObjecttoDict. (breaking change)Allowcollections.abc.Mappingobjects to be validDictobjects.ModifyTypevalidation so that objects are only compared withisinstance.Improve docs.v0.1.0 (2018-07-24)First release.LicenseThe MIT License (MIT)Copyright (c) 2018, Derrick GillandPermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.