@sinclair/typebox 0.25.23 → 0.25.25
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/compiler/compiler.d.ts +25 -28
- package/compiler/compiler.js +495 -495
- package/compiler/index.d.ts +2 -2
- package/compiler/index.js +47 -47
- package/conditional/conditional.d.ts +17 -17
- package/conditional/conditional.js +91 -91
- package/conditional/index.d.ts +2 -2
- package/conditional/index.js +45 -45
- package/conditional/structural.d.ts +11 -11
- package/conditional/structural.js +685 -685
- package/custom/custom.d.ts +12 -12
- package/custom/custom.js +55 -55
- package/custom/index.d.ts +1 -1
- package/custom/index.js +44 -44
- package/errors/errors.d.ts +67 -67
- package/errors/errors.js +472 -468
- package/errors/index.d.ts +1 -1
- package/errors/index.js +44 -44
- package/format/format.d.ts +12 -12
- package/format/format.js +55 -55
- package/format/index.d.ts +1 -1
- package/format/index.js +44 -44
- package/guard/extends.d.ts +10 -0
- package/guard/extends.js +50 -0
- package/guard/guard.d.ts +60 -60
- package/guard/guard.js +440 -440
- package/guard/index.d.ts +2 -1
- package/guard/index.js +45 -44
- package/hash/hash.d.ts +8 -8
- package/hash/hash.js +183 -183
- package/hash/index.d.ts +1 -1
- package/hash/index.js +44 -44
- package/license +22 -22
- package/package.json +55 -52
- package/readme.md +1237 -1325
- package/system/index.d.ts +1 -1
- package/system/index.js +44 -44
- package/system/system.d.ts +17 -17
- package/system/system.js +69 -69
- package/typebox.d.ts +422 -422
- package/typebox.js +388 -388
- package/value/cast.d.ts +26 -26
- package/value/cast.js +415 -420
- package/value/check.d.ts +8 -8
- package/value/check.js +405 -395
- package/value/clone.d.ts +3 -3
- package/value/clone.js +71 -71
- package/value/create.d.ts +14 -14
- package/value/create.js +378 -388
- package/value/delta.d.ts +43 -43
- package/value/delta.js +204 -204
- package/value/equal.d.ts +3 -3
- package/value/equal.js +80 -80
- package/value/index.d.ts +4 -4
- package/value/index.js +53 -53
- package/value/is.d.ts +11 -11
- package/value/is.js +53 -53
- package/value/pointer.d.ts +24 -24
- package/value/pointer.js +142 -142
- package/value/value.d.ts +32 -32
- package/value/value.js +87 -87
package/value/check.js
CHANGED
|
@@ -1,395 +1,405 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*--------------------------------------------------------------------------
|
|
3
|
-
|
|
4
|
-
@sinclair/typebox/value
|
|
5
|
-
|
|
6
|
-
The MIT License (MIT)
|
|
7
|
-
|
|
8
|
-
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
-
|
|
10
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
-
in the Software without restriction, including without limitation the rights
|
|
13
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
-
furnished to do so, subject to the following conditions:
|
|
16
|
-
|
|
17
|
-
The above copyright notice and this permission notice shall be included in
|
|
18
|
-
all copies or substantial portions of the Software.
|
|
19
|
-
|
|
20
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
-
THE SOFTWARE.
|
|
27
|
-
|
|
28
|
-
---------------------------------------------------------------------------*/
|
|
29
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueCheck = exports.ValueCheckUnknownTypeError = void 0;
|
|
31
|
-
const Types = require("../typebox");
|
|
32
|
-
const index_1 = require("../system/index");
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (schema.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
if (schema.
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (propertyKeys.includes(
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (!
|
|
242
|
-
return false;
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
case '
|
|
347
|
-
return
|
|
348
|
-
case '
|
|
349
|
-
return
|
|
350
|
-
case '
|
|
351
|
-
return
|
|
352
|
-
case '
|
|
353
|
-
return
|
|
354
|
-
case '
|
|
355
|
-
return
|
|
356
|
-
case '
|
|
357
|
-
return
|
|
358
|
-
case '
|
|
359
|
-
return
|
|
360
|
-
case '
|
|
361
|
-
return
|
|
362
|
-
case '
|
|
363
|
-
return
|
|
364
|
-
case '
|
|
365
|
-
return
|
|
366
|
-
case '
|
|
367
|
-
return
|
|
368
|
-
case '
|
|
369
|
-
return
|
|
370
|
-
case '
|
|
371
|
-
return
|
|
372
|
-
case '
|
|
373
|
-
return
|
|
374
|
-
case '
|
|
375
|
-
return
|
|
376
|
-
case '
|
|
377
|
-
return
|
|
378
|
-
case '
|
|
379
|
-
return
|
|
380
|
-
case '
|
|
381
|
-
return
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
return
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/value
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in
|
|
18
|
+
all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
THE SOFTWARE.
|
|
27
|
+
|
|
28
|
+
---------------------------------------------------------------------------*/
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.ValueCheck = exports.ValueCheckUnknownTypeError = void 0;
|
|
31
|
+
const Types = require("../typebox");
|
|
32
|
+
const index_1 = require("../system/index");
|
|
33
|
+
const extends_1 = require("../guard/extends");
|
|
34
|
+
const index_2 = require("../format/index");
|
|
35
|
+
const index_3 = require("../custom/index");
|
|
36
|
+
const index_4 = require("../hash/index");
|
|
37
|
+
class ValueCheckUnknownTypeError extends Error {
|
|
38
|
+
constructor(schema) {
|
|
39
|
+
super(`ValueCheck: ${schema[Types.Kind] ? `Unknown type '${schema[Types.Kind]}'` : 'Unknown type'}`);
|
|
40
|
+
this.schema = schema;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.ValueCheckUnknownTypeError = ValueCheckUnknownTypeError;
|
|
44
|
+
var ValueCheck;
|
|
45
|
+
(function (ValueCheck) {
|
|
46
|
+
// --------------------------------------------------------
|
|
47
|
+
// Guards
|
|
48
|
+
// --------------------------------------------------------
|
|
49
|
+
function IsNumber(value) {
|
|
50
|
+
return typeof value === 'number' && !isNaN(value);
|
|
51
|
+
}
|
|
52
|
+
// --------------------------------------------------------
|
|
53
|
+
// Guards
|
|
54
|
+
// --------------------------------------------------------
|
|
55
|
+
function Any(schema, references, value) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
function Array(schema, references, value) {
|
|
59
|
+
if (!globalThis.Array.isArray(value)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
if (IsNumber(schema.minItems) && !(value.length >= schema.minItems)) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
if (IsNumber(schema.maxItems) && !(value.length <= schema.maxItems)) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
// prettier-ignore
|
|
69
|
+
if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
|
|
70
|
+
const hashed = index_4.ValueHash.Create(element);
|
|
71
|
+
if (set.has(hashed)) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
set.add(hashed);
|
|
76
|
+
}
|
|
77
|
+
} return true; })())) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return value.every((val) => Visit(schema.items, references, val));
|
|
81
|
+
}
|
|
82
|
+
function Boolean(schema, references, value) {
|
|
83
|
+
return typeof value === 'boolean';
|
|
84
|
+
}
|
|
85
|
+
function Constructor(schema, references, value) {
|
|
86
|
+
return Visit(schema.returns, references, value.prototype);
|
|
87
|
+
}
|
|
88
|
+
function Date(schema, references, value) {
|
|
89
|
+
if (!(value instanceof globalThis.Date)) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
if (isNaN(value.getTime())) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
if (IsNumber(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
if (IsNumber(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
if (IsNumber(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
if (IsNumber(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
function Function(schema, references, value) {
|
|
110
|
+
return typeof value === 'function';
|
|
111
|
+
}
|
|
112
|
+
function Integer(schema, references, value) {
|
|
113
|
+
if (!(typeof value === 'number' && globalThis.Number.isInteger(value))) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
if (IsNumber(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
if (IsNumber(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
if (IsNumber(schema.minimum) && !(value >= schema.minimum)) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
if (IsNumber(schema.maximum) && !(value <= schema.maximum)) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
function Literal(schema, references, value) {
|
|
134
|
+
return value === schema.const;
|
|
135
|
+
}
|
|
136
|
+
function Never(schema, references, value) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
function Null(schema, references, value) {
|
|
140
|
+
return value === null;
|
|
141
|
+
}
|
|
142
|
+
function Number(schema, references, value) {
|
|
143
|
+
if (index_1.TypeSystem.AllowNaN) {
|
|
144
|
+
if (!(typeof value === 'number')) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
if (!(typeof value === 'number' && !isNaN(value))) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
if (IsNumber(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
if (IsNumber(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
if (IsNumber(schema.minimum) && !(value >= schema.minimum)) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
if (IsNumber(schema.maximum) && !(value <= schema.maximum)) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
function Object(schema, references, value) {
|
|
171
|
+
if (index_1.TypeSystem.AllowArrayObjects) {
|
|
172
|
+
if (!(typeof value === 'object' && value !== null)) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (IsNumber(schema.minProperties) && !(globalThis.Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
if (IsNumber(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
const propertyKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
|
|
188
|
+
if (schema.additionalProperties === false) {
|
|
189
|
+
// optimization: If the property key length matches the required keys length
|
|
190
|
+
// then we only need check that the values property key length matches that
|
|
191
|
+
// of the property key length. This is because exhaustive testing for values
|
|
192
|
+
// will occur in subsequent property tests.
|
|
193
|
+
if (schema.required && schema.required.length === propertyKeys.length && !(globalThis.Object.getOwnPropertyNames(value).length === propertyKeys.length)) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
if (!globalThis.Object.getOwnPropertyNames(value).every((key) => propertyKeys.includes(key))) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (typeof schema.additionalProperties === 'object') {
|
|
203
|
+
for (const objectKey of globalThis.Object.getOwnPropertyNames(value)) {
|
|
204
|
+
if (propertyKeys.includes(objectKey))
|
|
205
|
+
continue;
|
|
206
|
+
if (!Visit(schema.additionalProperties, references, value[objectKey])) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
for (const propertyKey of propertyKeys) {
|
|
212
|
+
const propertySchema = schema.properties[propertyKey];
|
|
213
|
+
if (schema.required && schema.required.includes(propertyKey)) {
|
|
214
|
+
if (!Visit(propertySchema, references, value[propertyKey])) {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
if (extends_1.TypeExtends.Undefined(propertySchema)) {
|
|
218
|
+
return propertyKey in value;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
if (value[propertyKey] !== undefined) {
|
|
223
|
+
if (!Visit(propertySchema, references, value[propertyKey])) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
function Promise(schema, references, value) {
|
|
232
|
+
return typeof value === 'object' && typeof value.then === 'function';
|
|
233
|
+
}
|
|
234
|
+
function Record(schema, references, value) {
|
|
235
|
+
if (index_1.TypeSystem.AllowArrayObjects) {
|
|
236
|
+
if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date))) {
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date) && !globalThis.Array.isArray(value))) {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
246
|
+
const regex = new RegExp(keyPattern);
|
|
247
|
+
if (!globalThis.Object.getOwnPropertyNames(value).every((key) => regex.test(key))) {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
for (const propValue of globalThis.Object.values(value)) {
|
|
251
|
+
if (!Visit(valueSchema, references, propValue))
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
function Ref(schema, references, value) {
|
|
257
|
+
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
258
|
+
if (reference === undefined)
|
|
259
|
+
throw new Error(`ValueCheck.Ref: Cannot find schema with $id '${schema.$ref}'.`);
|
|
260
|
+
return Visit(reference, references, value);
|
|
261
|
+
}
|
|
262
|
+
function Self(schema, references, value) {
|
|
263
|
+
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
264
|
+
if (reference === undefined)
|
|
265
|
+
throw new Error(`ValueCheck.Self: Cannot find schema with $id '${schema.$ref}'.`);
|
|
266
|
+
return Visit(reference, references, value);
|
|
267
|
+
}
|
|
268
|
+
function String(schema, references, value) {
|
|
269
|
+
if (!(typeof value === 'string')) {
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
if (IsNumber(schema.minLength)) {
|
|
273
|
+
if (!(value.length >= schema.minLength))
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
if (IsNumber(schema.maxLength)) {
|
|
277
|
+
if (!(value.length <= schema.maxLength))
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
if (schema.pattern !== undefined) {
|
|
281
|
+
const regex = new RegExp(schema.pattern);
|
|
282
|
+
if (!regex.test(value))
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
if (schema.format !== undefined) {
|
|
286
|
+
if (!index_2.Format.Has(schema.format))
|
|
287
|
+
return false;
|
|
288
|
+
const func = index_2.Format.Get(schema.format);
|
|
289
|
+
return func(value);
|
|
290
|
+
}
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
function Tuple(schema, references, value) {
|
|
294
|
+
if (!globalThis.Array.isArray(value)) {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
if (schema.items === undefined && !(value.length === 0)) {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
if (!(value.length === schema.maxItems)) {
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
if (!schema.items) {
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
for (let i = 0; i < schema.items.length; i++) {
|
|
307
|
+
if (!Visit(schema.items[i], references, value[i]))
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
function Undefined(schema, references, value) {
|
|
313
|
+
return value === undefined;
|
|
314
|
+
}
|
|
315
|
+
function Union(schema, references, value) {
|
|
316
|
+
return schema.anyOf.some((inner) => Visit(inner, references, value));
|
|
317
|
+
}
|
|
318
|
+
function Uint8Array(schema, references, value) {
|
|
319
|
+
if (!(value instanceof globalThis.Uint8Array)) {
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
if (IsNumber(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
if (IsNumber(schema.minByteLength) && !(value.length >= schema.minByteLength)) {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
330
|
+
function Unknown(schema, references, value) {
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
function Void(schema, references, value) {
|
|
334
|
+
return value === null;
|
|
335
|
+
}
|
|
336
|
+
function UserDefined(schema, references, value) {
|
|
337
|
+
if (!index_3.Custom.Has(schema[Types.Kind]))
|
|
338
|
+
return false;
|
|
339
|
+
const func = index_3.Custom.Get(schema[Types.Kind]);
|
|
340
|
+
return func(schema, value);
|
|
341
|
+
}
|
|
342
|
+
function Visit(schema, references, value) {
|
|
343
|
+
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
344
|
+
const anySchema = schema;
|
|
345
|
+
switch (anySchema[Types.Kind]) {
|
|
346
|
+
case 'Any':
|
|
347
|
+
return Any(anySchema, anyReferences, value);
|
|
348
|
+
case 'Array':
|
|
349
|
+
return Array(anySchema, anyReferences, value);
|
|
350
|
+
case 'Boolean':
|
|
351
|
+
return Boolean(anySchema, anyReferences, value);
|
|
352
|
+
case 'Constructor':
|
|
353
|
+
return Constructor(anySchema, anyReferences, value);
|
|
354
|
+
case 'Date':
|
|
355
|
+
return Date(anySchema, anyReferences, value);
|
|
356
|
+
case 'Function':
|
|
357
|
+
return Function(anySchema, anyReferences, value);
|
|
358
|
+
case 'Integer':
|
|
359
|
+
return Integer(anySchema, anyReferences, value);
|
|
360
|
+
case 'Literal':
|
|
361
|
+
return Literal(anySchema, anyReferences, value);
|
|
362
|
+
case 'Never':
|
|
363
|
+
return Never(anySchema, anyReferences, value);
|
|
364
|
+
case 'Null':
|
|
365
|
+
return Null(anySchema, anyReferences, value);
|
|
366
|
+
case 'Number':
|
|
367
|
+
return Number(anySchema, anyReferences, value);
|
|
368
|
+
case 'Object':
|
|
369
|
+
return Object(anySchema, anyReferences, value);
|
|
370
|
+
case 'Promise':
|
|
371
|
+
return Promise(anySchema, anyReferences, value);
|
|
372
|
+
case 'Record':
|
|
373
|
+
return Record(anySchema, anyReferences, value);
|
|
374
|
+
case 'Ref':
|
|
375
|
+
return Ref(anySchema, anyReferences, value);
|
|
376
|
+
case 'Self':
|
|
377
|
+
return Self(anySchema, anyReferences, value);
|
|
378
|
+
case 'String':
|
|
379
|
+
return String(anySchema, anyReferences, value);
|
|
380
|
+
case 'Tuple':
|
|
381
|
+
return Tuple(anySchema, anyReferences, value);
|
|
382
|
+
case 'Undefined':
|
|
383
|
+
return Undefined(anySchema, anyReferences, value);
|
|
384
|
+
case 'Union':
|
|
385
|
+
return Union(anySchema, anyReferences, value);
|
|
386
|
+
case 'Uint8Array':
|
|
387
|
+
return Uint8Array(anySchema, anyReferences, value);
|
|
388
|
+
case 'Unknown':
|
|
389
|
+
return Unknown(anySchema, anyReferences, value);
|
|
390
|
+
case 'Void':
|
|
391
|
+
return Void(anySchema, anyReferences, value);
|
|
392
|
+
default:
|
|
393
|
+
if (!index_3.Custom.Has(anySchema[Types.Kind]))
|
|
394
|
+
throw new ValueCheckUnknownTypeError(anySchema);
|
|
395
|
+
return UserDefined(anySchema, anyReferences, value);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
// -------------------------------------------------------------------------
|
|
399
|
+
// Check
|
|
400
|
+
// -------------------------------------------------------------------------
|
|
401
|
+
function Check(schema, references, value) {
|
|
402
|
+
return schema.$id === undefined ? Visit(schema, references, value) : Visit(schema, [schema, ...references], value);
|
|
403
|
+
}
|
|
404
|
+
ValueCheck.Check = Check;
|
|
405
|
+
})(ValueCheck = exports.ValueCheck || (exports.ValueCheck = {}));
|