s-knibbs / dataclasses-jsonschema

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

Support for array of enums #99

Open sahuguet opened 4 years ago

sahuguet commented 4 years ago

Hi,

I am trying to generate something like this

"propulsion_type": {
      "$id": "#/definitions/propulsion_type",
      "type": "array",
      "description": "The type of propulsion; allows multiple values",
      "items": {
        "type": "string",
        "enum": [
          "combustion",
          "electric",
          "electric_assist",
          "human"
        ]
      },
      "minItems": 1
    }

Is it something that can be implemented using dataclasses-jsonschema?

s-knibbs commented 4 years ago

You could do something like:


class PropulsionType(Enum):
    COMBUSTION = "combustion"
    ELECTRIC = "electric"
    ...

@dataclass
class SomeObject(JsonSchemaMixin):
    propulsion_type: List[PropulsionType]

Currently, it's not possible to specify the minItems requirement though. I might need to add this as one of the possible metadata fields that you can specify so that you could do the following:

@dataclass
class SomeObject(JsonSchemaMixin):
    propulsion_type: List[PropulsionType] = field(metadata=JsonSchemaMeta(min_items=1))