dataclasses-jsonschema
dataclasses-jsonschema copied to clipboard
Support adding examples to the generated schema
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:
- 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)
]
- 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.