Skip to content

Commit a90ec68

Browse files
committed
change: replace node.additionalItems by node.items for consistency
1 parent b82cfa5 commit a90ec68

File tree

8 files changed

+21
-36
lines changed

8 files changed

+21
-36
lines changed

src/SchemaNode.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,6 @@ export interface SchemaNode extends SchemaNodeMethodsType {
114114
$id?: string;
115115
$defs?: Record<string, SchemaNode>;
116116
$ref?: string;
117-
/**
118-
* # Items-object schema for additional items for drafts <= 2019-09
119-
*
120-
* - `additionalItems` is ignored when items-schema is not an array
121-
*
122-
* If `items` is present, and its annotation result is a number, validation succeeds if every
123-
* instance element at an index greater than that number validates against "additionalItems".
124-
* Otherwise, if `items` is absent or its annotation result is the boolean true,
125-
* `additionalItems` is ignored.
126-
*
127-
* [Specification](https://json-schema.org/draft/2019-09/draft-handrews-json-schema-02#additionalItems)
128-
*/
129-
additionalItems?: SchemaNode;
130117
additionalProperties?: SchemaNode;
131118
allOf?: SchemaNode[];
132119
anyOf?: SchemaNode[];
@@ -136,9 +123,9 @@ export interface SchemaNode extends SchemaNodeMethodsType {
136123
else?: SchemaNode;
137124
if?: SchemaNode;
138125
/**
139-
* # Items-array schema for all drafts
126+
* # Items-array schema - for all drafts
140127
*
141-
* - for drafts prior 2020-12 items[]-schema stored as `prefixItems`
128+
* - for drafts prior 2020-12 `schema.items[]`-schema stored as `node.prefixItems`
142129
*
143130
* Validation succeeds if each element of the instance validates against the schema at the
144131
* same position, if any.
@@ -154,9 +141,9 @@ export interface SchemaNode extends SchemaNodeMethodsType {
154141
*/
155142
prefixItems?: SchemaNode[];
156143
/**
157-
* # Items-object schema
144+
* # Items-object schema for additional array item - for all drafts
158145
*
159-
* - ⚠️ For drafts < 2020-12, `additionalItems` is still used and must be referenced by `node.additionalItems`
146+
* - for drafts prior 2020-12 `schema.additionalItems` object-schema stored as `node.items`
160147
*
161148
* Validation succeeds if each element of the instance not covered by `prefixItems` validates
162149
* against this schema.
@@ -167,6 +154,7 @@ export interface SchemaNode extends SchemaNodeMethodsType {
167154
*
168155
* [Docs](https://www.learnjsonschema.com/2020-12/applicator/items/)
169156
* | [Examples](https://json-schema.org/understanding-json-schema/reference/array#items)
157+
* | [AdditionalItems Specification](https://json-schema.org/draft/2019-09/draft-handrews-json-schema-02#additionalItems)
170158
*/
171159
items?: SchemaNode;
172160
not?: SchemaNode;

src/draft2019-09/keywords/additionalItems.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const additionalItemsKeyword: Keyword = {
99
keyword: "additionalItems",
1010
order: -10,
1111
parse: parseAdditionalItems,
12-
addResolve: (node: SchemaNode) => node.additionalItems != null,
12+
addResolve: (node: SchemaNode) => node.items != null,
1313
resolve: additionalItemsResolver,
1414
addValidate: ({ schema }) =>
1515
schema.additionalItems != null && schema.additionalItems !== true && Array.isArray(schema.items),
@@ -20,7 +20,7 @@ export const additionalItemsKeyword: Keyword = {
2020
export function parseAdditionalItems(node: SchemaNode) {
2121
const { schema, evaluationPath, schemaLocation } = node;
2222
if ((isObject(schema.additionalItems) || schema.additionalItems === true) && Array.isArray(schema.items)) {
23-
node.additionalItems = node.compileSchema(
23+
node.items = node.compileSchema(
2424
schema.additionalItems,
2525
`${evaluationPath}/additionalItems`,
2626
`${schemaLocation}/additionalItems`
@@ -32,7 +32,7 @@ function additionalItemsResolver({ node, key, data }: JsonSchemaResolverParams)
3232
if (Array.isArray(data)) {
3333
// @attention: items, etc should already have been tried
3434
const value = getValue(data, key);
35-
const { node: childNode, error } = node.additionalItems.reduceNode(value);
35+
const { node: childNode, error } = node.items.reduceNode(value);
3636
return childNode ?? error;
3737
}
3838
}
@@ -51,8 +51,8 @@ function validateAdditionalItems({ node, data, pointer, path }: JsonSchemaValida
5151
const errors: ValidationResult[] = [];
5252
for (let i = startIndex; i < data.length; i += 1) {
5353
const item = data[i];
54-
if (node.additionalItems) {
55-
const validationResult = validateNode(node.additionalItems, item, `${pointer}/${i}`, path);
54+
if (node.items) {
55+
const validationResult = validateNode(node.items, item, `${pointer}/${i}`, path);
5656
validationResult && errors.push(...validationResult);
5757
} else if (schema.additionalItems === false) {
5858
errors.push(

src/draft2019-09/keywords/items.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ export const itemsKeyword: Keyword = {
1414
};
1515

1616
function itemsResolver({ node, key }: JsonSchemaResolverParams) {
17+
if (node.prefixItems?.[key as number]) {
18+
return node.prefixItems[key as number];
19+
}
1720
if (node.items) {
1821
return node.items;
1922
}
20-
if (node.prefixItems[key as number]) {
21-
return node.prefixItems[key as number];
22-
}
2323
}
2424

2525
export function parseItems(node: SchemaNode) {

src/draft2019-09/methods/getChildSchemaSelection.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ describe("getChildSelection (2019)", () => {
167167

168168
assert(!isJsonError(result));
169169
assert.deepEqual(result.length, 1);
170+
170171
assert.deepEqual(
171172
result.map((n) => n.schema),
172173
[{ type: "string" }]

src/draft2019-09/methods/getChildSelection.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ export function getChildSelection(node: SchemaNode, property: string | number):
2323
}
2424

2525
// array.items[] exceeded (or undefined), but additionalItems specified
26-
if (node.additionalItems && node.items == null) {
26+
if (node.schema.additionalItems) {
2727
// we fallback to a string if no schema is defined - might be subject for configuration
28-
// @ts-expect-error boolean schema
29-
if (node.additionalItems.schema === true) {
28+
if (node.schema.additionalItems === true) {
3029
return [node.compileSchema({ type: "string" })];
3130
}
32-
return [node.additionalItems.resolveRef()];
31+
return [node.items.resolveRef()];
3332
}
3433

3534
// array.items[] exceeded

src/draft2019-09/methods/getData.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptio
273273
// when there are no array-items are defined
274274
if (schema.items == null) {
275275
// => all items are additionalItems
276-
if (node.additionalItems) {
276+
if (node.items) {
277277
const itemCount = Math.max(minItems, d.length);
278278
for (let i = 0; i < itemCount; i += 1) {
279-
d[i] = node.additionalItems.getData(d[i], opts);
279+
d[i] = node.items.getData(d[i], opts);
280280
}
281281
}
282282
return d || [];
@@ -289,7 +289,7 @@ const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptio
289289
// build defined set of items
290290
const length = Math.max(minItems ?? 0, node.prefixItems.length);
291291
for (let i = 0; i < length; i += 1) {
292-
const childNode = node.prefixItems[i] ?? node.additionalItems;
292+
const childNode = node.prefixItems[i] ?? node.items;
293293
if ((childNode && canResolveRef(childNode, opts)) || input[i] !== undefined) {
294294
const result = childNode.getData(d[i] == null ? template[i] : d[i], opts);
295295
if (result !== undefined) {
@@ -306,8 +306,7 @@ const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptio
306306
}
307307

308308
// build data from items-definition
309-
// @ts-expect-error asd
310-
if ((node.items && canResolveRef(node.items, opts)) || data?.length > 0) {
309+
if ((node.items && canResolveRef(node.items, opts)) || (Array.isArray(data) && data?.length > 0)) {
311310
// @attention this should disable cache or break intended behaviour as we reset it after loop
312311
// intention: reset cache after each property. last call will add counters
313312
const cache = { ...opts.cache };

src/mergeNode.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ export function mergeNode(a: SchemaNode, b?: SchemaNode, ...omit: string[]): Sch
8989
reducers: a.reducers.concat(b.reducers).filter(removeDuplicates).sort(sortCb),
9090
validators: a.validators.concat(b.validators).filter(removeDuplicates).sort(sortCb),
9191

92-
additionalItems: mergeNode(a.additionalItems, b.additionalItems),
9392
additionalProperties: mergeNode(a.additionalProperties, b.additionalProperties),
9493
contains: mergeNode(a.contains, b.contains),
9594
if: mergeNode(a.if, b.if),

src/methods/toSchemaNodes.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export function toSchemaNodes(node: SchemaNode | unknown, nodeList: SchemaNode[]
2020
nodeList.push(node);
2121

2222
eachProperty(nodeList, node.$defs);
23-
node.additionalItems && toSchemaNodes(node.additionalItems, nodeList);
2423
node.additionalProperties && toSchemaNodes(node.additionalProperties, nodeList);
2524
eachItem(nodeList, node.allOf);
2625
eachItem(nodeList, node.anyOf);

0 commit comments

Comments
 (0)