Skip to main content

middle-schema

Translate your middle model declarations to OpenAPI, JSONSchema or any other schema you need!

In a nutshell

>>> import enum
>>> import json
>>> import typing as t
>>> import middle
>>> from middle_schema.openapi import parse

>>> @enum.unique
... class PlatformEnum(str, enum.Enum):
...     XBOX1 = "XBOX1"
...     PLAYSTATION4 = "PLAYSTATION4"
...     PC = "PC"

>>> @enum.unique
... class LanguageEnum(enum.IntEnum):
...     ENGLISH = 1
...     JAPANESE = 2
...     SPANISH = 3
...     GERMAN = 4
...     PORTUGUESE = 5

>>> @enum.unique
... class CityRegionEnum(str, enum.Enum):
...     TROPICAL = "TROPICAL"
...     TEMPERATE = "TEMPERATE"
...     BOREAL = "BOREAL"

>>> class City(middle.Model):
...     __description__ = "One awesome city built"
...     name = middle.field(type=str, description="The city name")
...     region = middle.field(
...         default=CityRegionEnum.TEMPERATE,
...         type=CityRegionEnum,
...         description="The region this city is located",
...     )

>>> class Player(middle.Model):
...     nickname = middle.field(
...         type=str, description="The nickname of the player over the internet"
...     )
...     youtube_channel = middle.field(
...         type=str, description="The YouTube channel of the player", default=None
...     )

>>> class Game(middle.Model):
...     __description__ = "An electronic game model"
...     name = middle.field(type=str, description="The name of the game")
...     platform = middle.field(
...         type=PlatformEnum, description="Which platform it runs on"
...     )
...     score = middle.field(
...         type=float,
...         description="The average score of the game",
...         minimum=0,
...         maximum=10,
...         multiple_of=0.1,
...     )
...     resolution_tested = middle.field(
...         type=str,
...         description="The resolution which the game was tested",
...         pattern="^\d+x\d+$",
...     )
...     genre = middle.field(
...         type=t.List[str],
...         description="One or more genres this game is part of",
...         min_items=1,
...         unique_items=True,
...     )
...     rating = middle.field(
...         type=t.Dict[str, float],
...         description="Ratings given on specialized websites",
...         min_properties=3,
...     )
...     players = middle.field(
...         type=t.Set[str],
...         description="Some of the notorious players of this game",
...     )
...     language = middle.field(
...         type=LanguageEnum, description="The main language of the game"
...     )
...     awesome_city = middle.field(type=City)
...     remarkable_resources = middle.field(
...         type=t.Union[Player, City],
...         description="Some remarkable resources of this game over the internet",
...     )

>>> api = parse(Game)

>>> json.dumps(api.specification, indent=4, sort_keys=True)
{
    "description": "An electronic game model",
    "properties": {
        "awesome_city": {
            "description": "One awesome city built",
            "properties": {
                "name": {
                    "description": "The city name",
                    "type": "string"
                },
                "region": {
                    "choices": [
                        "TROPICAL",
                        "TEMPERATE",
                        "BOREAL"
                    ],
                    "description": "The region this city is located",
                    "type": "string"
                }
            },
            "required": [
                "name"
            ],
            "type": "object"
        },
        "genre": {
            "description": "One or more genres this game is part of",
            "items": {
                "type": "string"
            },
            "minItems": 1,
            "type": "array",
            "uniqueItems": true
        },
        "language": {
            "choices": [
                1,
                2,
                3,
                4,
                5
            ],
            "description": "The main language of the game",
            "format": "int64",
            "type": "integer"
        },
        "name": {
            "description": "The name of the game",
            "type": "string"
        },
        "platform": {
            "choices": [
                "XBOX1",
                "PLAYSTATION4",
                "PC"
            ],
            "description": "Which platform it runs on",
            "type": "string"
        },
        "players": {
            "description": "Some of the notorious players of this game",
            "items": {
                "properties": {
                    "nickname": {
                        "description": "The nickname of the player over the internet",
                        "type": "string"
                    },
                    "youtube_channel": {
                        "description": "The YouTube channel of the player",
                        "type": "string"
                    }
                },
                "required": [
                    "nickname"
                ],
                "type": "object"
            },
            "type": "array"
        },
        "rating": {
            "additionalProperties": {
                "format": "double",
                "type": "number"
            },
            "description": "Ratings given on specialized websites",
            "minProperties": 3,
            "type": "object"
        },
        "remarkable_resources": {
            "anyOf": [
                {
                    "properties": {
                        "nickname": {
                            "description": "The nickname of the player over the internet",
                            "type": "string"
                        },
                        "youtube_channel": {
                            "description": "The YouTube channel of the player",
                            "type": "string"
                        }
                    },
                    "required": [
                        "nickname"
                    ],
                    "type": "object"
                },
                {
                    "description": "One awesome city built",
                    "properties": {
                        "name": {
                            "description": "The city name",
                            "type": "string"
                        },
                        "region": {
                            "choices": [
                                "TROPICAL",
                                "TEMPERATE",
                                "BOREAL"
                            ],
                            "description": "The region this city is located",
                            "type": "string"
                        }
                    },
                    "required": [
                        "name"
                    ],
                    "type": "object"
                }
            ],
            "description": "Some remarkable resources of this game over the internet"
        },
        "resolution_tested": {
            "description": "The resolution which the game was tested",
            "pattern": "^\\d+x\\d+$",
            "type": "string"
        },
        "score": {
            "description": "The average score of the game",
            "format": "double",
            "maximum": 10,
            "minimum": 0,
            "multipleOf": 0.1,
            "type": "number"
        }
    },
    "required": [
        "name",
        "platform",
        "score",
        "resolution_tested",
        "genre",
        "rating",
        "players",
        "language",
        "awesome_city",
        "remarkable_resources"
    ],
    "type": "object"
}

Documentation

https://middle-schema.readthedocs.io/en/latest/

License

middle-schema is a free software distributed under the MIT license.

Changelog

v0.2.0 on 2018-08-01

  • Small refactoring on the Skeleton parser;

  • OpenAPI component and schema generation of middle models;

  • 99%+ of code coverage.

v0.1.0 on 2018-07-26

  • First release on PyPI. Not stable.

Metadata

Release files for middle-schema 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for middle-schema 0.2.0
File Size Uploaded
middle-schema-0.2.0.tar.gz 45.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for middle-schema 0.2.0
File Interpreter ABI Platform
middle_schema-0.2.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 52.7 kB

Release files / middle-schema-0.2.0.tar.gz

Download URL middle-schema-0.2.0.tar.gz
Size 45.3 kB
Tags Source
SHA-256 checksum
How to use checksums
64ccdb5fc77f41ab68c2d5011c104e2306816b19b6baa3bc52362f1fec526a3d
BLAKE2b-256 checksum
How to use checksums
d22f8931c8ff6aa0d4bd773a5896973f8b1ac2d6069c996fbbfbd8975b326cf1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.6

Release files / middle_schema-0.2.0-py2.py3-none-any.whl

Download URL middle_schema-0.2.0-py2.py3-none-any.whl
Size 7.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7d085cb27be48e57a1beef7008ad07f40b6be6bfecd11e575137403f15f690a9
BLAKE2b-256 checksum
How to use checksums
058ef0d4467159d887fd79a6152edd275b88767cd74a1c7995a5e03349fb794d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.6

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page