understanding-json-schema icon indicating copy to clipboard operation
understanding-json-schema copied to clipboard

prefixItems should not allow arrays of variable length

Open markoutso opened this issue 3 years ago • 5 comments

The documentation states that the keyword prefixItems is used for tuple validation. In the realm of programming and mathematics a tuple is something that has specific length. Quoting Wikipedia:

In computer science, tuples come in many forms. Most typed functional programming languages implement tuples directly as product types,[1] tightly associated with algebraic data types, pattern matching, and destructuring assignment.[2] Many programming languages offer an alternative to tuples, known as record types, featuring unordered elements accessed by label.[3] A few programming languages combine ordered tuple product types and unordered record types into a single construct, as in C structs and Haskell records. Relational databases may formally identify their rows (records) as tuples.

The json schema specification should follow common conventions or choose a better description for this validation.

In my opinion referring to arrays with variable as length as tuples is something that goes against the definition of the tuple and creates unnecessary confusion.

I would suggest that prefixItems stays as it is and it requires that all sub schemas validate the array elements in order. No more elements or should be allowed.

markoutso avatar Feb 17 '22 08:02 markoutso

Tuples are but one application of this keyword. I agree that it's odd they are highlighted as the reasoning behind the keyword, but it's current function does have merit.

Perhaps we can improve this section by focusing on what it does rather than try to illustrate one use case.

@jdesrosiers what do you think of moving the tuples language out of the spec and into the UJS content?

gregsdennis avatar Feb 17 '22 11:02 gregsdennis

The documentation states...

@markoutso @gregsdennis I don't see "tuple" in this repo at all. Which documentation do you mean specifically? (Likely need to migrate the issue.)

I usually describe it as "tuple form" validation as opposed to tuple validation.

Relequestual avatar Feb 17 '22 17:02 Relequestual

Thanks everyone for looking at this! @Relequestual I double checked and you are right. This belongs to the Understanding Json Schema book.

markoutso avatar Feb 18 '22 08:02 markoutso

I agree "tuple" isn't a perfect description, but I think it's more helpful than misleading. prefixItems (or previously array-form-items) is primarily useful for describing a tuple. I think the way it's written makes it sound like, if you're using this keyword then you're describing a tuple, which isn't always true. I think I'd like the example to be a tuple and call it a tuple, but make it more clear that you can describe things that are not quite tuples with this keyword as well.

In any case, I have another three months before I'll have a chance to work on this. So, don't wait on me if anyone wants to put up a PR.

jdesrosiers avatar Feb 23 '22 03:02 jdesrosiers

is primarily useful for describing a tuple

I think this is an incorrect assumption, and probably the basis of this confusion.

gregsdennis avatar Feb 23 '22 03:02 gregsdennis