jsonschema-gen is Python type hints parser which can convert function and method annotations into JSONSchema objects.
- Pythonic classes for JSONSchema types
- Extensive type coverage: TypedDict, Generic, NewType, etc.
- No external dependencies
Installation
With pip and python 3.8+:
pip3 install jsonschema-gen
How to use
See the user guide for more info.
Create a parser:
from jsonschema_gen import Parser
parser = Parser(strict=True)
Generate schema for your function or method from Python type hints (see the list of supported types):
from typing import NewType
UserName = NewType('UserName', str)
class UserData:
def get_user(self, name: UserName, active: bool = True) -> dict:
"""Get user by username."""
annotation = parser.parse_function(UserData.get_user, UserData)
The result is an annotation object with input .kwargs and output .returns. You can get a JSONSchema compatible dict
using json_repr() on .kwargs:
schema = annotation.kwargs.json_repr()
The result would look like this (if converted to JSON with dumps):
{
"type": "object",
"title": "Get user by username.",
"properties": {
"name": {
"title": "Username",
"type": "string"
},
"active": {
"type": "boolean",
"default": true
}
},
"required": [
"name"
],
"additionalProperties": false
}
Use fastjsonschema or other JSONSchema validation library to create a validator for the schema:
from fastjsonschema import compile
validator = compile(schema)
valiator({'name': 'John', 'email': 'john@dowe'})
Alternatively you can pass the whole class to the parser to get the annotation mapping:
annotations = parser.parse_class(UserData)
annotations['get_user'].kwargs.json_repr()
Compatibility
The Python type hints are vast and yet not well organized, so there could always be some data type I forgot to add here. Read the customization guide to extend the standard list of type parsers.
Some annotations cannot be converted to JSONSchema objects, for example: positional-only arguments, variable positionals, etc. There are different strategies considering these types of parameters.
Python 3.8 compatibility is so-so due to lots of features and changes made in 3.9. However, it still should support most of the functionality.
Also read about the strict mode.
Metadata
Release files for jsonschema-gen 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| jsonschema_gen-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for jsonschema_gen-0.1.1-py3-none-any.whl |
Release files / jsonschema_gen-0.1.1-py3-none-any.whl
| Download URL | jsonschema_gen-0.1.1-py3-none-any.whl |
|---|---|
| Size | 11.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
26835a130d781b5cf311ba1ef5d37532943a57bd21c7667e099410a988d34f50
|
|
BLAKE2b-256 checksum How to use checksums |
c66e7c0049f841179e0bf001a5a64536c23d4a51383f240569139d13875eb575
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.2
|
Security for jsonschema-gen 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.