json-schema-library 11.4.0 → 11.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -56
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/jlib.js +1 -1
- package/package.json +11 -1
- package/src/keywords/type.test.ts +13 -0
- package/src/utils/mergeSchema.test.ts +37 -0
- package/src/utils/mergeSchema.ts +24 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-schema-library",
|
|
3
|
-
"version": "11.4.
|
|
3
|
+
"version": "11.4.1",
|
|
4
4
|
"description": "Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation",
|
|
5
5
|
"types": "./dist/index.d.cts",
|
|
6
6
|
"exports": {
|
|
@@ -18,6 +18,16 @@
|
|
|
18
18
|
},
|
|
19
19
|
"./package.json": "./package.json"
|
|
20
20
|
},
|
|
21
|
+
"typesVersions": {
|
|
22
|
+
"*": {
|
|
23
|
+
"formats": [
|
|
24
|
+
"./dist/formats.d.cts"
|
|
25
|
+
],
|
|
26
|
+
"remotes": [
|
|
27
|
+
"./dist/remotes.d.cts"
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
},
|
|
21
31
|
"scripts": {
|
|
22
32
|
"coverage": "nyc npm run test --reporter=lcov",
|
|
23
33
|
"dist": "tsdown -f esm -f cjs --minify --report --attw --unused; pnpm run dist:iife;",
|
|
@@ -20,3 +20,16 @@ describe("keyword : type : validation", () => {
|
|
|
20
20
|
});
|
|
21
21
|
});
|
|
22
22
|
});
|
|
23
|
+
|
|
24
|
+
describe("keyword : type : reduce", () => {
|
|
25
|
+
it("should reduce array-type to the last matching data-type", () => {
|
|
26
|
+
const { node } = compileSchema({ type: ["null", "integer"] }).reduceNode(123);
|
|
27
|
+
assert.equal(node?.type, "integer");
|
|
28
|
+
assert.equal(node?.schema.type, "integer");
|
|
29
|
+
});
|
|
30
|
+
it("should reduce array-type to the first matching data-type", () => {
|
|
31
|
+
const { node } = compileSchema({ type: ["null", "integer"] }).reduceNode(null);
|
|
32
|
+
assert.equal(node?.type, "null");
|
|
33
|
+
assert.equal(node?.schema.type, "null");
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -78,4 +78,41 @@ describe("mergeSchema", () => {
|
|
|
78
78
|
);
|
|
79
79
|
assert.deepEqual(schema.items.anyOf, [{ type: "string" }, { type: "number" }]);
|
|
80
80
|
});
|
|
81
|
+
|
|
82
|
+
describe("type", () => {
|
|
83
|
+
it("should return last type if they conflict", () => {
|
|
84
|
+
const schema = mergeSchema({ type: "array" }, { type: "integer" });
|
|
85
|
+
assert.deepEqual(schema.type, "integer");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("should not merge mixed data types with string first", () => {
|
|
89
|
+
const schema = mergeSchema({ type: "array" }, { type: ["array", "object"] });
|
|
90
|
+
assert.deepEqual(schema.type, "array");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("should not merge mixed data types with string second", () => {
|
|
94
|
+
const schema = mergeSchema({ type: ["array", "object"] }, { type: "array" });
|
|
95
|
+
assert.deepEqual(schema.type, "array");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("should merge mixed data types if they have no type in common", () => {
|
|
99
|
+
const schema = mergeSchema({ type: ["array", "object"] }, { type: "integer" });
|
|
100
|
+
assert.deepEqual(schema.type, ["array", "object", "integer"]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should merge return last type if they do not match", () => {
|
|
104
|
+
const schema = mergeSchema({ type: "object" }, { type: "array" });
|
|
105
|
+
assert.deepEqual(schema.type, "array");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("should merge array types", () => {
|
|
109
|
+
const schema = mergeSchema({ type: ["object"] }, { type: ["array"] });
|
|
110
|
+
assert.deepEqual(schema.type, ["object", "array"]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("should merge array types without duplicated", () => {
|
|
114
|
+
const schema = mergeSchema({ type: ["integer", "object"] }, { type: ["object", "array"] });
|
|
115
|
+
assert.deepEqual(schema.type, ["integer", "object", "array"]);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
81
118
|
});
|
package/src/utils/mergeSchema.ts
CHANGED
|
@@ -32,7 +32,10 @@ export function mergeSchema2(a: unknown, b: unknown, property?: string): unknown
|
|
|
32
32
|
return newObject;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
const aIsArray = Array.isArray(a);
|
|
36
|
+
const bIsArray = Array.isArray(b);
|
|
37
|
+
|
|
38
|
+
if (aIsArray && bIsArray) {
|
|
36
39
|
if (property === "required" || property === "anyOf") {
|
|
37
40
|
return a.concat(b).filter((item, index, array) => array.indexOf(item) === index);
|
|
38
41
|
}
|
|
@@ -66,11 +69,29 @@ export function mergeSchema2(a: unknown, b: unknown, property?: string): unknown
|
|
|
66
69
|
return [...result, ...append].filter((item, index, array) => array.indexOf(item) === index);
|
|
67
70
|
}
|
|
68
71
|
|
|
69
|
-
|
|
72
|
+
// mixed data-type for type keyword
|
|
73
|
+
if (property === "type" && (aIsArray || bIsArray)) {
|
|
74
|
+
// we merge to the specific type
|
|
75
|
+
if (aIsArray && a.includes(b)) {
|
|
76
|
+
return b;
|
|
77
|
+
}
|
|
78
|
+
if (bIsArray && b.includes(a)) {
|
|
79
|
+
return a;
|
|
80
|
+
}
|
|
81
|
+
// extend the types if they do not match
|
|
82
|
+
if (aIsArray) {
|
|
83
|
+
return [...a, b];
|
|
84
|
+
}
|
|
85
|
+
if (bIsArray) {
|
|
86
|
+
return [...b, a];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (bIsArray) {
|
|
70
91
|
return b;
|
|
71
92
|
}
|
|
72
93
|
|
|
73
|
-
if (
|
|
94
|
+
if (aIsArray) {
|
|
74
95
|
return a;
|
|
75
96
|
}
|
|
76
97
|
|