@sinclair/typebox 0.25.24 → 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.
Files changed (61) hide show
  1. package/compiler/compiler.d.ts +25 -28
  2. package/compiler/compiler.js +495 -498
  3. package/compiler/index.d.ts +2 -2
  4. package/compiler/index.js +47 -47
  5. package/conditional/conditional.d.ts +17 -17
  6. package/conditional/conditional.js +91 -91
  7. package/conditional/index.d.ts +2 -2
  8. package/conditional/index.js +45 -45
  9. package/conditional/structural.d.ts +11 -11
  10. package/conditional/structural.js +685 -685
  11. package/custom/custom.d.ts +12 -12
  12. package/custom/custom.js +55 -55
  13. package/custom/index.d.ts +1 -1
  14. package/custom/index.js +44 -44
  15. package/errors/errors.d.ts +67 -67
  16. package/errors/errors.js +472 -472
  17. package/errors/index.d.ts +1 -1
  18. package/errors/index.js +44 -44
  19. package/format/format.d.ts +12 -12
  20. package/format/format.js +55 -55
  21. package/format/index.d.ts +1 -1
  22. package/format/index.js +44 -44
  23. package/guard/extends.d.ts +10 -10
  24. package/guard/extends.js +50 -50
  25. package/guard/guard.d.ts +60 -60
  26. package/guard/guard.js +440 -440
  27. package/guard/index.d.ts +2 -2
  28. package/guard/index.js +45 -45
  29. package/hash/hash.d.ts +8 -8
  30. package/hash/hash.js +183 -183
  31. package/hash/index.d.ts +1 -1
  32. package/hash/index.js +44 -44
  33. package/license +22 -22
  34. package/package.json +55 -52
  35. package/readme.md +1237 -1237
  36. package/system/index.d.ts +1 -1
  37. package/system/index.js +44 -44
  38. package/system/system.d.ts +17 -17
  39. package/system/system.js +69 -69
  40. package/typebox.d.ts +422 -422
  41. package/typebox.js +388 -388
  42. package/value/cast.d.ts +26 -26
  43. package/value/cast.js +415 -415
  44. package/value/check.d.ts +8 -8
  45. package/value/check.js +405 -405
  46. package/value/clone.d.ts +3 -3
  47. package/value/clone.js +71 -71
  48. package/value/create.d.ts +14 -14
  49. package/value/create.js +378 -378
  50. package/value/delta.d.ts +43 -43
  51. package/value/delta.js +204 -204
  52. package/value/equal.d.ts +3 -3
  53. package/value/equal.js +80 -80
  54. package/value/index.d.ts +4 -4
  55. package/value/index.js +53 -53
  56. package/value/is.d.ts +11 -11
  57. package/value/is.js +53 -53
  58. package/value/pointer.d.ts +24 -24
  59. package/value/pointer.js +142 -142
  60. package/value/value.d.ts +32 -32
  61. package/value/value.js +87 -87
package/value/check.js CHANGED
@@ -1,405 +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 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 = {}));
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 = {}));