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

Allow to use fields for arbitrary schema extensions.

Open mic47 opened this issue 5 years ago • 0 comments

Currently, you can use field metadata for custom descriptions, examples. But to do more custom validation (like constraints for integers), you have to use NewType and registering this type. While NewType is good for things like Email, and Enum-like things, it's cumbersome for numeric things, since you have to create newtype for each restriction (i.e. for restricting by 1 and 0 you need separate types). Instead, metadata from field could be used, as like this:

>>@dataclass
class Box(JsonSchemaMixin):
    """A box."""
    width: int = field(metadata={"description": "Some desctiption", "schema": {"minimum": 1}})

mic47 avatar Nov 30 '19 20:11 mic47