Package: tinyschema
featuresschema definitionschema validationdata validationschema definitionThe way of schema definition is like a below sample.importtinyschemaastclassPoint(t.Schema):x=t.column(t.IntegerField)y=t.column(t.IntegerField)z=t.column(t.IntegerField,required=False)Accessing field with dot-access, like a plain python object. But a
returned object is wrapped by Field object.Field object has these members.name – name of field (system value)value – value of fieldSo this Point schema accessing a field like a below.pt = Point(x=10, y=20)

print(pt.x.name)   # => x
print(pt.x.value)  # => 10additionA column of Schema can store your favirote value. below example is
stored a value about css-class “hidden”. and adding label option
that display expression for human (display value).classPoint(t.Schema):x=t.column(t.IntegerField,label=u"x-coordinate",class_="hidden")pt=Point(x=10,y=20)print(pt.x.label)# => x-coordinateprint(pt.x.class_)# => "hidden"schema validationSchema has a behavior of schema-validation. schema-validation is format checking.filtering expected values only.checking type of value.converting value if need.params={"x":"10","y":"20","foo":"foo"}pt=Point.fromdict(params)print(pt.validate())# => OrderedDict([('x', 10), ('y', 20), ('z', None)])schema-validation is run by calling validate() method. In above code,
“foo” value is not member of Point schema, so validated value does not
include a value name of foo. And a column-z has required=False option,
because of this, a passed value that doesn’t have a value name of z,
converted value is None.when schema error is found.when schema validation is failure, then, Failure exception is raised.params={"x":"aa"}pt=Point.fromdict(params)pt.validate()# tinyschema.Failure: <Failure errors=defaultdict(<class 'list'>, {'y': ['required'], 'x': ['aa is not int']})>Adding field validationAdding field validation example is here.(using oneOf validator)classSignal(t.Schema):color=t.column(t.TextField,t.OneOf(["red","blue","yellow"]))# success versionsignal=Signal(color="red")data=signal.validate()print(data["color"])# => "red"# failure versiontry:signal2=Signal(color="green")data=signal2.validate()exceptt.Failurease:print(e)# <Failure errors=defaultdict(<class 'list'>, {'color': ['green is not in red, blue, yellow']})>default validator are below.Any, Regex, Email, Range, Length, OneOf, Subset, URLdefault type of field.IntegerField, FloatField, BooleanField, TextField, ChoicesField, PositiveIntegerFieldmore complex structuretinyschema support more complex structure like a dict-tree, sequence,
or combination of one.dict-tree(using Container)A field of schema is also schema. below example, Pair Schema has two
members, l and r. And l and r is a Point Schema.classPair(t.Schema):l=t.column(t.Container(Point),class_="left")r=t.column(t.Container(Point),class_="right")params={"l":{"x":"10","y":"20","foo":"foo"},"r":{"x":"100","y":"20"},}pair=Pair.fromdict(params)importpprintpprint.pprint(pair.validate())# {'l': OrderedDict([('x', 10), ('y', 20), ('z', None)]),#  'r': OrderedDict([('x', 100), ('y', 20), ('z', None)])}pair.l.value.x.name# => xpair.l.value.x.value# => 10sequence(using Collection)PointList is a sequence of Point.classPointList(t.Schema):points=t.column(t.Collection(Point))params={"points":[{"x":"10","y":"20"},{"x":"20","y":"20"},{"x":"30","y":"20"},]}plist=PointList.fromdict(params)importpprintpprint.pprint(plist.validate())# {'points': [OrderedDict([('x', 10), ('y', 20), ('z', None)]),#             OrderedDict([('x', 20), ('y', 20), ('z', None)]),#             OrderedDict([('x', 30), ('y', 20), ('z', None)])]}data validationdata-validation is a checking about a relation of each data.(TODO: gentle example)fromtinyschema.datavalidationimportValidationObject,multi,Invalid,single,shareclassPointValidation(ValidationObject):def__init__(self,limit):self.limit=limit@multi(["x","z"])defequals(self,x,z):ifx!=z:raiseInvalid("not equal")@share(single("x"),single("y"),single("z"))deflimit(self,value):ifvalue>self.limit:raiseInvalid("too large")validate=PointValidation(limit=100)print(validate(Point(x=10,y=20)))# => OrderedDict([('x', 10), ('y', 20), ('z', None)])print(validate(Point(x=10,y=20,z=10)))# => OrderedDict([('x', 10), ('y', 20), ('z', 10)])print(validate(Point(x=10,y=20,z=1000)))# tinyschema.Failure: <Failure errors=defaultdict(<class 'list'>, {'z': ['too large'], 'x': ['not equal']})>print(validate(Point(x="aa")))# tinyschema.Failure: <Failure errors=defaultdict(<class 'list'>, {'x': ['aa is not int'], 'y': ['required']})>