Skip to content

Commit a38a1a6

Browse files
committed
dist: update
1 parent 13fb66e commit a38a1a6

File tree

14 files changed

+135
-103
lines changed

14 files changed

+135
-103
lines changed

dist/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export { isReduceable } from "./src/SchemaNode";
1818
export { isJsonError, isSchemaNode } from "./src/types";
1919
export { extendDraft, addKeywords } from "./src/Draft";
2020
export { mergeNode } from "./src/mergeNode";
21+
export { mergeSchema } from "./src/utils/mergeSchema";
2122
export { remotes } from "./remotes";

dist/jsonSchemaLibrary.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/module/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ export { isReduceable } from "./src/SchemaNode";
1515
export { isJsonError, isSchemaNode } from "./src/types";
1616
export { extendDraft, addKeywords } from "./src/Draft";
1717
export { mergeNode } from "./src/mergeNode";
18+
export { mergeSchema } from "./src/utils/mergeSchema";
1819
// remotes
1920
export { remotes } from "./remotes";

dist/module/src/compileSchema.reduceSchema.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ describe("compileSchema : reduceNode", () => {
107107
properties: { one: { type: "string" }, two: { type: "string" }, three: { type: "number" } }
108108
});
109109
});
110+
it("should NOT merge dependency when it is not defined", () => {
111+
const node = compileSchema({
112+
type: "object",
113+
properties: { one: { type: "string" } },
114+
dependencies: { one: ["two"], two: { type: "number" } }
115+
});
116+
const { node: reduced } = node.reduceNode({});
117+
console.log("reudced schema", reduced.schema);
118+
assert.deepEqual(reduced.schema, {
119+
type: "object",
120+
properties: { one: { type: "string" } }
121+
});
122+
});
110123
it("should NOT add dynamic schema if no data matches dependency", () => {
111124
const { node } = compileSchema({
112125
$ref: "#/$defs/schema",

dist/module/src/draft2019-09/methods/getData.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,18 @@ const TYPE = {
160160
}
161161
});
162162
}
163-
if (isObject(node.dependencies)) {
164-
Object.keys(node.dependencies).forEach((propertyName) => {
165-
const propertyValue = node.dependencies[propertyName];
163+
if (isObject(node.dependentRequired)) {
164+
Object.keys(node.dependentRequired).forEach((propertyName) => {
165+
const propertyValue = node.dependentRequired[propertyName];
166166
const hasValue = getValue(d, propertyName) !== undefined;
167-
if (hasValue && Array.isArray(propertyValue)) {
167+
if (hasValue) {
168168
propertyValue.forEach((addProperty) => {
169169
const { node: propertyNode } = node.getNodeChild(addProperty, d);
170170
if (propertyNode) {
171171
d[addProperty] = propertyNode.getData(getValue(d, addProperty), opts);
172172
}
173173
});
174174
}
175-
else if (d[propertyName] !== undefined && isSchemaNode(propertyValue)) {
176-
const dependencyData = propertyValue.getData(data !== null && data !== void 0 ? data : d, opts);
177-
Object.assign(d, dependencyData);
178-
}
179175
// if false and removeInvalidData => remove from data
180176
});
181177
}

dist/module/src/keywords/$ref.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ export function parseRef(node) {
6464
}
6565
}
6666
export function reduceRef({ node, data, key, pointer, path }) {
67+
if (node == null) {
68+
return;
69+
}
6770
const resolvedNode = node.resolveRef({ pointer, path });
71+
if (resolvedNode == null) {
72+
return;
73+
}
6874
if (resolvedNode.schemaId === node.schemaId) {
6975
return resolvedNode;
7076
}
Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { isSchemaNode } from "../types";
2-
import { getValue } from "../utils/getValue";
32
import { isObject } from "../utils/isObject";
4-
import { validateNode } from "../validateNode";
53
import { mergeNode } from "../mergeNode";
4+
import { hasProperty } from "../utils/hasProperty";
5+
import { validateDependentRequired } from "./dependentRequired";
6+
import { validateDependentSchemas } from "./dependentSchemas";
67
export const dependenciesKeyword = {
78
id: "dependencies",
89
keyword: "dependencies",
910
parse: parseDependencies,
10-
addReduce: (node) => node.dependencies != null,
11+
order: -9,
12+
addReduce: (node) => node.schema.dependencies != null,
1113
reduce: reduceDependencies,
12-
addValidate: (node) => node.dependencies != null,
14+
addValidate: (node) => node.schema.dependencies != null,
1315
validate: validateDependencies
1416
};
1517
export function parseDependencies(node) {
@@ -18,40 +20,63 @@ export function parseDependencies(node) {
1820
return;
1921
}
2022
const schemas = Object.keys(dependencies);
21-
if (schemas.length === 0) {
22-
return;
23-
}
24-
node.dependencies = {};
2523
schemas.forEach((property) => {
24+
var _a, _b;
2625
const schema = dependencies[property];
2726
if (isObject(schema) || typeof schema === "boolean") {
28-
node.dependencies[property] = node.compileSchema(
29-
// @ts-expect-error boolean schema
30-
schema, `${node.spointer}/dependencies/${property}`, `${node.schemaId}/dependencies/${property}`);
27+
node.dependentSchemas = (_a = node.dependentSchemas) !== null && _a !== void 0 ? _a : {};
28+
node.dependentSchemas[property] = node.compileSchema(schema, `${node.spointer}/dependencies/${property}`, `${node.schemaId}/dependencies/${property}`);
3129
}
32-
else if (Array.isArray(schema)) {
33-
node.dependencies[property] = schema;
30+
else {
31+
node.dependentRequired = (_b = node.dependentRequired) !== null && _b !== void 0 ? _b : {};
32+
node.dependentRequired[property] = schema;
3433
}
3534
});
3635
}
3736
export function reduceDependencies({ node, data, key, path }) {
3837
var _a;
39-
if (!isObject(data) || node.dependencies == null) {
38+
if (!isObject(data)) {
4039
// @todo remove dependentSchemas
4140
return node;
4241
}
42+
if (node.dependentRequired == null && node.dependentSchemas == null) {
43+
return node;
44+
}
4345
let workingNode = node.compileSchema(node.schema, node.spointer, node.schemaId);
44-
const { dependencies } = node;
4546
let required = (_a = workingNode.schema.required) !== null && _a !== void 0 ? _a : [];
46-
Object.keys(dependencies).forEach((propertyName) => {
47-
if (isSchemaNode(dependencies[propertyName])) {
48-
const reducedDependency = dependencies[propertyName].reduceNode(data, { key, path }).node;
47+
if (node.dependentRequired) {
48+
Object.keys(node.dependentRequired).forEach((propertyName) => {
49+
if (!hasProperty(data, propertyName) && !required.includes(propertyName)) {
50+
console.log(propertyName, "abort", required);
51+
return;
52+
}
53+
if (node.dependentRequired[propertyName] == null) {
54+
return;
55+
}
56+
required.push(...node.dependentRequired[propertyName]);
57+
});
58+
}
59+
if (node.dependentSchemas) {
60+
Object.keys(node.dependentSchemas).forEach((propertyName) => {
61+
if (!hasProperty(data, propertyName) && !required.includes(propertyName)) {
62+
return true;
63+
}
64+
const dependency = node.dependentSchemas[propertyName];
65+
if (!isSchemaNode(dependency)) {
66+
return true;
67+
}
68+
// @note pass on updated required-list to resolve nested dependencies. This is currently supported,
69+
// but probably not how json-schema spec defines this behaviour (resolve only within sub-schema)
70+
const reducedDependency = { ...dependency, schema: { ...dependency.schema, required } }.reduceNode(data, {
71+
key,
72+
path
73+
}).node;
4974
workingNode = mergeNode(workingNode, reducedDependency);
50-
}
51-
else if (Array.isArray(dependencies[propertyName]) && data[propertyName] !== undefined) {
52-
required.push(...dependencies[propertyName]);
53-
}
54-
});
75+
if (workingNode.schema.required) {
76+
required.push(...workingNode.schema.required);
77+
}
78+
});
79+
}
5580
if (workingNode === node) {
5681
return node;
5782
}
@@ -62,43 +87,25 @@ export function reduceDependencies({ node, data, key, path }) {
6287
return workingNode;
6388
}
6489
required = workingNode.schema.required ? workingNode.schema.required.concat(...required) : required;
90+
required = required.filter((r, index, list) => list.indexOf(r) === index);
91+
workingNode = mergeNode(workingNode, workingNode, "dependencies");
6592
return workingNode.compileSchema({ ...workingNode.schema, required }, workingNode.spointer, workingNode.schemaId);
6693
}
6794
function validateDependencies({ node, data, pointer, path }) {
95+
var _a;
6896
if (!isObject(data)) {
6997
return undefined;
7098
}
71-
const errors = [];
72-
const dependencies = node.dependencies;
73-
Object.keys(data).forEach((property) => {
74-
const propertyValue = dependencies[property];
75-
if (propertyValue === undefined) {
76-
return;
77-
}
78-
// @draft >= 6 boolean schema
79-
if (propertyValue === true) {
80-
return;
81-
}
82-
if (propertyValue === false) {
83-
errors.push(node.createError("missing-dependency-error", { pointer, schema: node.schema, value: data }));
84-
return;
85-
}
86-
if (Array.isArray(propertyValue)) {
87-
propertyValue
88-
.filter((dependency) => getValue(data, dependency) === undefined)
89-
.forEach((missingProperty) => errors.push(node.createError("missing-dependency-error", {
90-
missingProperty,
91-
pointer: `${pointer}/${missingProperty}`,
92-
schema: node.schema,
93-
value: data
94-
})));
95-
}
96-
else if (isSchemaNode(propertyValue)) {
97-
errors.push(...validateNode(propertyValue, data, pointer, path));
98-
}
99-
else {
100-
throw new Error(`Invalid dependency definition for ${pointer}/${property}. Must be string[] or schema`);
99+
let errors;
100+
if (node.dependentRequired) {
101+
errors = (_a = validateDependentRequired({ node, data, pointer, path })) !== null && _a !== void 0 ? _a : [];
102+
}
103+
if (node.dependentSchemas) {
104+
const schemaErrors = validateDependentSchemas({ node, data, pointer, path });
105+
if (schemaErrors) {
106+
errors = errors !== null && errors !== void 0 ? errors : [];
107+
errors.push(...schemaErrors);
101108
}
102-
});
109+
}
103110
return errors;
104111
}

dist/module/src/keywords/dependentRequired.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,51 @@ import { isObject } from "../utils/isObject";
22
export const dependentRequiredKeyword = {
33
id: "dependentRequired",
44
keyword: "dependentRequired",
5+
parse: parseDependentRequired,
56
addValidate: (node) => isObject(node.schema.dependentRequired),
67
validate: validateDependentRequired
78
};
9+
export function parseDependentRequired(node) {
10+
var _a;
11+
if (!isObject(node.schema.dependentRequired)) {
12+
return;
13+
}
14+
node.dependentRequired = (_a = node.schema.dependentRequired) !== null && _a !== void 0 ? _a : {};
15+
}
816
export function validateDependentRequired({ node, data, pointer = "#" }) {
917
if (!isObject(data)) {
1018
return undefined;
1119
}
12-
const { schema } = node;
13-
const dependentRequired = schema.dependentRequired;
20+
const { dependentRequired } = node;
1421
const errors = [];
15-
Object.keys(data).forEach((property) => {
16-
const dependencies = dependentRequired[property];
17-
// @draft >= 6 boolean schema
18-
if (dependencies === true) {
19-
return;
20-
}
21-
if (dependencies === false) {
22-
errors.push(node.createError("missing-dependency-error", { pointer, schema, value: data }));
23-
return;
24-
}
25-
if (!Array.isArray(dependencies)) {
26-
return;
27-
}
28-
for (let i = 0, l = dependencies.length; i < l; i += 1) {
29-
if (data[dependencies[i]] === undefined) {
30-
errors.push(node.createError("missing-dependency-error", {
31-
missingProperty: dependencies[i],
32-
pointer,
33-
schema,
34-
value: data
35-
}));
22+
if (dependentRequired) {
23+
Object.keys(data).forEach((property) => {
24+
const dependencies = dependentRequired[property];
25+
// @draft >= 6 boolean schema
26+
// @ts-expect-error boolean schema
27+
if (dependencies === true) {
28+
return;
29+
}
30+
// @ts-expect-error boolean schema
31+
if (dependencies === false) {
32+
// @ts-expect-error boolean schema
33+
errors.push(node.createError("missing-dependency-error", { pointer, schema, value: data }));
34+
return;
3635
}
37-
}
38-
});
36+
if (!Array.isArray(dependencies)) {
37+
return;
38+
}
39+
for (let i = 0, l = dependencies.length; i < l; i += 1) {
40+
if (data[dependencies[i]] === undefined) {
41+
errors.push(node.createError("missing-dependency-error", {
42+
missingProperty: dependencies[i],
43+
pointer,
44+
schema: node.schema,
45+
value: data
46+
}));
47+
}
48+
}
49+
});
50+
}
3951
return errors;
4052
}

dist/module/src/keywords/dependentSchemas.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function reduceDependentSchemas({ node, data }) {
6262
}
6363
export function validateDependentSchemas({ node, data, pointer, path }) {
6464
const { schema, dependentSchemas } = node;
65-
if (!isObject(data)) {
65+
if (!isObject(data) || dependentSchemas == null) {
6666
return undefined;
6767
}
6868
const errors = [];

dist/module/src/methods/getData.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,18 @@ const TYPE = {
152152
}
153153
});
154154
}
155-
if (isObject(node.dependencies)) {
156-
Object.keys(node.dependencies).forEach((propertyName) => {
157-
const propertyValue = node.dependencies[propertyName];
155+
if (isObject(node.dependentRequired)) {
156+
Object.keys(node.dependentRequired).forEach((propertyName) => {
157+
const propertyValue = node.dependentRequired[propertyName];
158158
const hasValue = getValue(d, propertyName) !== undefined;
159-
if (hasValue && Array.isArray(propertyValue)) {
159+
if (hasValue) {
160160
propertyValue.forEach((addProperty) => {
161161
const { node: propertyNode } = node.getNodeChild(addProperty, d);
162162
if (propertyNode) {
163163
d[addProperty] = propertyNode.getData(getValue(d, addProperty), opts);
164164
}
165165
});
166166
}
167-
else if (d[propertyName] !== undefined && isSchemaNode(propertyValue)) {
168-
const dependencyData = propertyValue.getData(data !== null && data !== void 0 ? data : d, opts);
169-
Object.assign(d, dependencyData);
170-
}
171167
// if false and removeInvalidData => remove from data
172168
});
173169
}

0 commit comments

Comments
 (0)