Skip to content

Commit 1daede5

Browse files
committed
refactor: draft-2020-12 use schemaNode for items boolean schema
1 parent a90ec68 commit 1daede5

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

src/draft2019-09/keywords/additionalItems.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isObject } from "../../utils/isObject";
21
import { Keyword, JsonSchemaResolverParams, JsonSchemaValidatorParams, ValidationResult } from "../../Keyword";
32
import { SchemaNode } from "../../types";
43
import { getValue } from "../../utils/getValue";
@@ -19,7 +18,7 @@ export const additionalItemsKeyword: Keyword = {
1918
// must come as last resolver
2019
export function parseAdditionalItems(node: SchemaNode) {
2120
const { schema, evaluationPath, schemaLocation } = node;
22-
if ((isObject(schema.additionalItems) || schema.additionalItems === true) && Array.isArray(schema.items)) {
21+
if (schema.additionalItems != null && Array.isArray(schema.items)) {
2322
node.items = node.compileSchema(
2423
schema.additionalItems,
2524
`${evaluationPath}/additionalItems`,
@@ -39,7 +38,7 @@ function additionalItemsResolver({ node, key, data }: JsonSchemaResolverParams)
3938

4039
function validateAdditionalItems({ node, data, pointer, path }: JsonSchemaValidatorParams) {
4140
const { schema } = node;
42-
if (!Array.isArray(data) || data.length === 0) {
41+
if (!Array.isArray(data) || data.length === 0 || node.items == null) {
4342
// - no items to validate
4443
return;
4544
}
@@ -51,10 +50,8 @@ function validateAdditionalItems({ node, data, pointer, path }: JsonSchemaValida
5150
const errors: ValidationResult[] = [];
5251
for (let i = startIndex; i < data.length; i += 1) {
5352
const item = data[i];
54-
if (node.items) {
55-
const validationResult = validateNode(node.items, item, `${pointer}/${i}`, path);
56-
validationResult && errors.push(...validationResult);
57-
} else if (schema.additionalItems === false) {
53+
// @ts-expect-error boolean-schema
54+
if (node.items?.schema === false) {
5855
errors.push(
5956
node.createError("additional-items-error", {
6057
key: i,
@@ -63,6 +60,10 @@ function validateAdditionalItems({ node, data, pointer, path }: JsonSchemaValida
6360
schema
6461
})
6562
);
63+
// @ts-expect-error boolean-schema
64+
} else if (node.items.schema !== true) {
65+
const validationResult = validateNode(node.items, item, `${pointer}/${i}`, path);
66+
validationResult && errors.push(...validationResult);
6667
}
6768
}
6869
return errors;

src/draft2019-09/keywords/items.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Keyword, JsonSchemaResolverParams, JsonSchemaValidatorParams, ValidationResult } from "../../Keyword";
22
import { SchemaNode } from "../../types";
3-
import { isObject } from "../../utils/isObject";
43
import { validateNode } from "../../validateNode";
54

65
export const itemsKeyword: Keyword = {
@@ -24,17 +23,17 @@ function itemsResolver({ node, key }: JsonSchemaResolverParams) {
2423

2524
export function parseItems(node: SchemaNode) {
2625
const { schema, evaluationPath } = node;
27-
if (isObject(schema.items)) {
26+
if (Array.isArray(schema.items)) {
27+
node.prefixItems = schema.items.map((itemSchema, index) =>
28+
node.compileSchema(itemSchema, `${evaluationPath}/items/${index}`, `${node.schemaLocation}/items/${index}`)
29+
);
30+
} else if (schema.items != null) {
2831
const propertyNode = node.compileSchema(
2932
schema.items,
3033
`${evaluationPath}/items`,
3134
`${node.schemaLocation}/items`
3235
);
3336
node.items = propertyNode;
34-
} else if (Array.isArray(schema.items)) {
35-
node.prefixItems = schema.items.map((itemSchema, index) =>
36-
node.compileSchema(itemSchema, `${evaluationPath}/items/${index}`, `${node.schemaLocation}/items/${index}`)
37-
);
3837
}
3938
}
4039

@@ -64,7 +63,8 @@ function validateItems({ node, data, pointer = "#", path }: JsonSchemaValidatorP
6463
return errors;
6564
}
6665

67-
if (node.items) {
66+
// @ts-expect-error boolean schema
67+
if (node.items.schema !== true) {
6868
for (let i = 0; i < data.length; i += 1) {
6969
const itemData = data[i];
7070
const result = validateNode(node.items, itemData, `${pointer}/${i}`, path);

src/getNodeChild.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ export function getNodeChild(
3636
for (const resolver of node.resolvers) {
3737
const schemaNode = resolver({ data, key, node });
3838
if (isSchemaNode(schemaNode)) {
39+
// @ts-expect-error boolean schema
40+
if (schemaNode.schema === true) {
41+
const generatedNode = schemaNode.compileSchema(schemaNode.createSchema(getValue(data, key)));
42+
return { node: generatedNode, error: undefined };
43+
}
44+
3945
return { node: schemaNode, error: undefined };
4046
}
4147
if (isJsonError(schemaNode)) {

src/keywords/items.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Keyword, JsonSchemaResolverParams, JsonSchemaValidatorParams, ValidationResult } from "../Keyword";
22
import { SchemaNode } from "../types";
3-
import { isObject } from "../utils/isObject";
43
import { validateNode } from "../validateNode";
54

65
export const itemsKeyword: Keyword = {
@@ -24,7 +23,7 @@ function itemsResolver({ node, key }: JsonSchemaResolverParams) {
2423

2524
export function parseItems(node: SchemaNode) {
2625
const { schema, evaluationPath } = node;
27-
if (isObject(schema.items)) {
26+
if (schema.items != null) {
2827
const propertyNode = node.compileSchema(
2928
schema.items,
3029
`${evaluationPath}/items`,

0 commit comments

Comments
 (0)