@sinclair/typebox 0.25.23 → 0.26.0-dev
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 +9 -4
- package/compiler/compiler.js +160 -119
- package/errors/errors.d.ts +56 -46
- package/errors/errors.js +234 -149
- package/package.json +1 -6
- package/readme.md +395 -396
- package/system/system.d.ts +9 -6
- package/system/system.js +17 -17
- package/typebox.d.ts +386 -162
- package/typebox.js +1710 -229
- package/value/cast.d.ts +2 -2
- package/value/cast.js +120 -192
- package/value/check.d.ts +1 -1
- package/value/check.js +162 -107
- package/value/convert.d.ts +13 -0
- package/value/convert.js +345 -0
- package/value/create.d.ts +6 -2
- package/value/create.js +149 -107
- package/{hash → value}/hash.js +39 -14
- package/value/index.d.ts +1 -0
- package/value/index.js +3 -1
- package/value/value.d.ts +2 -8
- package/value/value.js +20 -14
- package/conditional/conditional.d.ts +0 -17
- package/conditional/conditional.js +0 -91
- package/conditional/index.d.ts +0 -2
- package/conditional/index.js +0 -45
- package/conditional/structural.d.ts +0 -11
- package/conditional/structural.js +0 -685
- package/custom/custom.d.ts +0 -12
- package/custom/custom.js +0 -55
- package/custom/index.d.ts +0 -1
- package/custom/index.js +0 -44
- package/format/format.d.ts +0 -12
- package/format/format.js +0 -55
- package/format/index.d.ts +0 -1
- package/format/index.js +0 -44
- package/guard/guard.d.ts +0 -60
- package/guard/guard.js +0 -440
- package/guard/index.d.ts +0 -1
- package/guard/index.js +0 -44
- package/hash/index.d.ts +0 -1
- package/hash/index.js +0 -44
- /package/{hash → value}/hash.d.ts +0 -0
|
@@ -1,685 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*--------------------------------------------------------------------------
|
|
3
|
-
|
|
4
|
-
@sinclair/typebox/conditional
|
|
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.Structural = exports.StructuralResult = void 0;
|
|
31
|
-
const Types = require("../typebox");
|
|
32
|
-
const guard_1 = require("../guard");
|
|
33
|
-
// --------------------------------------------------------------------------
|
|
34
|
-
// StructuralResult
|
|
35
|
-
// --------------------------------------------------------------------------
|
|
36
|
-
var StructuralResult;
|
|
37
|
-
(function (StructuralResult) {
|
|
38
|
-
StructuralResult[StructuralResult["Union"] = 0] = "Union";
|
|
39
|
-
StructuralResult[StructuralResult["True"] = 1] = "True";
|
|
40
|
-
StructuralResult[StructuralResult["False"] = 2] = "False";
|
|
41
|
-
})(StructuralResult = exports.StructuralResult || (exports.StructuralResult = {}));
|
|
42
|
-
// --------------------------------------------------------------------------
|
|
43
|
-
// Structural
|
|
44
|
-
// --------------------------------------------------------------------------
|
|
45
|
-
/** Performs structural equivalence checks against TypeBox types. */
|
|
46
|
-
var Structural;
|
|
47
|
-
(function (Structural) {
|
|
48
|
-
const referenceMap = new Map();
|
|
49
|
-
// ------------------------------------------------------------------------
|
|
50
|
-
// Rules
|
|
51
|
-
// ------------------------------------------------------------------------
|
|
52
|
-
function AnyUnknownOrCustomRule(right) {
|
|
53
|
-
// https://github.com/microsoft/TypeScript/issues/40049
|
|
54
|
-
if (guard_1.TypeGuard.TUnion(right) && right.anyOf.some((schema) => schema[Types.Kind] === 'Any' || schema[Types.Kind] === 'Unknown'))
|
|
55
|
-
return true;
|
|
56
|
-
if (guard_1.TypeGuard.TUnknown(right))
|
|
57
|
-
return true;
|
|
58
|
-
if (guard_1.TypeGuard.TAny(right))
|
|
59
|
-
return true;
|
|
60
|
-
if (guard_1.TypeGuard.TUserDefined(right))
|
|
61
|
-
throw Error(`Structural: Cannot structurally compare custom type '${right[Types.Kind]}'`);
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
function ObjectRightRule(left, right) {
|
|
65
|
-
// type A = boolean extends {} ? 1 : 2 // additionalProperties: false
|
|
66
|
-
// type B = boolean extends object ? 1 : 2 // additionalProperties: true
|
|
67
|
-
const additionalProperties = right.additionalProperties;
|
|
68
|
-
const propertyLength = globalThis.Object.keys(right.properties).length;
|
|
69
|
-
return additionalProperties === false && propertyLength === 0;
|
|
70
|
-
}
|
|
71
|
-
function UnionRightRule(left, right) {
|
|
72
|
-
const result = right.anyOf.some((right) => Visit(left, right) !== StructuralResult.False);
|
|
73
|
-
return result ? StructuralResult.True : StructuralResult.False;
|
|
74
|
-
}
|
|
75
|
-
// ------------------------------------------------------------------------
|
|
76
|
-
// Records
|
|
77
|
-
// ------------------------------------------------------------------------
|
|
78
|
-
function RecordPattern(schema) {
|
|
79
|
-
return globalThis.Object.keys(schema.patternProperties)[0];
|
|
80
|
-
}
|
|
81
|
-
function RecordNumberOrStringKey(schema) {
|
|
82
|
-
const pattern = RecordPattern(schema);
|
|
83
|
-
return pattern === '^.*$' || pattern === '^(0|[1-9][0-9]*)$';
|
|
84
|
-
}
|
|
85
|
-
function RecordValue(schema) {
|
|
86
|
-
const pattern = RecordPattern(schema);
|
|
87
|
-
return schema.patternProperties[pattern];
|
|
88
|
-
}
|
|
89
|
-
function RecordKey(schema) {
|
|
90
|
-
const pattern = RecordPattern(schema);
|
|
91
|
-
if (pattern === '^.*$') {
|
|
92
|
-
return Types.Type.String();
|
|
93
|
-
}
|
|
94
|
-
else if (pattern === '^(0|[1-9][0-9]*)$') {
|
|
95
|
-
return Types.Type.Number();
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
const keys = pattern.slice(1, pattern.length - 1).split('|');
|
|
99
|
-
const schemas = keys.map((key) => (isNaN(+key) ? Types.Type.Literal(key) : Types.Type.Literal(parseFloat(key))));
|
|
100
|
-
return Types.Type.Union(schemas);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
function PropertyMap(schema) {
|
|
104
|
-
const comparable = new Map();
|
|
105
|
-
if (guard_1.TypeGuard.TRecord(schema)) {
|
|
106
|
-
const propertyPattern = RecordPattern(schema);
|
|
107
|
-
if (propertyPattern === '^.*$' || propertyPattern === '^(0|[1-9][0-9]*)$')
|
|
108
|
-
throw Error('Cannot extract record properties without property constraints');
|
|
109
|
-
const propertySchema = schema.patternProperties[propertyPattern];
|
|
110
|
-
const propertyKeys = propertyPattern.slice(1, propertyPattern.length - 1).split('|');
|
|
111
|
-
propertyKeys.forEach((propertyKey) => {
|
|
112
|
-
comparable.set(propertyKey, propertySchema);
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
globalThis.Object.entries(schema.properties).forEach(([propertyKey, propertySchema]) => {
|
|
117
|
-
comparable.set(propertyKey, propertySchema);
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
return comparable;
|
|
121
|
-
}
|
|
122
|
-
// ------------------------------------------------------------------------
|
|
123
|
-
// Indexable
|
|
124
|
-
// ------------------------------------------------------------------------
|
|
125
|
-
function Indexable(left, right) {
|
|
126
|
-
if (guard_1.TypeGuard.TUnion(right)) {
|
|
127
|
-
return StructuralResult.False;
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
return Visit(left, right);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
// ------------------------------------------------------------------------
|
|
134
|
-
// Checks
|
|
135
|
-
// ------------------------------------------------------------------------
|
|
136
|
-
function Any(left, right) {
|
|
137
|
-
return AnyUnknownOrCustomRule(right) ? StructuralResult.True : StructuralResult.Union;
|
|
138
|
-
}
|
|
139
|
-
function Array(left, right) {
|
|
140
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
141
|
-
return StructuralResult.True;
|
|
142
|
-
}
|
|
143
|
-
else if (guard_1.TypeGuard.TObject(right)) {
|
|
144
|
-
if (right.properties['length'] !== undefined && right.properties['length'][Types.Kind] === 'Number')
|
|
145
|
-
return StructuralResult.True;
|
|
146
|
-
if (globalThis.Object.keys(right.properties).length === 0)
|
|
147
|
-
return StructuralResult.True;
|
|
148
|
-
return StructuralResult.False;
|
|
149
|
-
}
|
|
150
|
-
else if (!guard_1.TypeGuard.TArray(right)) {
|
|
151
|
-
return StructuralResult.False;
|
|
152
|
-
}
|
|
153
|
-
else if (left.items === undefined && right.items !== undefined) {
|
|
154
|
-
return StructuralResult.False;
|
|
155
|
-
}
|
|
156
|
-
else if (left.items !== undefined && right.items === undefined) {
|
|
157
|
-
return StructuralResult.False;
|
|
158
|
-
}
|
|
159
|
-
else if (left.items === undefined && right.items === undefined) {
|
|
160
|
-
return StructuralResult.False;
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
const result = Visit(left.items, right.items) !== StructuralResult.False;
|
|
164
|
-
return result ? StructuralResult.True : StructuralResult.False;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
function Boolean(left, right) {
|
|
168
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
169
|
-
return StructuralResult.True;
|
|
170
|
-
}
|
|
171
|
-
else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
|
|
172
|
-
return StructuralResult.True;
|
|
173
|
-
}
|
|
174
|
-
else if (guard_1.TypeGuard.TBoolean(right)) {
|
|
175
|
-
return StructuralResult.True;
|
|
176
|
-
}
|
|
177
|
-
else if (guard_1.TypeGuard.TUnion(right)) {
|
|
178
|
-
return UnionRightRule(left, right);
|
|
179
|
-
}
|
|
180
|
-
else {
|
|
181
|
-
return StructuralResult.False;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
function Constructor(left, right) {
|
|
185
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
186
|
-
return StructuralResult.True;
|
|
187
|
-
}
|
|
188
|
-
else if (guard_1.TypeGuard.TObject(right) && globalThis.Object.keys(right.properties).length === 0) {
|
|
189
|
-
return StructuralResult.True;
|
|
190
|
-
}
|
|
191
|
-
else if (!guard_1.TypeGuard.TConstructor(right)) {
|
|
192
|
-
return StructuralResult.False;
|
|
193
|
-
}
|
|
194
|
-
else if (right.parameters.length < left.parameters.length) {
|
|
195
|
-
return StructuralResult.False;
|
|
196
|
-
}
|
|
197
|
-
else {
|
|
198
|
-
if (Visit(left.returns, right.returns) === StructuralResult.False) {
|
|
199
|
-
return StructuralResult.False;
|
|
200
|
-
}
|
|
201
|
-
for (let i = 0; i < left.parameters.length; i++) {
|
|
202
|
-
const result = Visit(right.parameters[i], left.parameters[i]);
|
|
203
|
-
if (result === StructuralResult.False)
|
|
204
|
-
return StructuralResult.False;
|
|
205
|
-
}
|
|
206
|
-
return StructuralResult.True;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
function Date(left, right) {
|
|
210
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
211
|
-
return StructuralResult.True;
|
|
212
|
-
}
|
|
213
|
-
else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
|
|
214
|
-
return StructuralResult.True;
|
|
215
|
-
}
|
|
216
|
-
else if (guard_1.TypeGuard.TRecord(right)) {
|
|
217
|
-
return StructuralResult.False;
|
|
218
|
-
}
|
|
219
|
-
else if (guard_1.TypeGuard.TDate(right)) {
|
|
220
|
-
return StructuralResult.True;
|
|
221
|
-
}
|
|
222
|
-
else if (guard_1.TypeGuard.TUnion(right)) {
|
|
223
|
-
return UnionRightRule(left, right);
|
|
224
|
-
}
|
|
225
|
-
else {
|
|
226
|
-
return StructuralResult.False;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
function Function(left, right) {
|
|
230
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
231
|
-
return StructuralResult.True;
|
|
232
|
-
}
|
|
233
|
-
else if (guard_1.TypeGuard.TObject(right)) {
|
|
234
|
-
if (right.properties['length'] !== undefined && right.properties['length'][Types.Kind] === 'Number')
|
|
235
|
-
return StructuralResult.True;
|
|
236
|
-
if (globalThis.Object.keys(right.properties).length === 0)
|
|
237
|
-
return StructuralResult.True;
|
|
238
|
-
return StructuralResult.False;
|
|
239
|
-
}
|
|
240
|
-
else if (!guard_1.TypeGuard.TFunction(right)) {
|
|
241
|
-
return StructuralResult.False;
|
|
242
|
-
}
|
|
243
|
-
else if (right.parameters.length < left.parameters.length) {
|
|
244
|
-
return StructuralResult.False;
|
|
245
|
-
}
|
|
246
|
-
else if (Visit(left.returns, right.returns) === StructuralResult.False) {
|
|
247
|
-
return StructuralResult.False;
|
|
248
|
-
}
|
|
249
|
-
else {
|
|
250
|
-
for (let i = 0; i < left.parameters.length; i++) {
|
|
251
|
-
const result = Visit(right.parameters[i], left.parameters[i]);
|
|
252
|
-
if (result === StructuralResult.False)
|
|
253
|
-
return StructuralResult.False;
|
|
254
|
-
}
|
|
255
|
-
return StructuralResult.True;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
function Integer(left, right) {
|
|
259
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
260
|
-
return StructuralResult.True;
|
|
261
|
-
}
|
|
262
|
-
else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
|
|
263
|
-
return StructuralResult.True;
|
|
264
|
-
}
|
|
265
|
-
else if (guard_1.TypeGuard.TInteger(right) || guard_1.TypeGuard.TNumber(right)) {
|
|
266
|
-
return StructuralResult.True;
|
|
267
|
-
}
|
|
268
|
-
else if (guard_1.TypeGuard.TUnion(right)) {
|
|
269
|
-
return UnionRightRule(left, right);
|
|
270
|
-
}
|
|
271
|
-
else {
|
|
272
|
-
return StructuralResult.False;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
function Literal(left, right) {
|
|
276
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
277
|
-
return StructuralResult.True;
|
|
278
|
-
}
|
|
279
|
-
else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
|
|
280
|
-
return StructuralResult.True;
|
|
281
|
-
}
|
|
282
|
-
else if (guard_1.TypeGuard.TRecord(right)) {
|
|
283
|
-
if (typeof left.const === 'string') {
|
|
284
|
-
return Indexable(left, RecordValue(right));
|
|
285
|
-
}
|
|
286
|
-
else {
|
|
287
|
-
return StructuralResult.False;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
else if (guard_1.TypeGuard.TLiteral(right) && left.const === right.const) {
|
|
291
|
-
return StructuralResult.True;
|
|
292
|
-
}
|
|
293
|
-
else if (guard_1.TypeGuard.TString(right) && typeof left.const === 'string') {
|
|
294
|
-
return StructuralResult.True;
|
|
295
|
-
}
|
|
296
|
-
else if (guard_1.TypeGuard.TNumber(right) && typeof left.const === 'number') {
|
|
297
|
-
return StructuralResult.True;
|
|
298
|
-
}
|
|
299
|
-
else if (guard_1.TypeGuard.TInteger(right) && typeof left.const === 'number') {
|
|
300
|
-
return StructuralResult.True;
|
|
301
|
-
}
|
|
302
|
-
else if (guard_1.TypeGuard.TBoolean(right) && typeof left.const === 'boolean') {
|
|
303
|
-
return StructuralResult.True;
|
|
304
|
-
}
|
|
305
|
-
else if (guard_1.TypeGuard.TUnion(right)) {
|
|
306
|
-
return UnionRightRule(left, right);
|
|
307
|
-
}
|
|
308
|
-
else {
|
|
309
|
-
return StructuralResult.False;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
function Number(left, right) {
|
|
313
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
314
|
-
return StructuralResult.True;
|
|
315
|
-
}
|
|
316
|
-
else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
|
|
317
|
-
return StructuralResult.True;
|
|
318
|
-
}
|
|
319
|
-
else if (guard_1.TypeGuard.TNumber(right)) {
|
|
320
|
-
return StructuralResult.True;
|
|
321
|
-
}
|
|
322
|
-
else if (guard_1.TypeGuard.TInteger(right)) {
|
|
323
|
-
return StructuralResult.True;
|
|
324
|
-
}
|
|
325
|
-
else if (guard_1.TypeGuard.TUnion(right)) {
|
|
326
|
-
return UnionRightRule(left, right);
|
|
327
|
-
}
|
|
328
|
-
else {
|
|
329
|
-
return StructuralResult.False;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
function Null(left, right) {
|
|
333
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
334
|
-
return StructuralResult.True;
|
|
335
|
-
}
|
|
336
|
-
else if (guard_1.TypeGuard.TNull(right)) {
|
|
337
|
-
return StructuralResult.True;
|
|
338
|
-
}
|
|
339
|
-
else if (guard_1.TypeGuard.TUnion(right)) {
|
|
340
|
-
return UnionRightRule(left, right);
|
|
341
|
-
}
|
|
342
|
-
else {
|
|
343
|
-
return StructuralResult.False;
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
function Properties(left, right) {
|
|
347
|
-
if (right.size > left.size)
|
|
348
|
-
return StructuralResult.False;
|
|
349
|
-
if (![...right.keys()].every((rightKey) => left.has(rightKey)))
|
|
350
|
-
return StructuralResult.False;
|
|
351
|
-
for (const rightKey of right.keys()) {
|
|
352
|
-
const leftProp = left.get(rightKey);
|
|
353
|
-
const rightProp = right.get(rightKey);
|
|
354
|
-
if (Visit(leftProp, rightProp) === StructuralResult.False) {
|
|
355
|
-
return StructuralResult.False;
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
return StructuralResult.True;
|
|
359
|
-
}
|
|
360
|
-
function Object(left, right) {
|
|
361
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
362
|
-
return StructuralResult.True;
|
|
363
|
-
}
|
|
364
|
-
else if (guard_1.TypeGuard.TObject(right)) {
|
|
365
|
-
return Properties(PropertyMap(left), PropertyMap(right));
|
|
366
|
-
}
|
|
367
|
-
else if (guard_1.TypeGuard.TRecord(right)) {
|
|
368
|
-
if (!RecordNumberOrStringKey(right)) {
|
|
369
|
-
return Properties(PropertyMap(left), PropertyMap(right));
|
|
370
|
-
}
|
|
371
|
-
else {
|
|
372
|
-
return StructuralResult.True;
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
else {
|
|
376
|
-
return StructuralResult.False;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
function Promise(left, right) {
|
|
380
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
381
|
-
return StructuralResult.True;
|
|
382
|
-
}
|
|
383
|
-
else if (guard_1.TypeGuard.TObject(right)) {
|
|
384
|
-
if (ObjectRightRule(left, right) || globalThis.Object.keys(right.properties).length === 0) {
|
|
385
|
-
return StructuralResult.True;
|
|
386
|
-
}
|
|
387
|
-
else {
|
|
388
|
-
return StructuralResult.False;
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
else if (!guard_1.TypeGuard.TPromise(right)) {
|
|
392
|
-
return StructuralResult.False;
|
|
393
|
-
}
|
|
394
|
-
else {
|
|
395
|
-
const result = Visit(left.item, right.item) !== StructuralResult.False;
|
|
396
|
-
return result ? StructuralResult.True : StructuralResult.False;
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
function Record(left, right) {
|
|
400
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
401
|
-
return StructuralResult.True;
|
|
402
|
-
}
|
|
403
|
-
else if (guard_1.TypeGuard.TObject(right)) {
|
|
404
|
-
if (RecordPattern(left) === '^.*$' && right[Types.Hint] === 'Record') {
|
|
405
|
-
return StructuralResult.True;
|
|
406
|
-
}
|
|
407
|
-
else if (RecordPattern(left) === '^.*$') {
|
|
408
|
-
return StructuralResult.False;
|
|
409
|
-
}
|
|
410
|
-
else {
|
|
411
|
-
return globalThis.Object.keys(right.properties).length === 0 ? StructuralResult.True : StructuralResult.False;
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
else if (guard_1.TypeGuard.TRecord(right)) {
|
|
415
|
-
if (!RecordNumberOrStringKey(left) && !RecordNumberOrStringKey(right)) {
|
|
416
|
-
return Properties(PropertyMap(left), PropertyMap(right));
|
|
417
|
-
}
|
|
418
|
-
else if (RecordNumberOrStringKey(left) && !RecordNumberOrStringKey(right)) {
|
|
419
|
-
const leftKey = RecordKey(left);
|
|
420
|
-
const rightKey = RecordKey(right);
|
|
421
|
-
if (Visit(rightKey, leftKey) === StructuralResult.False) {
|
|
422
|
-
return StructuralResult.False;
|
|
423
|
-
}
|
|
424
|
-
else {
|
|
425
|
-
return StructuralResult.True;
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
else {
|
|
429
|
-
return StructuralResult.True;
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
else {
|
|
433
|
-
return StructuralResult.False;
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
function Ref(left, right) {
|
|
437
|
-
if (!referenceMap.has(left.$ref))
|
|
438
|
-
throw Error(`Cannot locate referenced $id '${left.$ref}'`);
|
|
439
|
-
const resolved = referenceMap.get(left.$ref);
|
|
440
|
-
return Visit(resolved, right);
|
|
441
|
-
}
|
|
442
|
-
function Self(left, right) {
|
|
443
|
-
if (!referenceMap.has(left.$ref))
|
|
444
|
-
throw Error(`Cannot locate referenced self $id '${left.$ref}'`);
|
|
445
|
-
const resolved = referenceMap.get(left.$ref);
|
|
446
|
-
return Visit(resolved, right);
|
|
447
|
-
}
|
|
448
|
-
function String(left, right) {
|
|
449
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
450
|
-
return StructuralResult.True;
|
|
451
|
-
}
|
|
452
|
-
else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
|
|
453
|
-
return StructuralResult.True;
|
|
454
|
-
}
|
|
455
|
-
else if (guard_1.TypeGuard.TRecord(right)) {
|
|
456
|
-
return Indexable(left, RecordValue(right));
|
|
457
|
-
}
|
|
458
|
-
else if (guard_1.TypeGuard.TString(right)) {
|
|
459
|
-
return StructuralResult.True;
|
|
460
|
-
}
|
|
461
|
-
else if (guard_1.TypeGuard.TUnion(right)) {
|
|
462
|
-
return UnionRightRule(left, right);
|
|
463
|
-
}
|
|
464
|
-
else {
|
|
465
|
-
return StructuralResult.False;
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
function Tuple(left, right) {
|
|
469
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
470
|
-
return StructuralResult.True;
|
|
471
|
-
}
|
|
472
|
-
else if (guard_1.TypeGuard.TObject(right)) {
|
|
473
|
-
const result = ObjectRightRule(left, right) || globalThis.Object.keys(right.properties).length === 0;
|
|
474
|
-
return result ? StructuralResult.True : StructuralResult.False;
|
|
475
|
-
}
|
|
476
|
-
else if (guard_1.TypeGuard.TRecord(right)) {
|
|
477
|
-
return Indexable(left, RecordValue(right));
|
|
478
|
-
}
|
|
479
|
-
else if (guard_1.TypeGuard.TArray(right)) {
|
|
480
|
-
if (right.items === undefined) {
|
|
481
|
-
return StructuralResult.False;
|
|
482
|
-
}
|
|
483
|
-
else if (guard_1.TypeGuard.TUnion(right.items) && left.items) {
|
|
484
|
-
const result = left.items.every((left) => UnionRightRule(left, right.items) !== StructuralResult.False);
|
|
485
|
-
return result ? StructuralResult.True : StructuralResult.False;
|
|
486
|
-
}
|
|
487
|
-
else if (guard_1.TypeGuard.TAny(right.items)) {
|
|
488
|
-
return StructuralResult.True;
|
|
489
|
-
}
|
|
490
|
-
else {
|
|
491
|
-
return StructuralResult.False;
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
if (!guard_1.TypeGuard.TTuple(right))
|
|
495
|
-
return StructuralResult.False;
|
|
496
|
-
if (left.items === undefined && right.items === undefined)
|
|
497
|
-
return StructuralResult.True;
|
|
498
|
-
if (left.items === undefined && right.items !== undefined)
|
|
499
|
-
return StructuralResult.False;
|
|
500
|
-
if (left.items !== undefined && right.items === undefined)
|
|
501
|
-
return StructuralResult.False;
|
|
502
|
-
if (left.items === undefined && right.items === undefined)
|
|
503
|
-
return StructuralResult.True;
|
|
504
|
-
if (left.minItems !== right.minItems || left.maxItems !== right.maxItems)
|
|
505
|
-
return StructuralResult.False;
|
|
506
|
-
for (let i = 0; i < left.items.length; i++) {
|
|
507
|
-
if (Visit(left.items[i], right.items[i]) === StructuralResult.False)
|
|
508
|
-
return StructuralResult.False;
|
|
509
|
-
}
|
|
510
|
-
return StructuralResult.True;
|
|
511
|
-
}
|
|
512
|
-
function Uint8Array(left, right) {
|
|
513
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
514
|
-
return StructuralResult.True;
|
|
515
|
-
}
|
|
516
|
-
else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
|
|
517
|
-
return StructuralResult.True;
|
|
518
|
-
}
|
|
519
|
-
else if (guard_1.TypeGuard.TRecord(right)) {
|
|
520
|
-
return Indexable(left, RecordValue(right));
|
|
521
|
-
}
|
|
522
|
-
else if (guard_1.TypeGuard.TUint8Array(right)) {
|
|
523
|
-
return StructuralResult.True;
|
|
524
|
-
}
|
|
525
|
-
else if (guard_1.TypeGuard.TUnion(right)) {
|
|
526
|
-
return UnionRightRule(left, right);
|
|
527
|
-
}
|
|
528
|
-
else {
|
|
529
|
-
return StructuralResult.False;
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
function Undefined(left, right) {
|
|
533
|
-
if (AnyUnknownOrCustomRule(right)) {
|
|
534
|
-
return StructuralResult.True;
|
|
535
|
-
}
|
|
536
|
-
else if (guard_1.TypeGuard.TUndefined(right)) {
|
|
537
|
-
return StructuralResult.True;
|
|
538
|
-
}
|
|
539
|
-
else if (guard_1.TypeGuard.TVoid(right)) {
|
|
540
|
-
return StructuralResult.True;
|
|
541
|
-
}
|
|
542
|
-
else if (guard_1.TypeGuard.TUnion(right)) {
|
|
543
|
-
return UnionRightRule(left, right);
|
|
544
|
-
}
|
|
545
|
-
else {
|
|
546
|
-
return StructuralResult.False;
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
function Union(left, right) {
|
|
550
|
-
if (left.anyOf.some((left) => guard_1.TypeGuard.TAny(left))) {
|
|
551
|
-
return StructuralResult.Union;
|
|
552
|
-
}
|
|
553
|
-
else if (guard_1.TypeGuard.TUnion(right)) {
|
|
554
|
-
const result = left.anyOf.every((left) => right.anyOf.some((right) => Visit(left, right) !== StructuralResult.False));
|
|
555
|
-
return result ? StructuralResult.True : StructuralResult.False;
|
|
556
|
-
}
|
|
557
|
-
else {
|
|
558
|
-
const result = left.anyOf.every((left) => Visit(left, right) !== StructuralResult.False);
|
|
559
|
-
return result ? StructuralResult.True : StructuralResult.False;
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
function Unknown(left, right) {
|
|
563
|
-
if (guard_1.TypeGuard.TUnion(right)) {
|
|
564
|
-
const result = right.anyOf.some((right) => guard_1.TypeGuard.TAny(right) || guard_1.TypeGuard.TUnknown(right));
|
|
565
|
-
return result ? StructuralResult.True : StructuralResult.False;
|
|
566
|
-
}
|
|
567
|
-
else if (guard_1.TypeGuard.TAny(right)) {
|
|
568
|
-
return StructuralResult.True;
|
|
569
|
-
}
|
|
570
|
-
else if (guard_1.TypeGuard.TUnknown(right)) {
|
|
571
|
-
return StructuralResult.True;
|
|
572
|
-
}
|
|
573
|
-
else {
|
|
574
|
-
return StructuralResult.False;
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
function Void(left, right) {
|
|
578
|
-
if (guard_1.TypeGuard.TUnion(right)) {
|
|
579
|
-
const result = right.anyOf.some((right) => guard_1.TypeGuard.TAny(right) || guard_1.TypeGuard.TUnknown(right));
|
|
580
|
-
return result ? StructuralResult.True : StructuralResult.False;
|
|
581
|
-
}
|
|
582
|
-
else if (guard_1.TypeGuard.TAny(right)) {
|
|
583
|
-
return StructuralResult.True;
|
|
584
|
-
}
|
|
585
|
-
else if (guard_1.TypeGuard.TUnknown(right)) {
|
|
586
|
-
return StructuralResult.True;
|
|
587
|
-
}
|
|
588
|
-
else if (guard_1.TypeGuard.TVoid(right)) {
|
|
589
|
-
return StructuralResult.True;
|
|
590
|
-
}
|
|
591
|
-
else {
|
|
592
|
-
return StructuralResult.False;
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
let recursionDepth = 0;
|
|
596
|
-
function Visit(left, right) {
|
|
597
|
-
recursionDepth += 1;
|
|
598
|
-
if (recursionDepth >= 1000)
|
|
599
|
-
return StructuralResult.True;
|
|
600
|
-
if (left.$id !== undefined)
|
|
601
|
-
referenceMap.set(left.$id, left);
|
|
602
|
-
if (right.$id !== undefined)
|
|
603
|
-
referenceMap.set(right.$id, right);
|
|
604
|
-
const resolvedRight = right[Types.Kind] === 'Self' ? referenceMap.get(right.$ref) : right;
|
|
605
|
-
if (guard_1.TypeGuard.TAny(left)) {
|
|
606
|
-
return Any(left, resolvedRight);
|
|
607
|
-
}
|
|
608
|
-
else if (guard_1.TypeGuard.TArray(left)) {
|
|
609
|
-
return Array(left, resolvedRight);
|
|
610
|
-
}
|
|
611
|
-
else if (guard_1.TypeGuard.TBoolean(left)) {
|
|
612
|
-
return Boolean(left, resolvedRight);
|
|
613
|
-
}
|
|
614
|
-
else if (guard_1.TypeGuard.TConstructor(left)) {
|
|
615
|
-
return Constructor(left, resolvedRight);
|
|
616
|
-
}
|
|
617
|
-
else if (guard_1.TypeGuard.TDate(left)) {
|
|
618
|
-
return Date(left, resolvedRight);
|
|
619
|
-
}
|
|
620
|
-
else if (guard_1.TypeGuard.TFunction(left)) {
|
|
621
|
-
return Function(left, resolvedRight);
|
|
622
|
-
}
|
|
623
|
-
else if (guard_1.TypeGuard.TInteger(left)) {
|
|
624
|
-
return Integer(left, resolvedRight);
|
|
625
|
-
}
|
|
626
|
-
else if (guard_1.TypeGuard.TLiteral(left)) {
|
|
627
|
-
return Literal(left, resolvedRight);
|
|
628
|
-
}
|
|
629
|
-
else if (guard_1.TypeGuard.TNull(left)) {
|
|
630
|
-
return Null(left, resolvedRight);
|
|
631
|
-
}
|
|
632
|
-
else if (guard_1.TypeGuard.TNumber(left)) {
|
|
633
|
-
return Number(left, resolvedRight);
|
|
634
|
-
}
|
|
635
|
-
else if (guard_1.TypeGuard.TObject(left)) {
|
|
636
|
-
return Object(left, resolvedRight);
|
|
637
|
-
}
|
|
638
|
-
else if (guard_1.TypeGuard.TPromise(left)) {
|
|
639
|
-
return Promise(left, resolvedRight);
|
|
640
|
-
}
|
|
641
|
-
else if (guard_1.TypeGuard.TRecord(left)) {
|
|
642
|
-
return Record(left, resolvedRight);
|
|
643
|
-
}
|
|
644
|
-
else if (guard_1.TypeGuard.TRef(left)) {
|
|
645
|
-
return Ref(left, resolvedRight);
|
|
646
|
-
}
|
|
647
|
-
else if (guard_1.TypeGuard.TSelf(left)) {
|
|
648
|
-
return Self(left, resolvedRight);
|
|
649
|
-
}
|
|
650
|
-
else if (guard_1.TypeGuard.TString(left)) {
|
|
651
|
-
return String(left, resolvedRight);
|
|
652
|
-
}
|
|
653
|
-
else if (guard_1.TypeGuard.TTuple(left)) {
|
|
654
|
-
return Tuple(left, resolvedRight);
|
|
655
|
-
}
|
|
656
|
-
else if (guard_1.TypeGuard.TUndefined(left)) {
|
|
657
|
-
return Undefined(left, resolvedRight);
|
|
658
|
-
}
|
|
659
|
-
else if (guard_1.TypeGuard.TUint8Array(left)) {
|
|
660
|
-
return Uint8Array(left, resolvedRight);
|
|
661
|
-
}
|
|
662
|
-
else if (guard_1.TypeGuard.TUnion(left)) {
|
|
663
|
-
return Union(left, resolvedRight);
|
|
664
|
-
}
|
|
665
|
-
else if (guard_1.TypeGuard.TUnknown(left)) {
|
|
666
|
-
return Unknown(left, resolvedRight);
|
|
667
|
-
}
|
|
668
|
-
else if (guard_1.TypeGuard.TVoid(left)) {
|
|
669
|
-
return Void(left, resolvedRight);
|
|
670
|
-
}
|
|
671
|
-
else if (guard_1.TypeGuard.TUserDefined(left)) {
|
|
672
|
-
throw Error(`Structural: Cannot structurally compare custom type '${left[Types.Kind]}'`);
|
|
673
|
-
}
|
|
674
|
-
else {
|
|
675
|
-
throw Error(`Structural: Unknown left operand '${left[Types.Kind]}'`);
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
/** Structurally tests if the left schema extends the right. */
|
|
679
|
-
function Check(left, right) {
|
|
680
|
-
referenceMap.clear();
|
|
681
|
-
recursionDepth = 0;
|
|
682
|
-
return Visit(left, right);
|
|
683
|
-
}
|
|
684
|
-
Structural.Check = Check;
|
|
685
|
-
})(Structural = exports.Structural || (exports.Structural = {}));
|
package/custom/custom.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export type CustomValidationFunction<TSchema> = (schema: TSchema, value: unknown) => boolean;
|
|
2
|
-
/** Provides functions to create user defined types */
|
|
3
|
-
export declare namespace Custom {
|
|
4
|
-
/** Clears all user defined types */
|
|
5
|
-
function Clear(): void;
|
|
6
|
-
/** Returns true if this user defined type exists */
|
|
7
|
-
function Has(kind: string): boolean;
|
|
8
|
-
/** Sets a validation function for a user defined type */
|
|
9
|
-
function Set<TSchema = unknown>(kind: string, func: CustomValidationFunction<TSchema>): void;
|
|
10
|
-
/** Gets a custom validation function for a user defined type */
|
|
11
|
-
function Get(kind: string): CustomValidationFunction<any> | undefined;
|
|
12
|
-
}
|