z-schema 6.0.1 → 7.0.0-beta.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.
Files changed (47) hide show
  1. package/README.md +154 -137
  2. package/bin/z-schema +128 -124
  3. package/dist/Errors.js +50 -0
  4. package/dist/FormatValidators.js +136 -0
  5. package/{src → dist}/JsonValidation.js +186 -212
  6. package/dist/Report.js +220 -0
  7. package/{src → dist}/SchemaCache.js +67 -82
  8. package/{src → dist}/SchemaCompilation.js +89 -129
  9. package/dist/SchemaValidation.js +631 -0
  10. package/{src → dist}/Utils.js +96 -104
  11. package/dist/ZSchema-umd-min.js +1 -0
  12. package/dist/ZSchema-umd.js +13791 -0
  13. package/dist/ZSchema.cjs +13785 -0
  14. package/dist/ZSchema.js +366 -0
  15. package/dist/schemas/hyper-schema.json +156 -0
  16. package/dist/schemas/schema.json +151 -0
  17. package/dist/types/Errors.d.ts +44 -0
  18. package/dist/types/FormatValidators.d.ts +12 -0
  19. package/dist/types/JsonValidation.d.ts +37 -0
  20. package/dist/types/Report.d.ts +87 -0
  21. package/dist/types/SchemaCache.d.ts +26 -0
  22. package/dist/types/SchemaCompilation.d.ts +1 -0
  23. package/dist/types/SchemaValidation.d.ts +6 -0
  24. package/dist/types/Utils.d.ts +64 -0
  25. package/dist/types/ZSchema.d.ts +97 -0
  26. package/package.json +54 -43
  27. package/src/Errors.ts +56 -0
  28. package/src/FormatValidators.ts +136 -0
  29. package/src/JsonValidation.ts +624 -0
  30. package/src/Report.ts +337 -0
  31. package/src/SchemaCache.ts +189 -0
  32. package/src/SchemaCompilation.ts +293 -0
  33. package/src/SchemaValidation.ts +629 -0
  34. package/src/Utils.ts +286 -0
  35. package/src/ZSchema.ts +469 -0
  36. package/src/schemas/_ +0 -0
  37. package/dist/ZSchema-browser-min.js +0 -2
  38. package/dist/ZSchema-browser-min.js.map +0 -1
  39. package/dist/ZSchema-browser-test.js +0 -30633
  40. package/dist/ZSchema-browser.js +0 -13429
  41. package/index.d.ts +0 -175
  42. package/src/Errors.js +0 -60
  43. package/src/FormatValidators.js +0 -129
  44. package/src/Polyfills.js +0 -16
  45. package/src/Report.js +0 -299
  46. package/src/SchemaValidation.js +0 -619
  47. package/src/ZSchema.js +0 -409
package/src/Utils.ts ADDED
@@ -0,0 +1,286 @@
1
+ export const jsonSymbol = Symbol.for('z-schema/json');
2
+
3
+ export const schemaSymbol = Symbol.for('z-schema/schema');
4
+
5
+ /**
6
+ * @param {object} obj
7
+ *
8
+ * @returns {string[]}
9
+ */
10
+ export function sortedKeys(obj) {
11
+ return Object.keys(obj).sort();
12
+ }
13
+
14
+ /**
15
+ *
16
+ * @param {string} uri
17
+ *
18
+ * @returns {boolean}
19
+ */
20
+ export function isAbsoluteUri(uri) {
21
+ return /^https?:\/\//.test(uri);
22
+ }
23
+
24
+ /**
25
+ *
26
+ * @param {string} uri
27
+ *
28
+ * @returns {boolean}
29
+ */
30
+ export function isRelativeUri(uri) {
31
+ // relative URIs that end with a hash sign, issue #56
32
+ return /.+#/.test(uri);
33
+ }
34
+
35
+ export function whatIs(what) {
36
+ const to = typeof what;
37
+
38
+ if (to === 'object') {
39
+ if (what === null) {
40
+ return 'null';
41
+ }
42
+ if (Array.isArray(what)) {
43
+ return 'array';
44
+ }
45
+ return 'object'; // typeof what === 'object' && what === Object(what) && !Array.isArray(what);
46
+ }
47
+
48
+ if (to === 'number') {
49
+ if (Number.isFinite(what)) {
50
+ if (what % 1 === 0) {
51
+ return 'integer';
52
+ } else {
53
+ return 'number';
54
+ }
55
+ }
56
+ if (Number.isNaN(what)) {
57
+ return 'not-a-number';
58
+ }
59
+ return 'unknown-number';
60
+ }
61
+
62
+ return to; // undefined, boolean, string, function
63
+ }
64
+
65
+ /**
66
+ *
67
+ * @param {*} json1
68
+ * @param {*} json2
69
+ * @param {*} [options]
70
+ *
71
+ * @returns {boolean}
72
+ */
73
+ export function areEqual(json1, json2, options?) {
74
+ options = options || {};
75
+ const caseInsensitiveComparison = options.caseInsensitiveComparison || false;
76
+
77
+ // http://json-schema.org/latest/json-schema-core.html#rfc.section.3.6
78
+
79
+ // Two JSON values are said to be equal if and only if:
80
+ // both are nulls; or
81
+ // both are booleans, and have the same value; or
82
+ // both are strings, and have the same value; or
83
+ // both are numbers, and have the same mathematical value; or
84
+ if (json1 === json2) {
85
+ return true;
86
+ }
87
+ if (
88
+ caseInsensitiveComparison === true &&
89
+ typeof json1 === 'string' &&
90
+ typeof json2 === 'string' &&
91
+ json1.toUpperCase() === json2.toUpperCase()
92
+ ) {
93
+ return true;
94
+ }
95
+
96
+ let i, len;
97
+
98
+ // both are arrays, and:
99
+ if (Array.isArray(json1) && Array.isArray(json2)) {
100
+ // have the same number of items; and
101
+ if (json1.length !== json2.length) {
102
+ return false;
103
+ }
104
+ // items at the same index are equal according to this definition; or
105
+ len = json1.length;
106
+ for (i = 0; i < len; i++) {
107
+ if (!areEqual(json1[i], json2[i], { caseInsensitiveComparison: caseInsensitiveComparison })) {
108
+ return false;
109
+ }
110
+ }
111
+ return true;
112
+ }
113
+
114
+ // both are objects, and:
115
+ if (whatIs(json1) === 'object' && whatIs(json2) === 'object') {
116
+ // have the same set of property names; and
117
+ const keys1 = sortedKeys(json1);
118
+ const keys2 = sortedKeys(json2);
119
+ if (!areEqual(keys1, keys2, { caseInsensitiveComparison: caseInsensitiveComparison })) {
120
+ return false;
121
+ }
122
+ // values for a same property name are equal according to this definition.
123
+ len = keys1.length;
124
+ for (i = 0; i < len; i++) {
125
+ if (!areEqual(json1[keys1[i]], json2[keys1[i]], { caseInsensitiveComparison: caseInsensitiveComparison })) {
126
+ return false;
127
+ }
128
+ }
129
+ return true;
130
+ }
131
+
132
+ return false;
133
+ }
134
+
135
+ /**
136
+ *
137
+ * @param {*[]} arr
138
+ * @param {number[]} [indexes]
139
+ *
140
+ * @returns {boolean}
141
+ */
142
+ export function isUniqueArray(arr, indexes?) {
143
+ let i;
144
+ let j;
145
+ const l = arr.length;
146
+ for (i = 0; i < l; i++) {
147
+ for (j = i + 1; j < l; j++) {
148
+ if (areEqual(arr[i], arr[j])) {
149
+ if (indexes) {
150
+ indexes.push(i, j);
151
+ }
152
+ return false;
153
+ }
154
+ }
155
+ }
156
+ return true;
157
+ }
158
+
159
+ /**
160
+ *
161
+ * @param {*} bigSet
162
+ * @param {*} subSet
163
+ *
164
+ * @returns {*[]}
165
+ */
166
+ export function difference(bigSet, subSet) {
167
+ const arr = [];
168
+ let idx = bigSet.length;
169
+ while (idx--) {
170
+ if (subSet.indexOf(bigSet[idx]) === -1) {
171
+ arr.push(bigSet[idx]);
172
+ }
173
+ }
174
+ return arr;
175
+ }
176
+
177
+ // NOT a deep version of clone
178
+ export function clone(src) {
179
+ if (typeof src === 'undefined') {
180
+ return void 0;
181
+ }
182
+ if (typeof src !== 'object' || src === null) {
183
+ return src;
184
+ }
185
+ let res, idx;
186
+ if (Array.isArray(src)) {
187
+ res = [];
188
+ idx = src.length;
189
+ while (idx--) {
190
+ res[idx] = src[idx];
191
+ }
192
+ } else {
193
+ res = {};
194
+ const keys = Object.keys(src);
195
+ idx = keys.length;
196
+ while (idx--) {
197
+ const key = keys[idx];
198
+ res[key] = src[key];
199
+ }
200
+ }
201
+ return res;
202
+ }
203
+
204
+ export function cloneDeep(src) {
205
+ let vidx = 0;
206
+ const visited = new Map();
207
+ const cloned = [];
208
+ function cloneDeepInner(src) {
209
+ if (typeof src !== 'object' || src === null) {
210
+ return src;
211
+ }
212
+
213
+ let res;
214
+ let idx;
215
+ const cidx = visited.get(src);
216
+
217
+ if (cidx !== undefined) {
218
+ return cloned[cidx];
219
+ }
220
+
221
+ visited.set(src, vidx++);
222
+ if (Array.isArray(src)) {
223
+ res = [];
224
+ cloned.push(res);
225
+ idx = src.length;
226
+ while (idx--) {
227
+ res[idx] = cloneDeepInner(src[idx]);
228
+ }
229
+ } else {
230
+ res = {};
231
+ cloned.push(res);
232
+ const keys = Object.keys(src);
233
+ idx = keys.length;
234
+ while (idx--) {
235
+ const key = keys[idx];
236
+ res[key] = cloneDeepInner(src[key]);
237
+ }
238
+ }
239
+ return res;
240
+ }
241
+ return cloneDeepInner(src);
242
+ }
243
+
244
+ /*
245
+ following function comes from punycode.js library
246
+ see: https://github.com/bestiejs/punycode.js
247
+ */
248
+ /**
249
+ * Creates an array containing the numeric code points of each Unicode
250
+ * character in the string. While JavaScript uses UCS-2 internally,
251
+ * this function will convert a pair of surrogate halves (each of which
252
+ * UCS-2 exposes as separate characters) into a single code point,
253
+ * matching UTF-16.
254
+ * @see `punycode.ucs2.encode`
255
+ * @see <https://mathiasbynens.be/notes/javascript-encoding>
256
+ * @memberOf punycode.ucs2
257
+ * @name decode
258
+ * @param {String} string The Unicode input string (UCS-2).
259
+ * @returns {Array} The new array of code points.
260
+ */
261
+ export function ucs2decode(string) {
262
+ const output = [];
263
+ let counter = 0;
264
+ const length = string.length;
265
+ let value;
266
+ let extra;
267
+ while (counter < length) {
268
+ value = string.charCodeAt(counter++);
269
+ if (value >= 0xd800 && value <= 0xdbff && counter < length) {
270
+ // high surrogate, and there is a next character
271
+ extra = string.charCodeAt(counter++);
272
+ if ((extra & 0xfc00) == 0xdc00) {
273
+ // low surrogate
274
+ output.push(((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000);
275
+ } else {
276
+ // unmatched surrogate; only append this code unit, in case the next
277
+ // code unit is the high surrogate of a surrogate pair
278
+ output.push(value);
279
+ counter--;
280
+ }
281
+ } else {
282
+ output.push(value);
283
+ }
284
+ }
285
+ return output;
286
+ }