Skip to content

✨ Proposal: "itemsMultipleOf" #1641

@HansBrende

Description

@HansBrende

Describe the inspiration for your proposal

Splitting out this comment into its own issue, as suggested, since my use-case requires a much less powerful syntax than the itemsPattern proposed there.

Use-case: it's common to have arrays that contain flattened lists of tuples, such as x,y-coordinates. So it would be great to be able to say: "the number of items must be a multiple of 2".

Describe the proposal

Currently, every numeric constraint is also supported for array lengths, except multipleOf.

minimum: n -> minItems: n
maximum: n -> maxItems: n
exclusiveMinimum: n -> minItems: n + 1
exclusiveMaximum: n -> maxItems: n - 1
multipleOf: n -> ???

So I propose itemsMultipleOf to fill that last little gap.

Describe alternatives you've considered

I also considered the following possibilities:

{
    // 1.
    "itemCountMultipleOf": 2,
    // 2.
    "arrayLength": {"multipleOf": 2},
    // 3.
    "countItems": {"multipleOf": 2},
    // 4.
    "count": {"multipleOf": 2}
} 

But as @jdesrosiers mentioned, allowing length to be a whole subschema on its own (in cases 2, 3, and 4) seems like overkill when there is only one thing missing from the numeric schema: multipleOf.

Credit where credit is due: GPT 5 (extended thinking) came up with the name itemsMultipleOf in response to the question "What would be the most logical new extension to json schema that would allow you to specify that the number of items is a multiple of a certain integer, e.g. 2?"

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalInitial discussion of a new idea. A project will be created once a proposal document is created.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions