Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};
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`.

Copy link
Copy Markdown
Member

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" }
      ]
    }
  }
}

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.


## 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.