dataclasses-jsonschema icon indicating copy to clipboard operation
dataclasses-jsonschema copied to clipboard

Support adding examples to the generated schema

Open s-knibbs opened this issue 5 years ago • 0 comments

It would useful to be able to include examples in the generated json schema. I can think of a couple of ideas for implementing this:

  1. Add an examples classmethod that can be overriden:

@dataclass
class Point(JsonSchemaMixin):
    x: float
    y: float

    @classmethod
    def examples(self) -> List['Point']:
        return [
            Point(1.0, 2.0),
            Point(0.0, -1.0)
        ]
  1. Add an examples option to the field metadata:
@dataclass
class Line(JsonSchemaMixin):
    coords: List[Point] = field(metadata=dict(examples=[Point(1.0, 2.0)]))

Both 1 and 2 could be implemented together.

Note: Examples should be ignored when generating draft 4 json schema, since this was only added in draft 6.

s-knibbs avatar Apr 11 '19 15:04 s-knibbs