JSONSchema types¶
This module contains Python classes for JSONSchema types which can be helpful when manipulating such data in Python. Each class has a number of attributes and json_repr() method which returns a JSONSchema compatible dictionary.
import json
import jsonschema_gen.schema as js
user = js.Object({
'name': js.String(title='full name', minLength=1),
'email': js.String(pattern='/^[^\.\s][\w\-]+(\.[\w\-]+)*@([\w-]+\.)+[\w-]{2,}$/gm')
}, required=['name', 'email'], additionalProperties=False)
user_or_guest = js.Nullable(user)
user_or_guest.json_repr() # dumps the resulting schema into the python dict
json.dumps(user_or_guest.json_repr()) # dumps the schema to the JSON string
To create your own type you need to implement JSONSchemaType
interface, i.e.
the json_repr() method itself (subclassing is not required).
Note
To make it easier to use with the original JSONSchema documentation the type attribute names have been left in camel-case.
- class JSONSchemaObject[source]¶
Generic JSONSchema object.
>>> JSONSchemaObject().json_repr() {}
- title: str = None¶
- description: str = None¶
- examples: List = None¶
- default: Any = Ellipsis¶
- class AllOf[source]¶
All the included schemas must be valid.
See allOf keyword
>>> AllOf([String(), Integer()]).json_repr() {'allOf': [{'type': 'string'}, {'type': 'integer'}]}
- items: Collection[JSONSchemaType]¶
- class AnyOf[source]¶
Any of the included schemas must be valid.
See anyOf keyword
>>> AnyOf([String(), Integer()]).json_repr() {'anyOf': [{'type': 'string'}, {'type': 'integer'}]}
- items: Collection[JSONSchemaType]¶
- class Array[source]¶
Array type.
See array type
>>> Array(String()).json_repr() {'items': {'type': 'string'}, 'type': 'array'}
- items: JSONSchemaType = None¶
item type for a strict typed array
- prefixItems: Collection[JSONSchemaType] = None¶
a List of fixed object positions for a tuple type
- contains: JSONSchemaType = None¶
must contain this type of object
- additionalItems: bool = None¶
allow additional items
- uniqueItems: bool = None¶
specify an array as a set type
- minItems: int = None¶
- maxItems: int = None¶
- title: str = None¶
- description: str = None¶
- examples: List = None¶
- enum: List[List] = None¶
- default: List = Ellipsis¶
- class Boolean[source]¶
Boolean type.
See boolean type
>>> Boolean().json_repr() {'type': 'boolean'}
- title: str = None¶
- description: str = None¶
- default: bool = Ellipsis¶
- class Const[source]¶
Constant value.
See constants
>>> Const('1').json_repr() {'const': '1'}
- const: Any¶
- title: str = None¶
- description: str = None¶
- class Date[source]¶
Date type alias.
>>> Date().json_repr() {'format': 'date', 'type': 'string'}
- default: str = Ellipsis¶
- description: str = None¶
- enum: List[str] = None¶
- examples: List = None¶
- json_repr() Dict[str, Any] ¶
Produce a JSON-compatible representation of the object.
- maxLength: int = None¶
- minLength: int = None¶
- pattern: str = None¶
regex validation pattern
- title: str = None¶
- class DateTime[source]¶
Datetime type alias.
>>> DateTime().json_repr() {'format': 'date-time', 'type': 'string'}
- default: str = Ellipsis¶
- description: str = None¶
- enum: List[str] = None¶
- examples: List = None¶
- json_repr() Dict[str, Any] ¶
Produce a JSON-compatible representation of the object.
- maxLength: int = None¶
- minLength: int = None¶
- pattern: str = None¶
regex validation pattern
- title: str = None¶
- class Email[source]¶
UUID type alias.
>>> Email().json_repr() {'format': 'email', 'type': 'string'}
- default: str = Ellipsis¶
- description: str = None¶
- enum: List[str] = None¶
- examples: List = None¶
- json_repr() Dict[str, Any] ¶
Produce a JSON-compatible representation of the object.
- maxLength: int = None¶
- minLength: int = None¶
- pattern: str = None¶
regex validation pattern
- title: str = None¶
- class Enum[source]¶
Enum value.
>>> Enum([1, 2, 3]).json_repr() {'enum': [1, 2, 3]}
- enum: List¶
- title: str = None¶
- description: str = None¶
- examples: List = None¶
- default: Any = Ellipsis¶
- class GUID[source]¶
UUID type alias.
>>> GUID().json_repr() {'format': 'uuid', 'type': 'string'}
- default: str = Ellipsis¶
- description: str = None¶
- enum: List[str] = None¶
- examples: List = None¶
- json_repr() Dict[str, Any] ¶
Produce a JSON-compatible representation of the object.
- maxLength: int = None¶
- minLength: int = None¶
- pattern: str = None¶
regex validation pattern
- title: str = None¶
- class Integer[source]¶
Integer type.
See integer type
>>> Integer().json_repr() {'type': 'integer'}
- multipleOf: int = None¶
- minimum: int = None¶
- maximum: int = None¶
- exclusiveMinimum: int = None¶
- exclusiveMaximum: int = None¶
- title: str = None¶
- description: str = None¶
- examples: List = None¶
- enum: List[int] = None¶
- default: int = Ellipsis¶
- class Not[source]¶
Revert the condition of the schema.
See Not keyword
>>> Not(Boolean()).json_repr() {'not': {'type': 'boolean'}}
- item: JSONSchemaType¶
- class Null[source]¶
Null value alias.
>>> Null().json_repr() {'enum': [None]}
- default: Any = Ellipsis¶
- description: str = None¶
- examples: List = None¶
- json_repr() Dict[str, Any] ¶
Produce a JSON-compatible representation of the object.
- title: str = None¶
- class Nullable[source]¶
Nullable value alias.
>>> Nullable(String()).json_repr() {'oneOf': [{'type': 'string'}, {'enum': [None]}]}
- item: JSONSchemaType¶
- class Number[source]¶
Numeric data type.
See numeric type
>>> Number().json_repr() {'type': 'number'}
- multipleOf: float = None¶
- minimum: float = None¶
- maximum: float = None¶
- exclusiveMinimum: float = None¶
- exclusiveMaximum: float = None¶
- title: str = None¶
- description: str = None¶
- examples: List = None¶
- enum: List[float] = None¶
- default: float = Ellipsis¶
- class Object[source]¶
JSON object type (dictionary-like).
See object type
>>> Object({'name': String()}).json_repr() {'properties': {'name': {'type': 'string'}}, 'type': 'object'}
- properties: Dict[str, JSONSchemaType] = None¶
- patternProperties: Dict[str, JSONSchemaType] = None¶
- additionalProperties: bool = None¶
- minProperties: int = None¶
- maxProperties: int = None¶
- required: List[str] = None¶
- title: str = None¶
- description: str = None¶
- examples: List = None¶
- enum: List[Dict] = None¶
- default: Dict = Ellipsis¶
- class OneOf[source]¶
Only one of the included schemas must be valid.
See oneOf keyword
>>> OneOf([String(), Integer()]).json_repr() {'oneOf': [{'type': 'string'}, {'type': 'integer'}]}
- items: Collection[JSONSchemaType]¶
- class String[source]¶
String type.
See string type
>>> String().json_repr() {'type': 'string'}
- minLength: int = None¶
- maxLength: int = None¶
- pattern: str = None¶
regex validation pattern
- format: Literal['JSONSchemaType', 'date-time', 'time', 'date', 'email', 'idn-email', 'hostname', 'idn-hostname', 'ipv4', 'ipv6', 'uri', 'uri-reference', 'iri', 'iri-reference', 'regex'] = None¶
string format
- title: str = None¶
- description: str = None¶
- examples: List = None¶
- enum: List[str] = None¶
- default: str = Ellipsis¶