Skip to content

Feature request: support JSON Schema's prefixItems with precisely typed tuples instead of imprecise lists #1546

@kylebebak

Description

@kylebebak

Is your feature request related to a problem? Please describe.

When generating models from JSON Schema it would be nice to be able to generate Tuple[] types using Tuple Validation.

Describe the solution you'd like

Consider the following schema:

{
  "$schema": "https://json-schema.org/draft-07/schema",
  "type": "object",
  "properties": {
    "a": {
      "type": "array",
      "prefixItems": [
        { "type": "number" },
        { "type": "string" }
      ],
      "minItems": 2,
      "maxItems": 2
    }
  },
  "required": ["a"]
}

Running datamodel-codegen --input test.json --input-file-type jsonschema --output-model-type typing.TypedDict --output model.py, this produces the output:

from __future__ import annotations

from typing import List

from typing_extensions import TypedDict


class Model(TypedDict):
    a: List

Ideally, it would produce the following instead:

from __future__ import annotations

from typing import Tuple

from typing_extensions import TypedDict


class Model(TypedDict):
    a: Tuple[float, str]

Describe alternatives you've considered

The only real alternative to typing an array with heterogenous types is to replace the array with an object whose properties can be typed individually. But changing a schema to use an object instead of an array isn't always possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions