s-knibbs / dataclasses-jsonschema

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

Support unrecognized enum values #109

Closed s-knibbs closed 4 years ago

s-knibbs commented 4 years ago

It should be possible to support deserialisation of unrecognised enum values without throwing an error so that we handle new enum members gracefully. Like protocol buffers, the underlying enum value should be left as is.

s-knibbs commented 4 years ago

I propose to add an argument (validate_enums: bool = True ), to the from_dict method which will generate the schema but without any enum specifiers and validate against that. For example:

class PetType(Enum):
    CAT = "cat"
    DOG = "dog"

@dataclass
class Pet(JsonSchemaMixin):
    name: str
    type: PetType

p = Pet.from_dict({"name": "snakey", "type": "python"}, validate_enums=False)
>>> p.type
'python'