feat: add unevaluatedItems lesson for closing tuples (#61)#210
feat: add unevaluatedItems lesson for closing tuples (#61)#210Avanish-Gupta-CSE wants to merge 2 commits into
Conversation
) This lesson demonstrates the proper use case for unevaluatedItems: - Closing a tuple defined in a referenced schema using $ref - Why items cannot achieve this with schema composition - The difference between items and unevaluatedItems Example: A coordinate tuple (lat, long) that needs to be closed to prevent extra items like altitude. Fixes json-schema-org#61
jdesrosiers
left a comment
There was a problem hiding this comment.
I can confirm that this is a correct and real world use case of JSON Schema. There's just one thing that I think can be worded better (details inline).
| - Allows exactly two number items (from the referenced coordinate tuple) | ||
| - Rejects any additional items beyond the defined tuple | ||
|
|
||
| This is the key difference: `unevaluatedItems` looks at what items have been "evaluated" by any part of the schema, while `items` only looks at the local `prefixItems`. |
There was a problem hiding this comment.
"any part of the schema" isn't quite right. Or, at least, it's a little misleading. I don't know the best way to explain it, but this example should illustrate what I mean.
{
"allOf": [
{ "$ref": "#/$defs/coordinate" },
{ "unevaluatedItems": false }
],
"$defs": {
"coordinate": {
"type": "array",
"prefixItems": [
{ "type": "number" },
{ "type": "number" }
]
}
}
}unevaluatedItems doesn't "see" the prefixItems in this case. It only "sees" the prefixItems "below" it in the schema. (Although because $ref allows you to jump around the schema it isn't always clear what "below" means). Hope that helps.
|
Thanks for the review ! |
jdesrosiers
left a comment
There was a problem hiding this comment.
That looks better. Thanks for updating. I'll leave the other aspects of the review to the actual maintainers of this project.
|
Hi, just following up on this. The feedback from @jdesrosiers has been addressed. Would a maintainer be able to review this? Thank you! |
What kind of change does this PR introduce?
Feature - Adds a new lesson for the
unevaluatedItemskeyword.Issue Number:
Screenshots/videos:
N/A - This is a new lesson file (MDX content + code.ts with test cases).
If relevant, did you update the documentation?
N/A - This PR adds lesson content, not documentation.
Summary
Adds a new lesson demonstrating the proper use case for
unevaluatedItems- closing a tuple defined in a referenced schema.This addresses the issue where the original lesson could be solved with
itemsinstead ofunevaluatedItems. The new lesson:$refto reference a base tuple (coordinate pair with lat/long)unevaluatedItems: falseitemscannot achieve this when using schema compositionThis matches the approach approved by @jdesrosiers in the issue discussion.
Lesson Files Added:
content/07-Miscellaneous/02-Extending-Closed-Tuples-with-unevaluatedItems/instructions.mdxcontent/07-Miscellaneous/02-Extending-Closed-Tuples-with-unevaluatedItems/code.tsTest Cases: 9 comprehensive test cases covering valid coordinate tuples and invalid scenarios (extra items, wrong types).
Does this PR introduce a breaking change?
No.