Python schema library agnostic from languages.
Project description
Description
Python schema library agnostic from languages.
Links
Installation
pip install b3j0f.schema
Features
This 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:
python
json
xsd
It 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.
Example
Data Validation
from b3j0f.schema import build, validate
# json format with required subinteger property
resource = '{"title": "test", "properties": {"subname": {"type": "string", "default": "test"}}, {"subinteger": {"type": "integer"}}, "required": ["subinteger"]}'
Test = build(resource)
test = Test(subname='example')
assert test.subinteger == 0 # instanciation value
assert Test.subinteger.default == 0 # default value
assert test.subname == 'example' # instanciation value
assert Test.subname.default == 'test' # instanciation value
error = None
try:
test.subname = 2 # wrong setting because subname is not a string
except TypeError as error:
pass
assert error is not None
assert 'subname' in Test.getschemas()
validate(Test.subinteger, 1) # validate property
validate(test, {'subinteger': 1}) # validate dictionary
class Sub(object): # object to validate with required subinteger
subinteger = 1
validate(test, Sub) # validate an object with required subinteger
validate(test, Sub())
wrongvalues = [
'', # object without subinteger
{'subinteger': ''}, # wrong data type for subinteger
{} # dictionary without the required property subinteger
]
for wrongvalue in wrongvalues:
error = None
try:
validate(test, wrongvalues)
except TypeError as error:
pass
assert error is not None
Schema retrieving
from b3j0f.schema import register, getbyname, getbyuuid, data2schema
assert getbyuuid(test.uuid) is None
assert test not in getbyname(test.name)
register(test)
assert test is getbyuuid(test.uuid)
assert test in getbyname(test.name)
schema = data2schema(2, name='vint') # get an integer schema with 2 such as a default value and name vint
assert schema.default == 2
assert schema.name == 'vint'
error = None
try:
schema.default = ''
except TypeError as error:
pass
assert error is not None
Schema definition
from b3j0f.schema import Schema, updatecontent
@updatecontent # change public attributes/functionss to schemas
class Test(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 1
test = Test()
test = Test(subname='example')
assert test.subname == 'example' # instanciation value
assert Test.subname.default == 'test' # instanciation value
assert test.subinteger == 1 # instanciation value
assert Test.subinteger.default == 1 # default value
error = None
try:
test.subname = 2 # wrong setting because subname is not a string
except TypeError as error:
pass
assert error is not None
assert 'subname' in Test.getschemas()
Complex Schema definition
from b3j0f.schema import Schema, ThisSchema, RefSchema, build
from random import random
@build(foo=2) # transform a python class to a schema class with the additional property foo
class Test(object):
key = DynamicValue(lambda: random()) # generate a new key at each instanciation
subtest = ThisSchema(key=3.) # use this schema such as inner schema
ref = RefSchema() # ref is validated by this schema
assert issubclass(Test, Schema)
test1, test2 = Test(), Test()
# check foo
assert test1.foo == test2.foo == 2
# check key and subtest properties
assert test1.key != test2.key
assert test1.subtest.key == test2.subtest.key == 3.
# check ref
assert test1.ref is None
test1.ref = Test()
error = None
try:
test.ref = 2
except TypeError as error:
pass
assert error is not None
Function schema definition
from b3j0f.schema import FunctionSchema, ParamSchema, FloatSchema, BooleanSchema, StringSchema, ArraySchema, OneOfSchema
@data2schema
def test(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
"""
return a, b, c
assert isinstance(test, FunctionSchema)
assert isinstance(test.params, ArraySchema)
assert isinstance(test.params[0], ParamSchema)
assert len(test.params) == 6
assert test.params[0].name == 'a'
assert test.params[0].mandatory == True
assert isinstance(test.params[0].ref, FloatSchema)
assert test.params[0].default is 0.
assert test.params[1].name == 'b'
assert isinstance(test.params[1].ref, BooleanSchema)
assert test.params[1].mandatory is True
assert test.params[1].default is False
assert test.params[2].name == 'c'
assert isinstance(test.params[2].ref, IntegerSchema)
assert test.params[2].mandatory is False
assert test.params[2].default is 2
assert test.params[3].name == 'd'
assert isinstance(test.params[3].ref, ArraySchema)
assert isinstance(test.params[3].ref.itemtype, IntegerSchema)
assert test.params[3].mandatory is False
assert test.params[3].default is None
assert test.params[4].name == 'e'
assert isinstance(test.params[4].ref, ArraySchema)
assert isinstance(test.params[4].ref.itemtype, StringSchema)
assert test.params[4].mandatory is False
assert test.params[4].default is None
assert test.params[5].name == 'f'
assert isinstance(test.params[5].ref, OneOfSchema)
assert isinstance(test.params[5].ref.schemas[0], IntegerSchema)
assert isinstance(test.params[5].ref.schemas[1], FloatSchema)
assert test.params[5].mandatory is False
assert test.params[5].default is None
assert test.rtype is StringSchema
assert test(1, 2) == 'test'
Generate a schema from a data
from b3j0f.schema import data2schema
data = { # data is a dict
'a': 1
}
schemacls = dict2schemacls(data, name='test')
assert isinstance(schemacls.a, IntegerSchema)
assert schemacls.a.default is 1
assert isinstance(schemacls.name, StringSchema)
assert schemacls.name.default == 'test'
validate(schemacls(), data)
class Test(object): # data is an object
a = 1
schemacls = dict2schemacls(data, name='test')
assert isinstance(schemacls.a, IntegerSchema)
assert schemacls.a.default is 1
assert isinstance(schemacls.name, StringSchema)
assert schemacls.name.default == 'test'
validate(schemacls(), Test)
validate(schemacls(), Test())
Schema property getting/setting/deleting customisation such as a property
class Test(Schema):
@Schema
def test(self):
self.op = 'get'
return getattr(self, '_test', 1)
@test.setter
def test(self, value):
self.op = 'set'
self._test = value
@test.deleter
def test(self):
self.op = 'del'
del self._test
test = Test()
# check getter
assert test.test == 1
assert test.op == 'get'
# check setter
test.test = 2
assert test.op == 'set'
assert test.test == 2
# check deleter
del test.test
assert test.op == 'del'
assert test.test == 1
Perspectives
wait feedbacks during 6 months before passing it to a stable version.
Cython implementation.
Donation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file b3j0f.schema-0.0.9.zip
.
File metadata
- Download URL: b3j0f.schema-0.0.9.zip
- Upload date:
- Size: 54.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b135467e63b58ae1b78e08e6f7645496cda53368ad1a1ccfe0f79d2dba0653b |
|
MD5 | 3ce4211010ce426f419705310c6915cd |
|
BLAKE2b-256 | 1959fbfce4c0240d341dcd5985608b187acdef2dd1d7eb2436ecfe1c406a2490 |
File details
Details for the file b3j0f.schema-0.0.9.tar.gz
.
File metadata
- Download URL: b3j0f.schema-0.0.9.tar.gz
- Upload date:
- Size: 27.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 330dabb41b7a37283e355c4c2d6ceec3b64ff0d273b03eb8703c9527822accab |
|
MD5 | cc1e8d3b07dc7202d72358c3b72ce2f0 |
|
BLAKE2b-256 | 12bec8bd1c55d07dafdadf754045044b9a3bcb63d6b7f96a39228476453c6b93 |
File details
Details for the file b3j0f.schema-0.0.9.tar.bz2
.
File metadata
- Download URL: b3j0f.schema-0.0.9.tar.bz2
- Upload date:
- Size: 23.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a9e5259aa75461d08a6fb0ffa998c75c9b74e8a12929845afeb7d2c592df02f |
|
MD5 | 6c0e25da8a64d59679d7a444319868e7 |
|
BLAKE2b-256 | 32d0ac9d47dfccbfb29a1f1ca940b7a26546b6687bcdcbb444f6b904a3125b95 |
File details
Details for the file b3j0f.schema-0.0.9-py2.py3-none-any.whl
.
File metadata
- Download URL: b3j0f.schema-0.0.9-py2.py3-none-any.whl
- Upload date:
- Size: 52.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 070a7bf56147c195d439151442e5d5b24f247d001bb3e673db1d66146bfcbe88 |
|
MD5 | 6b2bc2d09e781a6ca4f652f9aea12a17 |
|
BLAKE2b-256 | d1805c8c733fa12173c130b5a54c2a5b12e58b8b1fb8bed4e586a5cb41bc30e3 |