attrs-jsonschema

convert a attrs annotation to json a json schema.


License
MIT
Install
pip install attrs-jsonschema==0.2

Documentation

jsonschema-extractor

jsonschema-extractor is a library and extensible framework for extracting json schema from various object and primitives.

Out of the box support exists for:

Usage

from typing import List
import jsonschema_extractor
assert jsonschema_extractor.extract(List[int]) == {
    "type": "array",
    "items": {"type": "integer"}
}

Attrs-example

import attr
from attr.validators import instance_of
import jsonschema_extractor

@attr.define
class Example(object):
    integer: int = attr.field()
    foo = attr.field(metadata={"jsonschema": {"type": "string", "format": "uuid"}})
    validator_list: List[float] = attr.field()
    string: str = attr.field(
        default="foo",
        metadata={"description": "This is an description."}
    )

assert extractor.extract(Example) == {
    "type": "object",
    "title": "Example",
    "properties": {
        "string": {"description": "This is an description.", "type": "string"},
        "integer": {"type": "integer"},
        "validator_list": {"items": {"type": "number"}, "type": "array"},
        "foo": {"type": "string", "format": "uuid"},
    },
    "required": ["integer", "foo", "validator_list"],
}