Repository navigation
feat: add unevaluatedItems lesson for closing tuples (#61) #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Avanish-Gupta-CSE
wants to merge
2
commits into
json-schema-org:main
Choose a base branch
from
Avanish-Gupta-CSE:feat/issue-61-unevaluatedItems-lesson
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
content/07-Miscellaneous/02-Extending-Closed-Tuples-with-unevaluatedItems/code.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| const code: any = { | ||
| $ref: "#/$defs/coordinate", | ||
| $defs: { | ||
| coordinate: { | ||
| type: "array", | ||
| prefixItems: [ | ||
| { type: "number", description: "latitude" }, | ||
| { type: "number", description: "longitude" }, | ||
| ], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const solution = structuredClone(code); | ||
| solution.unevaluatedItems = false; | ||
|
|
||
| const testCases = [ | ||
| // Valid: exactly two numbers (lat, long) | ||
| { | ||
| input: [40.7128, -74.006], | ||
| expected: true, | ||
| }, | ||
| // Valid: different coordinates | ||
| { | ||
| input: [51.5074, -0.1278], | ||
| expected: true, | ||
| }, | ||
| // Valid: negative coordinates | ||
| { | ||
| input: [-33.8688, 151.2093], | ||
| expected: true, | ||
| }, | ||
| // Valid: zero coordinates (null island) | ||
| { | ||
| input: [0, 0], | ||
| expected: true, | ||
| }, | ||
| // Invalid: extra item (altitude) - this is what unevaluatedItems: false prevents | ||
| { | ||
| input: [40.7128, -74.006, 10], | ||
| expected: false, | ||
| }, | ||
| // Invalid: extra string item | ||
| { | ||
| input: [40.7128, -74.006, "NYC"], | ||
| expected: false, | ||
| }, | ||
| // Invalid: multiple extra items | ||
| { | ||
| input: [40.7128, -74.006, 10, "NYC", true], | ||
| expected: false, | ||
| }, | ||
| // Invalid: wrong type for latitude | ||
| { | ||
| input: ["forty", -74.006], | ||
| expected: false, | ||
| }, | ||
| // Invalid: wrong type for longitude | ||
| { | ||
| input: [40.7128, "west"], | ||
| expected: false, | ||
| }, | ||
| ]; | ||
|
|
||
| module.exports = { | ||
| code, | ||
| solution, | ||
| testCases, | ||
| }; |
65 changes: 65 additions & 0 deletions
65
...Miscellaneous/02-Extending-Closed-Tuples-with-unevaluatedItems/instructions.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| --- | ||
| title: Closing Tuples with unevaluatedItems | ||
| description: "Learn how to close tuple arrays in JSON Schema using the unevaluatedItems keyword when working with schema composition like $ref." | ||
| keywords: "closing tuples, unevaluatedItems, JSON Schema, arrays, tuples, prefixItems, $ref, schema composition" | ||
| --- | ||
|
|
||
| # Closing Tuples with unevaluatedItems | ||
|
|
||
| In the Arrays module, we learned about using `items: false` to close a tuple and prevent additional items. However, `items` only recognizes `prefixItems` declared in the same [subschema](https://json-schema.org/learn/glossary#subschema) as itself. | ||
|
|
||
| This becomes a problem when you want to close a tuple that's defined in a referenced schema using `$ref`. | ||
|
|
||
| ## The Problem with items | ||
|
|
||
| Consider a coordinate tuple defined in `$defs` that we want to reference and close: | ||
|
|
||
| ```json | ||
| { | ||
| "$ref": "#/$defs/coordinate", | ||
| "items": false, | ||
| "$defs": { | ||
| "coordinate": { | ||
| "type": "array", | ||
| "prefixItems": [ | ||
| { "type": "number" }, | ||
| { "type": "number" } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| This **will not work as expected**. The `items: false` keyword only sees `prefixItems` in its own subschema (there are none), so it would reject ALL items, making the schema unusable. | ||
|
|
||
| ## The Solution: unevaluatedItems | ||
|
|
||
| The `unevaluatedItems` keyword solves this problem. It considers all `prefixItems` across the entire schema, including those in referenced schemas. | ||
|
|
||
| ```json highlightLineStart={3} | ||
| { | ||
| "$ref": "#/$defs/coordinate", | ||
| "unevaluatedItems": false, | ||
| "$defs": { | ||
| "coordinate": { | ||
| "type": "array", | ||
| "prefixItems": [ | ||
| { "type": "number" }, | ||
| { "type": "number" } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Now the schema correctly: | ||
| - 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`. | ||
|
|
||
| ## Task | ||
|
|
||
| You are given a coordinate tuple schema that defines latitude and longitude. Your task is to **close the tuple** so that no additional items can be added beyond the two coordinates. | ||
|
|
||
| > **Hint:** Add `unevaluatedItems` set to `false` to prevent any extra items in the array. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"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" } ] } } }unevaluatedItemsdoesn't "see" theprefixItemsin this case. It only "sees" theprefixItems"below" it in the schema. (Although because$refallows you to jump around the schema it isn't always clear what "below" means). Hope that helps.