Giter Club home page Giter Club logo

jsonschema-gentypes's Introduction

JSON Schema generate Python types

Tools to generate Python types based on TypedDict from a JSON schema

Quick start

install:

python3 -m pip install --user jsonschema-gentypes

Convert a JSON schema to a Python file contains the types:

jsonschema-gentypes --json-schema=<JSON schema> --python=<destination Python file>

Config file

You can also write a config file named jsonschema-gentypes.yaml with:

headers: >
  # Automatically generated file from a JSON schema
# Used to correctly format the generated file
callbacks:
  - - black
  - - isort
generate:
  - # JSON schema file path
    source: jsonschema_gentypes/schema.json
    # Python file path
    destination: jsonschema_gentypes/configuration.py
    # The name of the root element
    root_name: Config
    # Argument passed to the API
    api_arguments:
      additional_properties: Only explicit
    # Rename an element
    name_mapping: {}
    # The minimum Python version that the code should support. By default the
    # currently executing Python version is chosen. Note that the output
    # may require typing_extensions to be installed.
    python_version: '3.11'

And just run:

jsonschema-gentypes

Default

The default values are exported in the Python file, then you can do something like that:

value_with_default = my_object.get('field_name', my_schema.FIELD_DEFAULT)

Limitations

Requires Python 3.8

See the issues with label "limitation".

Pre-commit hooks

This project provides pre-commit hooks to automatically generate the files.

repos:
  - repo: https://github.com/camptocamp/jsonschema-gentypes
    rev: <version> # Use the ref you want to point at
    hools:
      - id: jsonschema-gentypes
        files: |
          (?x)^(
              jsonschema-gentypes\.yaml|
              <schema_path>\.json
          )$

See also the pre_commit section in the configuration to run the pre-commit just after the generation, for example with:

pre_commit:
  enabled: true
  arguments:
    - --color=never

OpenAPI3

We can also generate types for OpenAPI3 schemas (automatically detected).

The result of our example in tests/openapi3.json can be used in pyramid with for example:

import pyramid.request
from pyramid.view import view_config
from openaoi3 import *

def open_api(func):
    def wrapper(request: pyramid.request.Request, **kwargs) -> Any:
        typed_request = {}
        try:
            typed_request{'request_body'} = request.json
        except Exception as e:
            pass
        typed_request{'path'} = request.matchdict
        typed_request{'query'} = request.params

        return = func(request, request_typed=typed_request, **kwargs)

    return wrapper


@view_config(route_name="route_name", renderer="json")
@open_api
def view(
  request: pyramid.request.Request,
  request_typed: OgcapiCollectionsCollectionidGet,
) -> OgcapiCollectionsCollectionidGetResponse:
    return {...}

Contributing

Install the pre-commit hooks:

pip install pre-commit
pre-commit install --allow-missing-config

The prospector tests should pass.

The code should be typed.

The code should be tested with pytests.

jsonschema-gentypes's People

Contributors

sbrunner avatar dependabot[bot] avatar renovate[bot] avatar c2c-bot-gis-ci avatar renovate-bot avatar passchieri avatar pre-commit-ci[bot] avatar chlandsi avatar snocorp avatar untitaker avatar peteole avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.