s-knibbs / dataclasses-jsonschema

JSON schema generation from dataclasses
MIT License
166 stars 38 forks source link

Allow to use fields for arbitrary schema extensions. #120

Open mic47 opened 4 years ago

mic47 commented 4 years ago

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}})