Navigation

JSL is a Python library that provides a DSL for describing JSON schemas.

Why? Well, JSON schemas, especially large ones, can be tiresome to write. The standard is not always
intuitive and leaves some places to make a mistake – for instance, it is easy to mix
maxItems keyword with maxLength, or to forget to set additionalProperties to false, and so on.
The syntax is not very concise and sometimes schema definitions get clumsy and hard to comprehend.

The DSL allows you to define a JSON schema in the way similar to how you define a model using an ORM –
using classes and fields and relying on some metaclass magic under the hood.

fromjslimportDocument,BooleanField,StringField,ArrayField,DocumentField,OneOfField,IntFieldclassDiskDevice(Document):type=StringField(enum=['disk'],required=True)device=StringField(pattern='^/dev/[^/]+(/[^/]+)*$',required=True)classDiskUUID(Document):type=StringField(enum=['disk'],required=True)label=StringField(pattern='^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$',required=True)classNFS(Document):type=StringField(enum=['nfs'],required=True)remotePath=StringField(pattern='^(/[^/]+)+$',required=True)server=OneOfField([StringField(format='ipv4'),StringField(format='ipv6'),StringField(format='host-name'),],required=True)classTmpFS(Document):type=StringField(enum=['tmpfs'],required=True)sizeInMb=IntField(minimum=16,maximum=512,required=True)classSchema(Document):classOptions(object):schema_uri='http://json-schema.org/draft-04/schema#'description='schema for an fstab entry'storage=OneOfField([DocumentField(DiskDevice,as_ref=True),DocumentField(DiskUUID,as_ref=True),DocumentField(NFS,as_ref=True),DocumentField(TmpFS,as_ref=True),],required=True)fstype=StringField(enum=['ext3','ext4','btrfs'])options=ArrayField(StringField(),min_items=1,unique_items=True)readonly=BooleanField()

a list or a tuple of BaseField s – all items of the array must be
valid according to the field schema at the corresponding index (tuple typing).

min_items (int) – A minimum length of an array.

max_items (int) – A maximum length of an array.

unique_items (bool or BaseField) – Whether all the values in the array must be distinct.

additional_items – If the value of items is a list or a tuple, and the array length is larger than
the number of fields in items, then the additional items are described
by the schema in this property.

pattern_properties (dict from str to BaseField) – A dictionary whose keys are regular expressions (ECMA 262).
Properties match against these regular expressions, and for any that match,
the property is described by the corresponding field schema.

additional_properties (bool or BaseField) – Describes properties that are not described by the properties or pattern_properties.