@sinclair/typebox 0.24.38 → 0.24.39
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.js +5 -0
- package/errors/errors.d.ts +29 -28
- package/errors/errors.js +34 -28
- package/guard/guard.d.ts +2 -0
- package/guard/guard.js +17 -0
- package/package.json +1 -1
- package/readme.md +12 -1
- package/typebox.d.ts +12 -0
- package/typebox.js +12 -2
- package/value/cast.d.ts +4 -0
- package/value/cast.js +39 -26
- package/value/check.js +5 -0
- package/value/create.d.ts +4 -0
- package/value/create.js +13 -1
- package/value/delta.js +0 -1
package/compiler/compiler.js
CHANGED
|
@@ -151,6 +151,9 @@ var TypeCompiler;
|
|
|
151
151
|
yield `(${value} === '${schema.const}')`;
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
|
+
function* Never(schema, value) {
|
|
155
|
+
yield `(false)`;
|
|
156
|
+
}
|
|
154
157
|
function* Null(schema, value) {
|
|
155
158
|
yield `(${value} === null)`;
|
|
156
159
|
}
|
|
@@ -299,6 +302,8 @@ var TypeCompiler;
|
|
|
299
302
|
return yield* Integer(anySchema, value);
|
|
300
303
|
case 'Literal':
|
|
301
304
|
return yield* Literal(anySchema, value);
|
|
305
|
+
case 'Never':
|
|
306
|
+
return yield* Never(anySchema, value);
|
|
302
307
|
case 'Null':
|
|
303
308
|
return yield* Null(anySchema, value);
|
|
304
309
|
case 'Number':
|
package/errors/errors.d.ts
CHANGED
|
@@ -13,34 +13,35 @@ export declare enum ValueErrorType {
|
|
|
13
13
|
IntegerMinimum = 10,
|
|
14
14
|
IntegerMaximum = 11,
|
|
15
15
|
Literal = 12,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
16
|
+
Never = 13,
|
|
17
|
+
Null = 14,
|
|
18
|
+
Number = 15,
|
|
19
|
+
NumberMultipleOf = 16,
|
|
20
|
+
NumberExclusiveMinimum = 17,
|
|
21
|
+
NumberExclusiveMaximum = 18,
|
|
22
|
+
NumberMinumum = 19,
|
|
23
|
+
NumberMaximum = 20,
|
|
24
|
+
Object = 21,
|
|
25
|
+
ObjectMinProperties = 22,
|
|
26
|
+
ObjectMaxProperties = 23,
|
|
27
|
+
ObjectAdditionalProperties = 24,
|
|
28
|
+
Promise = 25,
|
|
29
|
+
RecordKeyNumeric = 26,
|
|
30
|
+
RecordKeyString = 27,
|
|
31
|
+
String = 28,
|
|
32
|
+
StringMinLength = 29,
|
|
33
|
+
StringMaxLength = 30,
|
|
34
|
+
StringPattern = 31,
|
|
35
|
+
StringFormatUnknown = 32,
|
|
36
|
+
StringFormat = 33,
|
|
37
|
+
TupleZeroLength = 34,
|
|
38
|
+
TupleLength = 35,
|
|
39
|
+
Undefined = 36,
|
|
40
|
+
Union = 37,
|
|
41
|
+
Uint8Array = 38,
|
|
42
|
+
Uint8ArrayMinByteLength = 39,
|
|
43
|
+
Uint8ArrayMaxByteLength = 40,
|
|
44
|
+
Void = 41
|
|
44
45
|
}
|
|
45
46
|
export interface ValueError {
|
|
46
47
|
type: ValueErrorType;
|
package/errors/errors.js
CHANGED
|
@@ -48,34 +48,35 @@ var ValueErrorType;
|
|
|
48
48
|
ValueErrorType[ValueErrorType["IntegerMinimum"] = 10] = "IntegerMinimum";
|
|
49
49
|
ValueErrorType[ValueErrorType["IntegerMaximum"] = 11] = "IntegerMaximum";
|
|
50
50
|
ValueErrorType[ValueErrorType["Literal"] = 12] = "Literal";
|
|
51
|
-
ValueErrorType[ValueErrorType["
|
|
52
|
-
ValueErrorType[ValueErrorType["
|
|
53
|
-
ValueErrorType[ValueErrorType["
|
|
54
|
-
ValueErrorType[ValueErrorType["
|
|
55
|
-
ValueErrorType[ValueErrorType["
|
|
56
|
-
ValueErrorType[ValueErrorType["
|
|
57
|
-
ValueErrorType[ValueErrorType["
|
|
58
|
-
ValueErrorType[ValueErrorType["
|
|
59
|
-
ValueErrorType[ValueErrorType["
|
|
60
|
-
ValueErrorType[ValueErrorType["
|
|
61
|
-
ValueErrorType[ValueErrorType["
|
|
62
|
-
ValueErrorType[ValueErrorType["
|
|
63
|
-
ValueErrorType[ValueErrorType["
|
|
64
|
-
ValueErrorType[ValueErrorType["
|
|
65
|
-
ValueErrorType[ValueErrorType["
|
|
66
|
-
ValueErrorType[ValueErrorType["
|
|
67
|
-
ValueErrorType[ValueErrorType["
|
|
68
|
-
ValueErrorType[ValueErrorType["
|
|
69
|
-
ValueErrorType[ValueErrorType["
|
|
70
|
-
ValueErrorType[ValueErrorType["
|
|
71
|
-
ValueErrorType[ValueErrorType["
|
|
72
|
-
ValueErrorType[ValueErrorType["
|
|
73
|
-
ValueErrorType[ValueErrorType["
|
|
74
|
-
ValueErrorType[ValueErrorType["
|
|
75
|
-
ValueErrorType[ValueErrorType["
|
|
76
|
-
ValueErrorType[ValueErrorType["
|
|
77
|
-
ValueErrorType[ValueErrorType["
|
|
78
|
-
ValueErrorType[ValueErrorType["
|
|
51
|
+
ValueErrorType[ValueErrorType["Never"] = 13] = "Never";
|
|
52
|
+
ValueErrorType[ValueErrorType["Null"] = 14] = "Null";
|
|
53
|
+
ValueErrorType[ValueErrorType["Number"] = 15] = "Number";
|
|
54
|
+
ValueErrorType[ValueErrorType["NumberMultipleOf"] = 16] = "NumberMultipleOf";
|
|
55
|
+
ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 17] = "NumberExclusiveMinimum";
|
|
56
|
+
ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 18] = "NumberExclusiveMaximum";
|
|
57
|
+
ValueErrorType[ValueErrorType["NumberMinumum"] = 19] = "NumberMinumum";
|
|
58
|
+
ValueErrorType[ValueErrorType["NumberMaximum"] = 20] = "NumberMaximum";
|
|
59
|
+
ValueErrorType[ValueErrorType["Object"] = 21] = "Object";
|
|
60
|
+
ValueErrorType[ValueErrorType["ObjectMinProperties"] = 22] = "ObjectMinProperties";
|
|
61
|
+
ValueErrorType[ValueErrorType["ObjectMaxProperties"] = 23] = "ObjectMaxProperties";
|
|
62
|
+
ValueErrorType[ValueErrorType["ObjectAdditionalProperties"] = 24] = "ObjectAdditionalProperties";
|
|
63
|
+
ValueErrorType[ValueErrorType["Promise"] = 25] = "Promise";
|
|
64
|
+
ValueErrorType[ValueErrorType["RecordKeyNumeric"] = 26] = "RecordKeyNumeric";
|
|
65
|
+
ValueErrorType[ValueErrorType["RecordKeyString"] = 27] = "RecordKeyString";
|
|
66
|
+
ValueErrorType[ValueErrorType["String"] = 28] = "String";
|
|
67
|
+
ValueErrorType[ValueErrorType["StringMinLength"] = 29] = "StringMinLength";
|
|
68
|
+
ValueErrorType[ValueErrorType["StringMaxLength"] = 30] = "StringMaxLength";
|
|
69
|
+
ValueErrorType[ValueErrorType["StringPattern"] = 31] = "StringPattern";
|
|
70
|
+
ValueErrorType[ValueErrorType["StringFormatUnknown"] = 32] = "StringFormatUnknown";
|
|
71
|
+
ValueErrorType[ValueErrorType["StringFormat"] = 33] = "StringFormat";
|
|
72
|
+
ValueErrorType[ValueErrorType["TupleZeroLength"] = 34] = "TupleZeroLength";
|
|
73
|
+
ValueErrorType[ValueErrorType["TupleLength"] = 35] = "TupleLength";
|
|
74
|
+
ValueErrorType[ValueErrorType["Undefined"] = 36] = "Undefined";
|
|
75
|
+
ValueErrorType[ValueErrorType["Union"] = 37] = "Union";
|
|
76
|
+
ValueErrorType[ValueErrorType["Uint8Array"] = 38] = "Uint8Array";
|
|
77
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 39] = "Uint8ArrayMinByteLength";
|
|
78
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 40] = "Uint8ArrayMaxByteLength";
|
|
79
|
+
ValueErrorType[ValueErrorType["Void"] = 41] = "Void";
|
|
79
80
|
})(ValueErrorType = exports.ValueErrorType || (exports.ValueErrorType = {}));
|
|
80
81
|
// -------------------------------------------------------------------
|
|
81
82
|
// ValueErrors
|
|
@@ -149,6 +150,9 @@ var ValueErrors;
|
|
|
149
150
|
return yield { type: ValueErrorType.Literal, schema, path, value, message: `Expected ${error}` };
|
|
150
151
|
}
|
|
151
152
|
}
|
|
153
|
+
function* Never(schema, references, path, value) {
|
|
154
|
+
yield { type: ValueErrorType.Never, schema, path, value, message: `Value cannot be validated` };
|
|
155
|
+
}
|
|
152
156
|
function* Null(schema, references, path, value) {
|
|
153
157
|
if (!(value === null)) {
|
|
154
158
|
return yield { type: ValueErrorType.Null, schema, path, value, message: `Expected null` };
|
|
@@ -337,6 +341,8 @@ var ValueErrors;
|
|
|
337
341
|
return yield* Integer(anySchema, anyReferences, path, value);
|
|
338
342
|
case 'Literal':
|
|
339
343
|
return yield* Literal(anySchema, anyReferences, path, value);
|
|
344
|
+
case 'Never':
|
|
345
|
+
return yield* Never(anySchema, anyReferences, path, value);
|
|
340
346
|
case 'Null':
|
|
341
347
|
return yield* Null(anySchema, anyReferences, path, value);
|
|
342
348
|
case 'Number':
|
package/guard/guard.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ export declare namespace TypeGuard {
|
|
|
19
19
|
function TInteger(schema: unknown): schema is Types.TInteger;
|
|
20
20
|
/** Returns true if the given schema is TLiteral */
|
|
21
21
|
function TLiteral(schema: unknown): schema is Types.TLiteral;
|
|
22
|
+
/** Returns true if the given schema is TNever */
|
|
23
|
+
function TNever(schema: unknown): schema is Types.TNever;
|
|
22
24
|
/** Returns true if the given schema is TNull */
|
|
23
25
|
function TNull(schema: unknown): schema is Types.TNull;
|
|
24
26
|
/** Returns true if the given schema is TNumber */
|
package/guard/guard.js
CHANGED
|
@@ -153,6 +153,22 @@ var TypeGuard;
|
|
|
153
153
|
return IsObject(schema) && schema[Types.Kind] === 'Literal' && IsOptionalString(schema.$id) && (IsString(schema.const) || IsNumber(schema.const) || IsBoolean(schema.const));
|
|
154
154
|
}
|
|
155
155
|
TypeGuard.TLiteral = TLiteral;
|
|
156
|
+
/** Returns true if the given schema is TNever */
|
|
157
|
+
function TNever(schema) {
|
|
158
|
+
return (IsObject(schema) &&
|
|
159
|
+
schema[Types.Kind] === 'Never' &&
|
|
160
|
+
IsArray(schema.allOf) &&
|
|
161
|
+
schema.allOf.length === 2 &&
|
|
162
|
+
IsObject(schema.allOf[0]) &&
|
|
163
|
+
IsString(schema.allOf[0].type) &&
|
|
164
|
+
schema.allOf[0].type === 'number' &&
|
|
165
|
+
schema.allOf[0].const === 0 &&
|
|
166
|
+
IsObject(schema.allOf[1]) &&
|
|
167
|
+
IsString(schema.allOf[1].type) &&
|
|
168
|
+
schema.allOf[1].type === 'number' &&
|
|
169
|
+
schema.allOf[1].const === 1);
|
|
170
|
+
}
|
|
171
|
+
TypeGuard.TNever = TNever;
|
|
156
172
|
/** Returns true if the given schema is TNull */
|
|
157
173
|
function TNull(schema) {
|
|
158
174
|
return IsObject(schema) && schema[Types.Kind] === 'Null' && schema.type === 'null' && IsOptionalString(schema.$id);
|
|
@@ -302,6 +318,7 @@ var TypeGuard;
|
|
|
302
318
|
TFunction(schema) ||
|
|
303
319
|
TInteger(schema) ||
|
|
304
320
|
TLiteral(schema) ||
|
|
321
|
+
TNever(schema) ||
|
|
305
322
|
TNull(schema) ||
|
|
306
323
|
TNumber(schema) ||
|
|
307
324
|
TObject(schema) ||
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -302,6 +302,17 @@ The following table lists the standard TypeBox types.
|
|
|
302
302
|
│ │ │ } │
|
|
303
303
|
│ │ │ │
|
|
304
304
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
305
|
+
│ const T = Type.Never() │ type T = never │ const T = { │
|
|
306
|
+
│ │ │ allOf: [{ │
|
|
307
|
+
│ │ │ type: 'number' │
|
|
308
|
+
│ │ │ const: 0 │
|
|
309
|
+
│ │ │ }, { │
|
|
310
|
+
│ │ │ type: 'number' │
|
|
311
|
+
│ │ │ const: 1 │
|
|
312
|
+
│ │ │ }] │
|
|
313
|
+
│ │ │ } │
|
|
314
|
+
│ │ │ │
|
|
315
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
305
316
|
│ const T = Type.Record( │ type T = Record< │ const T = { │
|
|
306
317
|
│ Type.String(), │ string, │ type: 'object', │
|
|
307
318
|
│ Type.Number() │ number, │ patternProperties: { │
|
|
@@ -428,7 +439,7 @@ const T = Type.Array(Type.Integer(), { minItems: 5 })
|
|
|
428
439
|
|
|
429
440
|
### Extended
|
|
430
441
|
|
|
431
|
-
In addition to JSON schema types, TypeBox provides several extended types that allow for `function` and `constructor` types
|
|
442
|
+
In addition to JSON schema types, TypeBox provides several extended types that allow for the composition of `function` and `constructor` types. These additional types are not valid JSON Schema and will not validate using typical JSON Schema validation. However, these types can be used to frame JSON schema and describe callable interfaces that may receive JSON validated data. These types are as follows.
|
|
432
443
|
|
|
433
444
|
```typescript
|
|
434
445
|
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
|
package/typebox.d.ts
CHANGED
|
@@ -125,6 +125,15 @@ export interface TLiteral<T extends TLiteralValue = TLiteralValue> extends TSche
|
|
|
125
125
|
static: T;
|
|
126
126
|
const: T;
|
|
127
127
|
}
|
|
128
|
+
export interface TNever extends TSchema {
|
|
129
|
+
[Kind]: 'Never';
|
|
130
|
+
static: never;
|
|
131
|
+
allOf: [{
|
|
132
|
+
const: 0;
|
|
133
|
+
}, {
|
|
134
|
+
const: 1;
|
|
135
|
+
}];
|
|
136
|
+
}
|
|
128
137
|
export interface TNull extends TSchema {
|
|
129
138
|
[Kind]: 'Null';
|
|
130
139
|
static: null;
|
|
@@ -323,6 +332,8 @@ export declare class TypeBuilder {
|
|
|
323
332
|
KeyOf<T extends TObject>(object: T, options?: SchemaOptions): TUnion<TKeyOf<T>>;
|
|
324
333
|
/** Creates a literal type. */
|
|
325
334
|
Literal<T extends TLiteralValue>(value: T, options?: SchemaOptions): TLiteral<T>;
|
|
335
|
+
/** Creates a never type */
|
|
336
|
+
Never(options?: SchemaOptions): TNever;
|
|
326
337
|
/** Creates a null type */
|
|
327
338
|
Null(options?: SchemaOptions): TNull;
|
|
328
339
|
/** Creates a number type */
|
|
@@ -366,6 +377,7 @@ export declare class TypeBuilder {
|
|
|
366
377
|
/** Creates a undefined type */
|
|
367
378
|
Undefined(options?: SchemaOptions): TUndefined;
|
|
368
379
|
/** Creates a union type */
|
|
380
|
+
Union(items: [], options?: SchemaOptions): TNever;
|
|
369
381
|
Union<T extends TSchema[]>(items: [...T], options?: SchemaOptions): TUnion<T>;
|
|
370
382
|
/** Creates a Uint8Array type */
|
|
371
383
|
Uint8Array(options?: Uint8ArrayOptions): TUint8Array;
|
package/typebox.js
CHANGED
|
@@ -153,6 +153,17 @@ class TypeBuilder {
|
|
|
153
153
|
Literal(value, options = {}) {
|
|
154
154
|
return this.Create({ ...options, [exports.Kind]: 'Literal', const: value, type: typeof value });
|
|
155
155
|
}
|
|
156
|
+
/** Creates a never type */
|
|
157
|
+
Never(options = {}) {
|
|
158
|
+
return this.Create({
|
|
159
|
+
...options,
|
|
160
|
+
[exports.Kind]: 'Never',
|
|
161
|
+
allOf: [
|
|
162
|
+
{ type: 'number', const: 0 },
|
|
163
|
+
{ type: 'number', const: 1 },
|
|
164
|
+
],
|
|
165
|
+
});
|
|
166
|
+
}
|
|
156
167
|
/** Creates a null type */
|
|
157
168
|
Null(options = {}) {
|
|
158
169
|
return this.Create({ ...options, [exports.Kind]: 'Null', type: 'null' });
|
|
@@ -315,9 +326,8 @@ class TypeBuilder {
|
|
|
315
326
|
Undefined(options = {}) {
|
|
316
327
|
return this.Create({ ...options, [exports.Kind]: 'Undefined', type: 'object', specialized: 'Undefined' });
|
|
317
328
|
}
|
|
318
|
-
/** Creates a union type */
|
|
319
329
|
Union(items, options = {}) {
|
|
320
|
-
return this.Create({ ...options, [exports.Kind]: 'Union', anyOf: items });
|
|
330
|
+
return items.length === 0 ? exports.Type.Never({ ...options }) : this.Create({ ...options, [exports.Kind]: 'Union', anyOf: items });
|
|
321
331
|
}
|
|
322
332
|
/** Creates a Uint8Array type */
|
|
323
333
|
Uint8Array(options = {}) {
|
package/value/cast.d.ts
CHANGED
|
@@ -3,6 +3,10 @@ export declare class ValueCastUnknownTypeError extends Error {
|
|
|
3
3
|
readonly schema: Types.TSchema;
|
|
4
4
|
constructor(schema: Types.TSchema);
|
|
5
5
|
}
|
|
6
|
+
export declare class ValueCastNeverTypeError extends Error {
|
|
7
|
+
readonly schema: Types.TSchema;
|
|
8
|
+
constructor(schema: Types.TSchema);
|
|
9
|
+
}
|
|
6
10
|
export declare namespace ValueCast {
|
|
7
11
|
function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any): any;
|
|
8
12
|
function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: any): Types.Static<T>;
|
package/value/cast.js
CHANGED
|
@@ -27,47 +27,48 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueCast = exports.ValueCastUnknownTypeError = void 0;
|
|
30
|
+
exports.ValueCast = exports.ValueCastNeverTypeError = exports.ValueCastUnknownTypeError = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
const create_1 = require("./create");
|
|
33
33
|
const check_1 = require("./check");
|
|
34
|
-
// --------------------------------------------------------------------------
|
|
35
|
-
// Specialized Union Cast. Because a union can be one of many varying types
|
|
36
|
-
// with properties potentially overlapping, we need a strategy to determine
|
|
37
|
-
// which of those types we should cast into. This strategy needs to factor
|
|
38
|
-
// the value provided by the user to make this decision.
|
|
39
|
-
//
|
|
40
|
-
// The following will score each union type found within the types anyOf
|
|
41
|
-
// array. Typically this is executed for objects only, so the score is a
|
|
42
|
-
// essentially a tally of how many properties are valid. The reasoning
|
|
43
|
-
// here is the discriminator field would tip the scales in favor of that
|
|
44
|
-
// union if other properties overlap and match.
|
|
45
|
-
// --------------------------------------------------------------------------
|
|
46
34
|
var UnionValueCast;
|
|
47
35
|
(function (UnionValueCast) {
|
|
36
|
+
// ----------------------------------------------------------------------------------------------
|
|
37
|
+
// The following will score a schema against a value. For objects, the score is the tally of
|
|
38
|
+
// points awarded for each property of the value. Property points are (1.0 / propertyCount)
|
|
39
|
+
// to prevent large property counts biasing results. Properties that match literal values are
|
|
40
|
+
// maximally awarded as literals are typically used as union discriminator fields.
|
|
41
|
+
// ----------------------------------------------------------------------------------------------
|
|
48
42
|
function Score(schema, references, value) {
|
|
49
|
-
let score = 0;
|
|
50
43
|
if (schema[Types.Kind] === 'Object' && typeof value === 'object' && value !== null) {
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
|
|
44
|
+
const object = schema;
|
|
45
|
+
const keys = Object.keys(value);
|
|
46
|
+
const entries = globalThis.Object.entries(object.properties);
|
|
47
|
+
const [point, max] = [1 / entries.length, entries.length];
|
|
48
|
+
return entries.reduce((acc, [key, schema]) => {
|
|
49
|
+
const literal = schema[Types.Kind] === 'Literal' && schema.const === value[key] ? max : 0;
|
|
50
|
+
const checks = check_1.ValueCheck.Check(schema, references, value[key]) ? point : 0;
|
|
51
|
+
const exists = keys.includes(key) ? point : 0;
|
|
52
|
+
return acc + (literal + checks + exists);
|
|
53
|
+
}, 0);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
return check_1.ValueCheck.Check(schema, references, value) ? 1 : 0;
|
|
54
57
|
}
|
|
55
|
-
return score;
|
|
56
58
|
}
|
|
57
|
-
function Select(
|
|
58
|
-
let select =
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const score = Score(subschema, references, value);
|
|
59
|
+
function Select(union, references, value) {
|
|
60
|
+
let [select, best] = [union.anyOf[0], 0];
|
|
61
|
+
for (const schema of union.anyOf) {
|
|
62
|
+
const score = Score(schema, references, value);
|
|
62
63
|
if (score > best) {
|
|
63
|
-
select =
|
|
64
|
+
select = schema;
|
|
64
65
|
best = score;
|
|
65
66
|
}
|
|
66
67
|
}
|
|
67
68
|
return select;
|
|
68
69
|
}
|
|
69
|
-
function Create(
|
|
70
|
-
return check_1.ValueCheck.Check(
|
|
70
|
+
function Create(union, references, value) {
|
|
71
|
+
return check_1.ValueCheck.Check(union, references, value) ? value : ValueCast.Cast(Select(union, references, value), references, value);
|
|
71
72
|
}
|
|
72
73
|
UnionValueCast.Create = Create;
|
|
73
74
|
})(UnionValueCast || (UnionValueCast = {}));
|
|
@@ -78,6 +79,13 @@ class ValueCastUnknownTypeError extends Error {
|
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
|
|
82
|
+
class ValueCastNeverTypeError extends Error {
|
|
83
|
+
constructor(schema) {
|
|
84
|
+
super('ValueCast: Never types cannot be cast');
|
|
85
|
+
this.schema = schema;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.ValueCastNeverTypeError = ValueCastNeverTypeError;
|
|
81
89
|
var ValueCast;
|
|
82
90
|
(function (ValueCast) {
|
|
83
91
|
// -----------------------------------------------------------
|
|
@@ -161,6 +169,9 @@ var ValueCast;
|
|
|
161
169
|
function Literal(schema, references, value) {
|
|
162
170
|
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
163
171
|
}
|
|
172
|
+
function Never(schema, references, value) {
|
|
173
|
+
throw new ValueCastNeverTypeError(schema);
|
|
174
|
+
}
|
|
164
175
|
function Null(schema, references, value) {
|
|
165
176
|
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
166
177
|
}
|
|
@@ -261,6 +272,8 @@ var ValueCast;
|
|
|
261
272
|
return Integer(anySchema, anyReferences, value);
|
|
262
273
|
case 'Literal':
|
|
263
274
|
return Literal(anySchema, anyReferences, value);
|
|
275
|
+
case 'Never':
|
|
276
|
+
return Never(anySchema, anyReferences, value);
|
|
264
277
|
case 'Null':
|
|
265
278
|
return Null(anySchema, anyReferences, value);
|
|
266
279
|
case 'Number':
|
package/value/check.js
CHANGED
|
@@ -93,6 +93,9 @@ var ValueCheck;
|
|
|
93
93
|
function Literal(schema, references, value) {
|
|
94
94
|
return value === schema.const;
|
|
95
95
|
}
|
|
96
|
+
function Never(schema, references, value) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
96
99
|
function Null(schema, references, value) {
|
|
97
100
|
return value === null;
|
|
98
101
|
}
|
|
@@ -275,6 +278,8 @@ var ValueCheck;
|
|
|
275
278
|
return Integer(anySchema, anyReferences, value);
|
|
276
279
|
case 'Literal':
|
|
277
280
|
return Literal(anySchema, anyReferences, value);
|
|
281
|
+
case 'Never':
|
|
282
|
+
return Never(anySchema, anyReferences, value);
|
|
278
283
|
case 'Null':
|
|
279
284
|
return Null(anySchema, anyReferences, value);
|
|
280
285
|
case 'Number':
|
package/value/create.d.ts
CHANGED
|
@@ -3,6 +3,10 @@ export declare class ValueCreateUnknownTypeError extends Error {
|
|
|
3
3
|
readonly schema: Types.TSchema;
|
|
4
4
|
constructor(schema: Types.TSchema);
|
|
5
5
|
}
|
|
6
|
+
export declare class ValueCreateNeverTypeError extends Error {
|
|
7
|
+
readonly schema: Types.TSchema;
|
|
8
|
+
constructor(schema: Types.TSchema);
|
|
9
|
+
}
|
|
6
10
|
export declare namespace ValueCreate {
|
|
7
11
|
/** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
|
|
8
12
|
function Visit<T extends Types.TSchema>(schema: T, references: Types.TSchema[]): Types.Static<T>;
|
package/value/create.js
CHANGED
|
@@ -27,7 +27,7 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueCreate = exports.ValueCreateUnknownTypeError = void 0;
|
|
30
|
+
exports.ValueCreate = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
class ValueCreateUnknownTypeError extends Error {
|
|
33
33
|
constructor(schema) {
|
|
@@ -36,6 +36,13 @@ class ValueCreateUnknownTypeError extends Error {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
exports.ValueCreateUnknownTypeError = ValueCreateUnknownTypeError;
|
|
39
|
+
class ValueCreateNeverTypeError extends Error {
|
|
40
|
+
constructor(schema) {
|
|
41
|
+
super('ValueCreate: Never types cannot be created');
|
|
42
|
+
this.schema = schema;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.ValueCreateNeverTypeError = ValueCreateNeverTypeError;
|
|
39
46
|
var ValueCreate;
|
|
40
47
|
(function (ValueCreate) {
|
|
41
48
|
function Any(schema, references) {
|
|
@@ -125,6 +132,9 @@ var ValueCreate;
|
|
|
125
132
|
function Literal(schema, references) {
|
|
126
133
|
return schema.const;
|
|
127
134
|
}
|
|
135
|
+
function Never(schema, references) {
|
|
136
|
+
throw new ValueCreateNeverTypeError(schema);
|
|
137
|
+
}
|
|
128
138
|
function Null(schema, references) {
|
|
129
139
|
return null;
|
|
130
140
|
}
|
|
@@ -303,6 +313,8 @@ var ValueCreate;
|
|
|
303
313
|
return Integer(anySchema, anyReferences);
|
|
304
314
|
case 'Literal':
|
|
305
315
|
return Literal(anySchema, anyReferences);
|
|
316
|
+
case 'Never':
|
|
317
|
+
return Never(anySchema, anyReferences);
|
|
306
318
|
case 'Null':
|
|
307
319
|
return Null(anySchema, anyReferences);
|
|
308
320
|
case 'Number':
|