@sinclair/typebox 0.24.37 → 0.24.40
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 +13 -1
- package/typebox.js +12 -2
- package/value/cast.d.ts +17 -0
- package/value/cast.js +84 -33
- 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
|
@@ -104,7 +104,7 @@ export declare type IntersectEvaluate<T extends readonly TSchema[], P extends un
|
|
|
104
104
|
[K in keyof T]: T[K] extends TSchema ? Static<T[K], P> : never;
|
|
105
105
|
};
|
|
106
106
|
export declare type IntersectProperties<T extends readonly TObject[]> = {
|
|
107
|
-
[K in keyof T]: T[K]
|
|
107
|
+
[K in keyof T]: T[K] extends TObject<infer P> ? P : {};
|
|
108
108
|
};
|
|
109
109
|
export interface TIntersect<T extends TObject[] = TObject[]> extends TObject {
|
|
110
110
|
static: IntersectReduce<unknown, IntersectEvaluate<T, this['params']>>;
|
|
@@ -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
|
@@ -1,4 +1,21 @@
|
|
|
1
1
|
import * as Types from '../typebox';
|
|
2
|
+
export declare class ValueCastReferenceTypeError extends Error {
|
|
3
|
+
readonly schema: Types.TRef | Types.TSelf;
|
|
4
|
+
constructor(schema: Types.TRef | Types.TSelf);
|
|
5
|
+
}
|
|
6
|
+
export declare class ValueCastArrayUniqueItemsTypeError extends Error {
|
|
7
|
+
readonly schema: Types.TSchema;
|
|
8
|
+
readonly value: unknown;
|
|
9
|
+
constructor(schema: Types.TSchema, value: unknown);
|
|
10
|
+
}
|
|
11
|
+
export declare class ValueCastNeverTypeError extends Error {
|
|
12
|
+
readonly schema: Types.TSchema;
|
|
13
|
+
constructor(schema: Types.TSchema);
|
|
14
|
+
}
|
|
15
|
+
export declare class ValueCastRecursiveTypeError extends Error {
|
|
16
|
+
readonly schema: Types.TSchema;
|
|
17
|
+
constructor(schema: Types.TSchema);
|
|
18
|
+
}
|
|
2
19
|
export declare class ValueCastUnknownTypeError extends Error {
|
|
3
20
|
readonly schema: Types.TSchema;
|
|
4
21
|
constructor(schema: Types.TSchema);
|
package/value/cast.js
CHANGED
|
@@ -27,50 +27,83 @@ 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.ValueCastUnknownTypeError = exports.ValueCastRecursiveTypeError = exports.ValueCastNeverTypeError = exports.ValueCastArrayUniqueItemsTypeError = exports.ValueCastReferenceTypeError = 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 = {}));
|
|
75
|
+
// -----------------------------------------------------------
|
|
76
|
+
// Errors
|
|
77
|
+
// -----------------------------------------------------------
|
|
78
|
+
class ValueCastReferenceTypeError extends Error {
|
|
79
|
+
constructor(schema) {
|
|
80
|
+
super(`ValueCast: Cannot locate referenced schema with $id '${schema.$ref}'`);
|
|
81
|
+
this.schema = schema;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.ValueCastReferenceTypeError = ValueCastReferenceTypeError;
|
|
85
|
+
class ValueCastArrayUniqueItemsTypeError extends Error {
|
|
86
|
+
constructor(schema, value) {
|
|
87
|
+
super('ValueCast: Array cast produced invalid data due to uniqueItems constraint');
|
|
88
|
+
this.schema = schema;
|
|
89
|
+
this.value = value;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.ValueCastArrayUniqueItemsTypeError = ValueCastArrayUniqueItemsTypeError;
|
|
93
|
+
class ValueCastNeverTypeError extends Error {
|
|
94
|
+
constructor(schema) {
|
|
95
|
+
super('ValueCast: Never types cannot be cast');
|
|
96
|
+
this.schema = schema;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.ValueCastNeverTypeError = ValueCastNeverTypeError;
|
|
100
|
+
class ValueCastRecursiveTypeError extends Error {
|
|
101
|
+
constructor(schema) {
|
|
102
|
+
super('ValueCast.Recursive: Cannot cast recursive schemas');
|
|
103
|
+
this.schema = schema;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.ValueCastRecursiveTypeError = ValueCastRecursiveTypeError;
|
|
74
107
|
class ValueCastUnknownTypeError extends Error {
|
|
75
108
|
constructor(schema) {
|
|
76
109
|
super('ValueCast: Unknown type');
|
|
@@ -81,8 +114,11 @@ exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
|
|
|
81
114
|
var ValueCast;
|
|
82
115
|
(function (ValueCast) {
|
|
83
116
|
// -----------------------------------------------------------
|
|
84
|
-
//
|
|
117
|
+
// Guards
|
|
85
118
|
// -----------------------------------------------------------
|
|
119
|
+
function IsArray(value) {
|
|
120
|
+
return typeof value === 'object' && globalThis.Array.isArray(value);
|
|
121
|
+
}
|
|
86
122
|
function IsString(value) {
|
|
87
123
|
return typeof value === 'string';
|
|
88
124
|
}
|
|
@@ -107,6 +143,9 @@ var ValueCast;
|
|
|
107
143
|
function IsValueFalse(value) {
|
|
108
144
|
return value === false || (IsNumber(value) && value === 0) || (IsBigInt(value) && value === 0n) || (IsString(value) && (value.toLowerCase() === 'false' || value === '0'));
|
|
109
145
|
}
|
|
146
|
+
// -----------------------------------------------------------
|
|
147
|
+
// Convert
|
|
148
|
+
// -----------------------------------------------------------
|
|
110
149
|
function TryConvertString(value) {
|
|
111
150
|
return IsValueToString(value) ? value.toString() : value;
|
|
112
151
|
}
|
|
@@ -128,9 +167,16 @@ var ValueCast;
|
|
|
128
167
|
function Array(schema, references, value) {
|
|
129
168
|
if (check_1.ValueCheck.Check(schema, references, value))
|
|
130
169
|
return value;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
170
|
+
const created = IsArray(value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
171
|
+
const minimum = IsNumber(schema.minItems) && created.length < schema.minItems ? [...created, ...globalThis.Array.from({ length: schema.minItems - created.length }, () => null)] : created;
|
|
172
|
+
const maximum = IsNumber(schema.maxItems) && minimum.length > schema.maxItems ? minimum.slice(0, schema.maxItems) : minimum;
|
|
173
|
+
const casted = maximum.map((value) => Visit(schema.items, references, value));
|
|
174
|
+
if (schema.uniqueItems !== true)
|
|
175
|
+
return casted;
|
|
176
|
+
const unique = [...new Set(casted)];
|
|
177
|
+
if (!check_1.ValueCheck.Check(schema, references, unique))
|
|
178
|
+
throw new ValueCastArrayUniqueItemsTypeError(schema, unique);
|
|
179
|
+
return unique;
|
|
134
180
|
}
|
|
135
181
|
function Boolean(schema, references, value) {
|
|
136
182
|
const conversion = TryConvertBoolean(value);
|
|
@@ -161,6 +207,9 @@ var ValueCast;
|
|
|
161
207
|
function Literal(schema, references, value) {
|
|
162
208
|
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
163
209
|
}
|
|
210
|
+
function Never(schema, references, value) {
|
|
211
|
+
throw new ValueCastNeverTypeError(schema);
|
|
212
|
+
}
|
|
164
213
|
function Null(schema, references, value) {
|
|
165
214
|
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
166
215
|
}
|
|
@@ -199,18 +248,18 @@ var ValueCast;
|
|
|
199
248
|
return result;
|
|
200
249
|
}
|
|
201
250
|
function Recursive(schema, references, value) {
|
|
202
|
-
throw new
|
|
251
|
+
throw new ValueCastRecursiveTypeError(schema);
|
|
203
252
|
}
|
|
204
253
|
function Ref(schema, references, value) {
|
|
205
254
|
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
206
255
|
if (reference === undefined)
|
|
207
|
-
throw new
|
|
256
|
+
throw new ValueCastReferenceTypeError(schema);
|
|
208
257
|
return Visit(reference, references, value);
|
|
209
258
|
}
|
|
210
259
|
function Self(schema, references, value) {
|
|
211
260
|
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
212
261
|
if (reference === undefined)
|
|
213
|
-
throw new
|
|
262
|
+
throw new ValueCastReferenceTypeError(schema);
|
|
214
263
|
return Visit(reference, references, value);
|
|
215
264
|
}
|
|
216
265
|
function String(schema, references, value) {
|
|
@@ -261,6 +310,8 @@ var ValueCast;
|
|
|
261
310
|
return Integer(anySchema, anyReferences, value);
|
|
262
311
|
case 'Literal':
|
|
263
312
|
return Literal(anySchema, anyReferences, value);
|
|
313
|
+
case 'Never':
|
|
314
|
+
return Never(anySchema, anyReferences, value);
|
|
264
315
|
case 'Null':
|
|
265
316
|
return Null(anySchema, anyReferences, value);
|
|
266
317
|
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':
|