Package: b3j0f.schema
DescriptionPython schema library agnostic from languages.LinksHomepagePyPIDocumentationInstallationpip install b3j0f.schemaFeaturesThis library provides an abstraction layer for manipulating schema from several languages.The abstraction layer is a python object which can validate data (properties to validate are object attributes or dictionary items) and be dumped into a dictionary or specific language format.Supported languages are:pythonjsonxsdIt is also possible to generate a schema from a dictionary. And validation rules are fully and easily customisable thanks to using a schema such as a property.ExampleData Validationfromb3j0f.schemaimportbuild,validate# json format with required subinteger propertyresource='{"title": "test", "properties": {"subname": {"type": "string", "default": "test"}}, {"subinteger": {"type": "integer"}}, "required": ["subinteger"]}'Test=build(resource)test=Test(subname='example')asserttest.subinteger==0# instanciation valueassertTest.subinteger.default==0# default valueasserttest.subname=='example'# instanciation valueassertTest.subname.default=='test'# instanciation valueerror=Nonetry:test.subname=2# wrong setting because subname is not a stringexceptTypeErroraserror:passasserterrorisnotNoneassert'subname'inTest.getschemas()validate(Test.subinteger,1)# validate propertyvalidate(test,{'subinteger':1})# validate dictionaryclassSub(object):# object to validate with required subintegersubinteger=1validate(test,Sub)# validate an object with required subintegervalidate(test,Sub())wrongvalues=['',# object without subinteger{'subinteger':''},# wrong data type for subinteger{}# dictionary without the required property subinteger]forwrongvalueinwrongvalues:error=Nonetry:validate(test,wrongvalues)exceptTypeErroraserror:passasserterrorisnotNoneSchema retrievingfromb3j0f.schemaimportregister,getbyname,getbyuuid,data2schemaassertgetbyuuid(test.uuid)isNoneasserttestnotingetbyname(test.name)register(test)asserttestisgetbyuuid(test.uuid)asserttestingetbyname(test.name)schema=data2schema(2,name='vint')# get an integer schema with 2 such as a default value and name vintassertschema.default==2assertschema.name=='vint'error=Nonetry:schema.default=''exceptTypeErroraserror:passasserterrorisnotNoneSchema definitionfromb3j0f.schemaimportSchema,updatecontent@updatecontent# change public attributes/functionss to schemasclassTest(Schema):subname='test'# specify inner schema such as a string schema with default value 'test'subinteger=1# speciy inner schema sub as an integer with default value 1test=Test()test=Test(subname='example')asserttest.subname=='example'# instanciation valueassertTest.subname.default=='test'# instanciation valueasserttest.subinteger==1# instanciation valueassertTest.subinteger.default==1# default valueerror=Nonetry:test.subname=2# wrong setting because subname is not a stringexceptTypeErroraserror:passasserterrorisnotNoneassert'subname'inTest.getschemas()Complex Schema definitionfromb3j0f.schemaimportSchema,ThisSchema,RefSchema,buildfromrandomimportrandom@build(foo=2)# transform a python class to a schema class with the additional property fooclassTest(object):key=DynamicValue(lambda:random())# generate a new key at each instanciationsubtest=ThisSchema(key=3.)# use this schema such as inner schemaref=RefSchema()# ref is validated by this schemaassertissubclass(Test,Schema)test1,test2=Test(),Test()# check fooasserttest1.foo==test2.foo==2# check key and subtest propertiesasserttest1.key!=test2.keyasserttest1.subtest.key==test2.subtest.key==3.# check refasserttest1.refisNonetest1.ref=Test()error=Nonetry:test.ref=2exceptTypeErroraserror:passasserterrorisnotNoneFunction schema definitionfromb3j0f.schemaimportFunctionSchema,ParamSchema,FloatSchema,BooleanSchema,StringSchema,ArraySchema,OneOfSchema@data2schemadeftest(a,b,c=2,d=None,e=None,f=None):# definition of a shema function. Parameter values and (function) types are defined in the signature and the docstring."""
   :param float a: default 0.
   :type b: bool
   :type d: ints  # list of int
   :type e: list of str  #: list of str
   :type f: int,float  #: one of (int, float)
   :rtype: str
   """returna,b,cassertisinstance(test,FunctionSchema)assertisinstance(test.params,ArraySchema)assertisinstance(test.params[0],ParamSchema)assertlen(test.params)==6asserttest.params[0].name=='a'asserttest.params[0].mandatory==Trueassertisinstance(test.params[0].ref,FloatSchema)asserttest.params[0].defaultis0.asserttest.params[1].name=='b'assertisinstance(test.params[1].ref,BooleanSchema)asserttest.params[1].mandatoryisTrueasserttest.params[1].defaultisFalseasserttest.params[2].name=='c'assertisinstance(test.params[2].ref,IntegerSchema)asserttest.params[2].mandatoryisFalseasserttest.params[2].defaultis2asserttest.params[3].name=='d'assertisinstance(test.params[3].ref,ArraySchema)assertisinstance(test.params[3].ref.itemtype,IntegerSchema)asserttest.params[3].mandatoryisFalseasserttest.params[3].defaultisNoneasserttest.params[4].name=='e'assertisinstance(test.params[4].ref,ArraySchema)assertisinstance(test.params[4].ref.itemtype,StringSchema)asserttest.params[4].mandatoryisFalseasserttest.params[4].defaultisNoneasserttest.params[5].name=='f'assertisinstance(test.params[5].ref,OneOfSchema)assertisinstance(test.params[5].ref.schemas[0],IntegerSchema)assertisinstance(test.params[5].ref.schemas[1],FloatSchema)asserttest.params[5].mandatoryisFalseasserttest.params[5].defaultisNoneasserttest.rtypeisStringSchemaasserttest(1,2)=='test'Generate a schema from a datafromb3j0f.schemaimportdata2schemadata={# data is a dict'a':1}schemacls=dict2schemacls(data,name='test')assertisinstance(schemacls.a,IntegerSchema)assertschemacls.a.defaultis1assertisinstance(schemacls.name,StringSchema)assertschemacls.name.default=='test'validate(schemacls(),data)classTest(object):# data is an objecta=1schemacls=dict2schemacls(data,name='test')assertisinstance(schemacls.a,IntegerSchema)assertschemacls.a.defaultis1assertisinstance(schemacls.name,StringSchema)assertschemacls.name.default=='test'validate(schemacls(),Test)validate(schemacls(),Test())Schema property getting/setting/deleting customisation such as a propertyclassTest(Schema):@Schemadeftest(self):self.op='get'returngetattr(self,'_test',1)@test.setterdeftest(self,value):self.op='set'self._test=value@test.deleterdeftest(self):self.op='del'delself._testtest=Test()# check getterasserttest.test==1asserttest.op=='get'# check settertest.test=2asserttest.op=='set'asserttest.test==2# check deleterdeltest.testasserttest.op=='del'asserttest.test==1Perspectiveswait feedbacks during 6 months before passing it to a stable version.Cython implementation.Donation