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.
Files changed (76) hide show
  1. package/README.md +7 -11
  2. package/bin/z-schema +0 -0
  3. package/cjs/index.d.ts +192 -117
  4. package/cjs/index.js +949 -998
  5. package/{src/FormatValidators.ts → dist/format-validators.js} +97 -65
  6. package/dist/index.js +1 -1
  7. package/dist/json-schema.js +40 -0
  8. package/dist/{JsonValidation.js → json-validation.js} +75 -69
  9. package/dist/{Report.js → report.js} +35 -45
  10. package/dist/schema-cache.js +109 -0
  11. package/dist/schema-compiler.js +255 -0
  12. package/dist/{SchemaValidation.js → schema-validator.js} +153 -149
  13. package/dist/types/{Errors.d.ts → errors.d.ts} +2 -0
  14. package/dist/types/format-validators.d.ts +10 -0
  15. package/dist/types/index.d.ts +10 -1
  16. package/dist/types/json-schema.d.ts +50 -0
  17. package/dist/types/json-validation.d.ts +7 -0
  18. package/dist/types/{Report.d.ts → report.d.ts} +22 -23
  19. package/dist/types/schema-cache.d.ts +17 -0
  20. package/dist/types/schema-compiler.d.ts +16 -0
  21. package/dist/types/schema-validator.d.ts +10 -0
  22. package/dist/types/utils/array.d.ts +2 -0
  23. package/dist/types/utils/clone.d.ts +2 -0
  24. package/dist/types/utils/json.d.ts +7 -0
  25. package/dist/types/utils/symbols.d.ts +2 -0
  26. package/dist/types/utils/unicode.d.ts +14 -0
  27. package/dist/types/utils/uri.d.ts +4 -0
  28. package/dist/types/utils/what-is.d.ts +3 -0
  29. package/dist/types/z-schema.d.ts +75 -0
  30. package/dist/utils/array.js +27 -0
  31. package/dist/utils/clone.js +61 -0
  32. package/dist/utils/json.js +59 -0
  33. package/dist/utils/symbols.js +2 -0
  34. package/dist/utils/unicode.js +45 -0
  35. package/dist/utils/uri.js +15 -0
  36. package/dist/utils/what-is.js +29 -0
  37. package/dist/{ZSchema.js → z-schema.js} +66 -77
  38. package/package.json +8 -4
  39. package/src/{Errors.ts → errors.ts} +4 -0
  40. package/src/format-validators.ts +191 -0
  41. package/src/index.ts +12 -1
  42. package/src/json-schema.ts +97 -0
  43. package/src/{JsonValidation.ts → json-validation.ts} +137 -127
  44. package/src/{Report.ts → report.ts} +60 -70
  45. package/src/schema-cache.ts +122 -0
  46. package/src/schema-compiler.ts +300 -0
  47. package/src/{SchemaValidation.ts → schema-validator.ts} +213 -215
  48. package/src/utils/array.ts +29 -0
  49. package/src/utils/clone.ts +63 -0
  50. package/src/utils/json.ts +74 -0
  51. package/src/utils/symbols.ts +3 -0
  52. package/src/utils/unicode.ts +43 -0
  53. package/src/utils/uri.ts +18 -0
  54. package/src/utils/what-is.ts +46 -0
  55. package/src/{ZSchema.ts → z-schema.ts} +108 -113
  56. package/umd/ZSchema.js +949 -998
  57. package/umd/ZSchema.min.js +1 -1
  58. package/dist/FormatValidators.js +0 -136
  59. package/dist/SchemaCache.js +0 -173
  60. package/dist/SchemaCompilation.js +0 -259
  61. package/dist/Utils.js +0 -266
  62. package/dist/types/FormatValidators.d.ts +0 -12
  63. package/dist/types/JsonValidation.d.ts +0 -37
  64. package/dist/types/SchemaCache.d.ts +0 -26
  65. package/dist/types/SchemaCompilation.d.ts +0 -1
  66. package/dist/types/SchemaValidation.d.ts +0 -6
  67. package/dist/types/Utils.d.ts +0 -64
  68. package/dist/types/ZSchema.d.ts +0 -97
  69. package/src/SchemaCache.ts +0 -189
  70. package/src/SchemaCompilation.ts +0 -293
  71. package/src/Utils.ts +0 -286
  72. /package/dist/{Errors.js → errors.js} +0 -0
  73. /package/dist/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
  74. /package/dist/schemas/{schema.json → draft-04-schema.json} +0 -0
  75. /package/src/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
  76. /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,3 @@
1
+ export const jsonSymbol = Symbol.for('z-schema/json');
2
+
3
+ export const schemaSymbol = Symbol.for('z-schema/schema');
@@ -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
+ }
@@ -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
+ }