Skip to main content

JSON schema Utils

Project description

ju

JSON schema Utils

To install: pip install ju

Documentation

Examples

JSON Schema

You have tools to extract JSON schema information from python objects, as well as to create python objects from them.

>>> from ju import signature_to_json_schema, json_schema_to_signature
>>>
>>>
>>> def earth(north: str, south: bool, east: int = 1, west: float = 2.0):
...     """Earth docs"""
...     return f'{north=}, {south=}, {east=}, {west=}'
...
>>> schema = signature_to_json_schema(earth)
>>> assert schema == {
...     'description': 'Earth docs',
...     'title': 'earth',
...     'type': 'object',
...     'properties': {
...         'north': {'type': 'string'},
...         'south': {'type': 'boolean'},
...         'east': {'type': 'integer', 'default': 1},
...         'west': {'type': 'number', 'default': 2.0},
...     },
...     'required': ['north', 'south'],
... }
>>>
>>> sig = json_schema_to_signature(schema)
>>> sig
<Sig (north: str, south: bool, east: int = 1, west: float = 2.0)>
>>> sig.name
'earth'
>>> sig.docs
'Earth docs'

React JSON Schema Form (rjsf)

You can get a react-jsonschema-form (rjsf) specification (see the rjsf playground) from a python function.

>>> def foo(
    ...     a_bool: bool,
    ...     a_float=3.14,
    ...     an_int=2,
    ...     a_str: str = 'hello',
    ...     something_else=None
    ... ):
    ...     '''A Foo function'''
    >>>
    >>> form_spec = func_to_form_spec(foo)
    >>> assert form_spec == {
    ...     'rjsf': {
    ...         'schema': {
    ...             'title': 'foo',
    ...             'type': 'object',
    ...             'properties': {
    ...                 'a_bool': {'type': 'boolean'},
    ...                 'a_float': {'type': 'number', 'default': 3.14},
    ...                 'an_int': {'type': 'integer', 'default': 2},
    ...                 'a_str': {'type': 'string', 'default': 'hello'},
    ...                 'something_else': {'type': 'string', 'default': None}
    ...             },
    ...             'required': ['a_bool'],
    ...             'description': 'A Foo function'
    ...         },
    ...         'uiSchema': {
    ...             'ui:submitButtonOptions': {
    ...                 'submitText': 'Run'
    ...             },
    ...             'a_bool': {'ui:autofocus': True}
    ...         },
    ...         'liveValidate': False,
    ...         'disabled': False,
    ...         'readonly': False,
    ...         'omitExtraData': False,
    ...         'liveOmit': False,
    ...         'noValidate': False,
    ...         'noHtml5Validate': False,
    ...         'focusOnFirstError': False,
    ...         'showErrorList': 'top'
    ...     }
    ... }

OpenAPI Routes

Represents a collection of routes in an OpenAPI specification.

Each instance of this class contains a list of Route objects, which can be accessed and manipulated as needed.

>>> from yaml import safe_load
>>> spec_yaml = '''
... openapi: 3.0.3
... paths:
...   /items:
...     get:
...       summary: List items
...       responses:
...         '200':
...           description: An array of items
...     post:
...       summary: Create item
...       responses:
...         '201':
...           description: Item created
... '''
>>> spec = safe_load(spec_yaml)
>>> routes = Routes(spec)
>>> len(routes)
2
>>> list(routes)
[('get', '/items'), ('post', '/items')]
>>> r = routes['get', '/items']
>>> r
Route(method='get', endpoint='/items')
>>> r.method_data
{'summary': 'List items', 'responses': {'200': {'description': 'An array of items'}}}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ju-0.1.25.tar.gz (39.1 kB view details)

Uploaded Source

Built Distribution

ju-0.1.25-py3-none-any.whl (42.4 kB view details)

Uploaded Python 3

File details

Details for the file ju-0.1.25.tar.gz.

File metadata

  • Download URL: ju-0.1.25.tar.gz
  • Upload date:
  • Size: 39.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for ju-0.1.25.tar.gz
Algorithm Hash digest
SHA256 b05bb2b3c482d0b6b7bbe556f72da8809796aea5b03f54b6dce44537114f551f
MD5 6d5e330b9fcc0a94d775aa279705480c
BLAKE2b-256 00593a33bc22246709304c32954ba5b79268e7fc841e01cc08337746fbed2d31

See more details on using hashes here.

File details

Details for the file ju-0.1.25-py3-none-any.whl.

File metadata

  • Download URL: ju-0.1.25-py3-none-any.whl
  • Upload date:
  • Size: 42.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for ju-0.1.25-py3-none-any.whl
Algorithm Hash digest
SHA256 cd272e06b071dfb380f5f77e778ee9db124cb713c26339e93e2cd74652b8b0cb
MD5 ff68b16c0b881b73da7aa62fc914f9d6
BLAKE2b-256 350f6fe21fcd283d1621e6d33ff078761d9664798b0f2a39bfc84b31702cd615

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page