Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 annotations produced by subschemas in its evaluation scope. In this example, the `$ref` and `unevaluatedItems` are in the same schema object, so the tuple items evaluated by the referenced schema are visible to `unevaluatedItems`.

```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` only uses annotations from the same evaluation path (roughly, subschemas "below" it). If the tuple is evaluated in a different branch (for example, inside an `allOf` alongside `unevaluatedItems`), it won't see those `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.