z-schema 7.0.0 → 7.0.6
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 +7 -11
- package/bin/z-schema +0 -0
- package/cjs/index.d.ts +192 -117
- package/cjs/index.js +949 -998
- package/{src/FormatValidators.ts → dist/format-validators.js} +97 -65
- package/dist/index.js +1 -1
- package/dist/json-schema.js +40 -0
- package/dist/{JsonValidation.js → json-validation.js} +75 -69
- package/dist/{Report.js → report.js} +35 -45
- package/dist/schema-cache.js +109 -0
- package/dist/schema-compiler.js +255 -0
- package/dist/{SchemaValidation.js → schema-validator.js} +153 -149
- package/dist/types/{Errors.d.ts → errors.d.ts} +2 -0
- package/dist/types/format-validators.d.ts +10 -0
- package/dist/types/index.d.ts +10 -1
- package/dist/types/json-schema.d.ts +50 -0
- package/dist/types/json-validation.d.ts +7 -0
- package/dist/types/{Report.d.ts → report.d.ts} +22 -23
- package/dist/types/schema-cache.d.ts +17 -0
- package/dist/types/schema-compiler.d.ts +16 -0
- package/dist/types/schema-validator.d.ts +10 -0
- package/dist/types/utils/array.d.ts +2 -0
- package/dist/types/utils/clone.d.ts +2 -0
- package/dist/types/utils/json.d.ts +7 -0
- package/dist/types/utils/symbols.d.ts +2 -0
- package/dist/types/utils/unicode.d.ts +14 -0
- package/dist/types/utils/uri.d.ts +4 -0
- package/dist/types/utils/what-is.d.ts +3 -0
- package/dist/types/z-schema.d.ts +75 -0
- package/dist/utils/array.js +27 -0
- package/dist/utils/clone.js +61 -0
- package/dist/utils/json.js +59 -0
- package/dist/utils/symbols.js +2 -0
- package/dist/utils/unicode.js +45 -0
- package/dist/utils/uri.js +15 -0
- package/dist/utils/what-is.js +29 -0
- package/dist/{ZSchema.js → z-schema.js} +66 -77
- package/package.json +8 -4
- package/src/{Errors.ts → errors.ts} +4 -0
- package/src/format-validators.ts +191 -0
- package/src/index.ts +12 -1
- package/src/json-schema.ts +97 -0
- package/src/{JsonValidation.ts → json-validation.ts} +137 -127
- package/src/{Report.ts → report.ts} +60 -70
- package/src/schema-cache.ts +122 -0
- package/src/schema-compiler.ts +300 -0
- package/src/{SchemaValidation.ts → schema-validator.ts} +213 -215
- package/src/utils/array.ts +29 -0
- package/src/utils/clone.ts +63 -0
- package/src/utils/json.ts +74 -0
- package/src/utils/symbols.ts +3 -0
- package/src/utils/unicode.ts +43 -0
- package/src/utils/uri.ts +18 -0
- package/src/utils/what-is.ts +46 -0
- package/src/{ZSchema.ts → z-schema.ts} +108 -113
- package/umd/ZSchema.js +949 -998
- package/umd/ZSchema.min.js +1 -1
- package/dist/FormatValidators.js +0 -136
- package/dist/SchemaCache.js +0 -173
- package/dist/SchemaCompilation.js +0 -259
- package/dist/Utils.js +0 -266
- package/dist/types/FormatValidators.d.ts +0 -12
- package/dist/types/JsonValidation.d.ts +0 -37
- package/dist/types/SchemaCache.d.ts +0 -26
- package/dist/types/SchemaCompilation.d.ts +0 -1
- package/dist/types/SchemaValidation.d.ts +0 -6
- package/dist/types/Utils.d.ts +0 -64
- package/dist/types/ZSchema.d.ts +0 -97
- package/src/SchemaCache.ts +0 -189
- package/src/SchemaCompilation.ts +0 -293
- package/src/Utils.ts +0 -286
- /package/dist/{Errors.js → errors.js} +0 -0
- /package/dist/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
- /package/dist/schemas/{schema.json → draft-04-schema.json} +0 -0
- /package/src/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
- /package/src/schemas/{schema.json → draft-04-schema.json} +0 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { areEqual } from './json.js';
|
|
2
|
+
|
|
3
|
+
export const isUniqueArray = <T>(arr: T[], indexes?: number[]): boolean => {
|
|
4
|
+
let i;
|
|
5
|
+
let j;
|
|
6
|
+
const l = arr.length;
|
|
7
|
+
for (i = 0; i < l; i++) {
|
|
8
|
+
for (j = i + 1; j < l; j++) {
|
|
9
|
+
if (areEqual(arr[i], arr[j])) {
|
|
10
|
+
if (indexes) {
|
|
11
|
+
indexes.push(i, j);
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return true;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const difference = (bigSet: any[], subSet: any[]) => {
|
|
21
|
+
const arr = [];
|
|
22
|
+
let idx = bigSet.length;
|
|
23
|
+
while (idx--) {
|
|
24
|
+
if (subSet.indexOf(bigSet[idx]) === -1) {
|
|
25
|
+
arr.push(bigSet[idx]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return arr;
|
|
29
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export const shallowClone = <T>(src: T): T => {
|
|
2
|
+
if (src == null || typeof src !== 'object') {
|
|
3
|
+
return src;
|
|
4
|
+
}
|
|
5
|
+
let res: any;
|
|
6
|
+
let idx;
|
|
7
|
+
if (Array.isArray(src)) {
|
|
8
|
+
res = [];
|
|
9
|
+
idx = src.length;
|
|
10
|
+
while (idx--) {
|
|
11
|
+
res[idx] = src[idx];
|
|
12
|
+
}
|
|
13
|
+
} else {
|
|
14
|
+
res = {};
|
|
15
|
+
const keys = Object.keys(src) as Array<keyof T>;
|
|
16
|
+
idx = keys.length;
|
|
17
|
+
while (idx--) {
|
|
18
|
+
const key = keys[idx];
|
|
19
|
+
res[key] = src[key];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return res;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const deepClone = <T>(src: T): T => {
|
|
26
|
+
let vidx = 0;
|
|
27
|
+
const visited = new Map();
|
|
28
|
+
const cloned: any[] = [];
|
|
29
|
+
const cloneDeepInner = <T>(src: T): T => {
|
|
30
|
+
if (typeof src !== 'object' || src === null) {
|
|
31
|
+
return src;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let res: any;
|
|
35
|
+
let idx;
|
|
36
|
+
const cidx = visited.get(src);
|
|
37
|
+
|
|
38
|
+
if (cidx !== undefined) {
|
|
39
|
+
return cloned[cidx];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
visited.set(src, vidx++);
|
|
43
|
+
if (Array.isArray(src)) {
|
|
44
|
+
res = [];
|
|
45
|
+
cloned.push(res);
|
|
46
|
+
idx = src.length;
|
|
47
|
+
while (idx--) {
|
|
48
|
+
res[idx] = cloneDeepInner(src[idx]);
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
res = {};
|
|
52
|
+
cloned.push(res);
|
|
53
|
+
const keys = Object.keys(src) as Array<keyof T>;
|
|
54
|
+
idx = keys.length;
|
|
55
|
+
while (idx--) {
|
|
56
|
+
const key = keys[idx];
|
|
57
|
+
res[key] = cloneDeepInner(src[key]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return res;
|
|
61
|
+
};
|
|
62
|
+
return cloneDeepInner(src);
|
|
63
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { isObject } from './what-is.js';
|
|
2
|
+
|
|
3
|
+
interface AreEqualOptions {
|
|
4
|
+
caseInsensitiveComparison?: boolean;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const areEqual = (json1: unknown, json2: unknown, options?: AreEqualOptions): boolean => {
|
|
8
|
+
options = options || {};
|
|
9
|
+
const caseInsensitiveComparison = options.caseInsensitiveComparison || false;
|
|
10
|
+
|
|
11
|
+
// http://json-schema.org/latest/json-schema-core.html#rfc.section.3.6
|
|
12
|
+
|
|
13
|
+
// Two JSON values are said to be equal if and only if:
|
|
14
|
+
// both are nulls; or
|
|
15
|
+
// both are booleans, and have the same value; or
|
|
16
|
+
// both are strings, and have the same value; or
|
|
17
|
+
// both are numbers, and have the same mathematical value; or
|
|
18
|
+
if (json1 === json2) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
if (
|
|
22
|
+
caseInsensitiveComparison === true &&
|
|
23
|
+
typeof json1 === 'string' &&
|
|
24
|
+
typeof json2 === 'string' &&
|
|
25
|
+
json1.toUpperCase() === json2.toUpperCase()
|
|
26
|
+
) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let i, len;
|
|
31
|
+
|
|
32
|
+
// both are arrays, and:
|
|
33
|
+
if (Array.isArray(json1) && Array.isArray(json2)) {
|
|
34
|
+
// have the same number of items; and
|
|
35
|
+
if (json1.length !== json2.length) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
// items at the same index are equal according to this definition; or
|
|
39
|
+
len = json1.length;
|
|
40
|
+
for (i = 0; i < len; i++) {
|
|
41
|
+
if (!areEqual(json1[i], json2[i], { caseInsensitiveComparison: caseInsensitiveComparison })) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// both are objects, and:
|
|
49
|
+
if (isObject(json1) && isObject(json2)) {
|
|
50
|
+
// have the same set of property names; and
|
|
51
|
+
const keys1 = sortedKeys(json1 as Record<string, unknown>);
|
|
52
|
+
const keys2 = sortedKeys(json2 as Record<string, unknown>);
|
|
53
|
+
if (!areEqual(keys1, keys2, { caseInsensitiveComparison: caseInsensitiveComparison })) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
// values for a same property name are equal according to this definition.
|
|
57
|
+
len = keys1.length;
|
|
58
|
+
for (i = 0; i < len; i++) {
|
|
59
|
+
if (!areEqual(json1[keys1[i]], json2[keys1[i]], { caseInsensitiveComparison: caseInsensitiveComparison })) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return false;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const decodeJSONPointer = (str: string) => {
|
|
70
|
+
// http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07#section-3
|
|
71
|
+
return decodeURIComponent(str).replace(/~[0-1]/g, (x) => (x === '~1' ? '/' : '~'));
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export const sortedKeys = (obj: Record<string, unknown>): string[] => Object.keys(obj).sort();
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/*
|
|
2
|
+
following function comes from punycode.js library
|
|
3
|
+
see: https://github.com/bestiejs/punycode.js
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Creates an array containing the numeric code points of each Unicode
|
|
7
|
+
* character in the string. While JavaScript uses UCS-2 internally,
|
|
8
|
+
* this function will convert a pair of surrogate halves (each of which
|
|
9
|
+
* UCS-2 exposes as separate characters) into a single code point,
|
|
10
|
+
* matching UTF-16.
|
|
11
|
+
* @see `punycode.ucs2.encode`
|
|
12
|
+
* @see <https://mathiasbynens.be/notes/javascript-encoding>
|
|
13
|
+
* @memberOf punycode.ucs2
|
|
14
|
+
* @name decode
|
|
15
|
+
* @param {String} string The Unicode input string (UCS-2).
|
|
16
|
+
* @returns {Array} The new array of code points.
|
|
17
|
+
*/
|
|
18
|
+
export function ucs2decode(string: string): number[] {
|
|
19
|
+
const output = [];
|
|
20
|
+
let counter = 0;
|
|
21
|
+
const length = string.length;
|
|
22
|
+
let value;
|
|
23
|
+
let extra;
|
|
24
|
+
while (counter < length) {
|
|
25
|
+
value = string.charCodeAt(counter++);
|
|
26
|
+
if (value >= 0xd800 && value <= 0xdbff && counter < length) {
|
|
27
|
+
// high surrogate, and there is a next character
|
|
28
|
+
extra = string.charCodeAt(counter++);
|
|
29
|
+
if ((extra & 0xfc00) == 0xdc00) {
|
|
30
|
+
// low surrogate
|
|
31
|
+
output.push(((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000);
|
|
32
|
+
} else {
|
|
33
|
+
// unmatched surrogate; only append this code unit, in case the next
|
|
34
|
+
// code unit is the high surrogate of a surrogate pair
|
|
35
|
+
output.push(value);
|
|
36
|
+
counter--;
|
|
37
|
+
}
|
|
38
|
+
} else {
|
|
39
|
+
output.push(value);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return output;
|
|
43
|
+
}
|
package/src/utils/uri.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const getQueryPath = (uri: string) => {
|
|
2
|
+
const io = uri.indexOf('#');
|
|
3
|
+
const res = io === -1 ? undefined : uri.slice(io + 1);
|
|
4
|
+
// WARN: do not slice slash, #/ means take root and go down from it
|
|
5
|
+
// if (res && res[0] === "/") { res = res.slice(1); }
|
|
6
|
+
return res;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const getRemotePath = (uri: string) => {
|
|
10
|
+
const io = uri.indexOf('#');
|
|
11
|
+
return io === -1 ? uri : uri.slice(0, io);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const isAbsoluteUri = (uri: string): boolean => /^https?:\/\//.test(uri);
|
|
15
|
+
|
|
16
|
+
export const isRelativeUri = (uri: string): boolean =>
|
|
17
|
+
// relative URIs that end with a hash sign, issue #56
|
|
18
|
+
/.+#/.test(uri);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export type WHAT_IS =
|
|
2
|
+
| 'undefined'
|
|
3
|
+
| 'null'
|
|
4
|
+
| 'object'
|
|
5
|
+
| 'array'
|
|
6
|
+
| 'integer'
|
|
7
|
+
| 'number'
|
|
8
|
+
| 'string'
|
|
9
|
+
| 'not-a-number'
|
|
10
|
+
| 'unknown-number'
|
|
11
|
+
| 'bigint'
|
|
12
|
+
| 'boolean'
|
|
13
|
+
| 'symbol'
|
|
14
|
+
| 'function';
|
|
15
|
+
|
|
16
|
+
export const whatIs = (what: unknown): WHAT_IS => {
|
|
17
|
+
if (typeof what === 'object') {
|
|
18
|
+
if (what === null) {
|
|
19
|
+
return 'null';
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(what)) {
|
|
22
|
+
return 'array';
|
|
23
|
+
}
|
|
24
|
+
return 'object'; // typeof what === 'object' && what === Object(what) && !Array.isArray(what);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (typeof what === 'number') {
|
|
28
|
+
if (Number.isFinite(what)) {
|
|
29
|
+
if (what % 1 === 0) {
|
|
30
|
+
return 'integer';
|
|
31
|
+
} else {
|
|
32
|
+
return 'number';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (Number.isNaN(what)) {
|
|
36
|
+
return 'not-a-number';
|
|
37
|
+
}
|
|
38
|
+
return 'unknown-number';
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return typeof what; // undefined, boolean, string, function
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export function isObject(value: unknown): value is Record<any, any> {
|
|
45
|
+
return whatIs(value) === 'object';
|
|
46
|
+
}
|