@sinclair/typebox 0.24.21 → 0.24.24
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 +8 -1
- package/compiler/compiler.js +130 -55
- package/compiler/index.d.ts +1 -1
- package/compiler/index.js +3 -0
- package/errors/errors.d.ts +56 -0
- package/errors/errors.js +381 -0
- package/errors/index.d.ts +1 -0
- package/errors/index.js +44 -0
- package/guard/guard.js +82 -14
- package/package.json +1 -1
- package/readme.md +84 -104
- package/typebox.js +4 -2
- package/value/cast.d.ts +4 -0
- package/value/cast.js +75 -52
- package/value/check.d.ts +4 -0
- package/value/check.js +75 -49
- package/value/create.d.ts +4 -0
- package/value/create.js +75 -53
- package/value/index.d.ts +1 -1
- package/value/index.js +3 -0
- package/value/value.d.ts +1 -1
- package/value/value.js +2 -2
- package/value/errors.d.ts +0 -10
- package/value/errors.js +0 -305
package/compiler/compiler.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ValueError } from '../
|
|
1
|
+
import { ValueError } from '../errors/index';
|
|
2
2
|
import * as Types from '../typebox';
|
|
3
3
|
export declare type CheckFunction = (value: unknown) => boolean;
|
|
4
4
|
export declare class TypeCheck<T extends Types.TSchema> {
|
|
@@ -14,6 +14,13 @@ export declare class TypeCheck<T extends Types.TSchema> {
|
|
|
14
14
|
/** Returns true if the value matches the given type. */
|
|
15
15
|
Check(value: unknown): value is Types.Static<T>;
|
|
16
16
|
}
|
|
17
|
+
export declare namespace Property {
|
|
18
|
+
function Check(propertyName: string): boolean;
|
|
19
|
+
}
|
|
20
|
+
export declare class TypeCompilerInvalidTypeError extends Error {
|
|
21
|
+
readonly schema: Types.TSchema;
|
|
22
|
+
constructor(schema: Types.TSchema);
|
|
23
|
+
}
|
|
17
24
|
/** Compiles Types for Runtime Type Checking */
|
|
18
25
|
export declare namespace TypeCompiler {
|
|
19
26
|
/** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */
|
package/compiler/compiler.js
CHANGED
|
@@ -27,9 +27,9 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.TypeCompiler = exports.TypeCheck = void 0;
|
|
31
|
-
const
|
|
32
|
-
const
|
|
30
|
+
exports.TypeCompiler = exports.TypeCompilerInvalidTypeError = exports.Property = exports.TypeCheck = void 0;
|
|
31
|
+
const index_1 = require("../errors/index");
|
|
32
|
+
const index_2 = require("../guard/index");
|
|
33
33
|
// -------------------------------------------------------------------
|
|
34
34
|
// TypeCheck
|
|
35
35
|
// -------------------------------------------------------------------
|
|
@@ -46,7 +46,7 @@ class TypeCheck {
|
|
|
46
46
|
}
|
|
47
47
|
/** Returns an iterator for each error in this value. */
|
|
48
48
|
Errors(value) {
|
|
49
|
-
return
|
|
49
|
+
return index_1.ValueErrors.Errors(this.schema, this.references, value);
|
|
50
50
|
}
|
|
51
51
|
/** Returns true if the value matches the given type. */
|
|
52
52
|
Check(value) {
|
|
@@ -55,8 +55,60 @@ class TypeCheck {
|
|
|
55
55
|
}
|
|
56
56
|
exports.TypeCheck = TypeCheck;
|
|
57
57
|
// -------------------------------------------------------------------
|
|
58
|
+
// Property
|
|
59
|
+
// -------------------------------------------------------------------
|
|
60
|
+
var Property;
|
|
61
|
+
(function (Property) {
|
|
62
|
+
function DollarSign(code) {
|
|
63
|
+
return code === 36;
|
|
64
|
+
}
|
|
65
|
+
function Underscore(code) {
|
|
66
|
+
return code === 95;
|
|
67
|
+
}
|
|
68
|
+
function Numeric(code) {
|
|
69
|
+
return code >= 48 && code <= 57;
|
|
70
|
+
}
|
|
71
|
+
function Alpha(code) {
|
|
72
|
+
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
|
|
73
|
+
}
|
|
74
|
+
function AssertEscapeCharacters(propertyName) {
|
|
75
|
+
for (let i = 0; i < propertyName.length; i++) {
|
|
76
|
+
const code = propertyName.charCodeAt(i);
|
|
77
|
+
if ((code >= 7 && code <= 13) || code === 27 || code === 127) {
|
|
78
|
+
throw Error('Property: Invalid escape character found in property key');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function Check(propertyName) {
|
|
83
|
+
AssertEscapeCharacters(propertyName);
|
|
84
|
+
if (propertyName.length === 0)
|
|
85
|
+
return false;
|
|
86
|
+
{
|
|
87
|
+
const code = propertyName.charCodeAt(0);
|
|
88
|
+
if (!(DollarSign(code) || Underscore(code) || Alpha(code))) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
for (let i = 1; i < propertyName.length; i++) {
|
|
93
|
+
const code = propertyName.charCodeAt(i);
|
|
94
|
+
if (!(DollarSign(code) || Underscore(code) || Alpha(code) || Numeric(code))) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
Property.Check = Check;
|
|
101
|
+
})(Property = exports.Property || (exports.Property = {}));
|
|
102
|
+
// -------------------------------------------------------------------
|
|
58
103
|
// TypeCompiler
|
|
59
104
|
// -------------------------------------------------------------------
|
|
105
|
+
class TypeCompilerInvalidTypeError extends Error {
|
|
106
|
+
constructor(schema) {
|
|
107
|
+
super('TypeCompiler: Invalid type');
|
|
108
|
+
this.schema = schema;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.TypeCompilerInvalidTypeError = TypeCompilerInvalidTypeError;
|
|
60
112
|
/** Compiles Types for Runtime Type Checking */
|
|
61
113
|
var TypeCompiler;
|
|
62
114
|
(function (TypeCompiler) {
|
|
@@ -99,11 +151,14 @@ var TypeCompiler;
|
|
|
99
151
|
yield `(${value} <= ${schema.maximum})`;
|
|
100
152
|
}
|
|
101
153
|
function* Literal(schema, value) {
|
|
102
|
-
if (typeof schema.const === '
|
|
154
|
+
if (typeof schema.const === 'number' || typeof schema.const === 'boolean') {
|
|
155
|
+
yield `(${value} === ${schema.const})`;
|
|
156
|
+
}
|
|
157
|
+
else if (typeof schema.const === 'string') {
|
|
103
158
|
yield `(${value} === '${schema.const}')`;
|
|
104
159
|
}
|
|
105
160
|
else {
|
|
106
|
-
|
|
161
|
+
throw Error('TypeCompiler: Literal value should be string, number or boolean');
|
|
107
162
|
}
|
|
108
163
|
}
|
|
109
164
|
function* Null(schema, value) {
|
|
@@ -143,13 +198,14 @@ var TypeCompiler;
|
|
|
143
198
|
}
|
|
144
199
|
}
|
|
145
200
|
for (const propertyKey of propertyKeys) {
|
|
201
|
+
const memberExpression = Property.Check(propertyKey) ? `${value}.${propertyKey}` : `${value}['${propertyKey}']`;
|
|
146
202
|
const propertySchema = schema.properties[propertyKey];
|
|
147
203
|
if (schema.required && schema.required.includes(propertyKey)) {
|
|
148
|
-
yield* Visit(propertySchema,
|
|
204
|
+
yield* Visit(propertySchema, memberExpression);
|
|
149
205
|
}
|
|
150
206
|
else {
|
|
151
|
-
const expression = CreateExpression(propertySchema,
|
|
152
|
-
yield `(${
|
|
207
|
+
const expression = CreateExpression(propertySchema, memberExpression);
|
|
208
|
+
yield `(${memberExpression} === undefined ? true : (${expression}))`;
|
|
153
209
|
}
|
|
154
210
|
}
|
|
155
211
|
}
|
|
@@ -229,52 +285,71 @@ var TypeCompiler;
|
|
|
229
285
|
yield `(${name}(${value}))`;
|
|
230
286
|
return;
|
|
231
287
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
288
|
+
if (index_2.TypeGuard.TAny(schema)) {
|
|
289
|
+
return yield* Any(schema, value);
|
|
290
|
+
}
|
|
291
|
+
else if (index_2.TypeGuard.TArray(schema)) {
|
|
292
|
+
return yield* Array(schema, value);
|
|
293
|
+
}
|
|
294
|
+
else if (index_2.TypeGuard.TBoolean(schema)) {
|
|
295
|
+
return yield* Boolean(schema, value);
|
|
296
|
+
}
|
|
297
|
+
else if (index_2.TypeGuard.TConstructor(schema)) {
|
|
298
|
+
return yield* Constructor(schema, value);
|
|
299
|
+
}
|
|
300
|
+
else if (index_2.TypeGuard.TFunction(schema)) {
|
|
301
|
+
return yield* Function(schema, value);
|
|
302
|
+
}
|
|
303
|
+
else if (index_2.TypeGuard.TInteger(schema)) {
|
|
304
|
+
return yield* Integer(schema, value);
|
|
305
|
+
}
|
|
306
|
+
else if (index_2.TypeGuard.TLiteral(schema)) {
|
|
307
|
+
return yield* Literal(schema, value);
|
|
308
|
+
}
|
|
309
|
+
else if (index_2.TypeGuard.TNull(schema)) {
|
|
310
|
+
return yield* Null(schema, value);
|
|
311
|
+
}
|
|
312
|
+
else if (index_2.TypeGuard.TNumber(schema)) {
|
|
313
|
+
return yield* Number(schema, value);
|
|
314
|
+
}
|
|
315
|
+
else if (index_2.TypeGuard.TObject(schema)) {
|
|
316
|
+
return yield* Object(schema, value);
|
|
317
|
+
}
|
|
318
|
+
else if (index_2.TypeGuard.TPromise(schema)) {
|
|
319
|
+
return yield* Promise(schema, value);
|
|
320
|
+
}
|
|
321
|
+
else if (index_2.TypeGuard.TRecord(schema)) {
|
|
322
|
+
return yield* Record(schema, value);
|
|
323
|
+
}
|
|
324
|
+
else if (index_2.TypeGuard.TRef(schema)) {
|
|
325
|
+
return yield* Ref(schema, value);
|
|
326
|
+
}
|
|
327
|
+
else if (index_2.TypeGuard.TSelf(schema)) {
|
|
328
|
+
return yield* Self(schema, value);
|
|
329
|
+
}
|
|
330
|
+
else if (index_2.TypeGuard.TString(schema)) {
|
|
331
|
+
return yield* String(schema, value);
|
|
332
|
+
}
|
|
333
|
+
else if (index_2.TypeGuard.TTuple(schema)) {
|
|
334
|
+
return yield* Tuple(schema, value);
|
|
335
|
+
}
|
|
336
|
+
else if (index_2.TypeGuard.TUndefined(schema)) {
|
|
337
|
+
return yield* Undefined(schema, value);
|
|
338
|
+
}
|
|
339
|
+
else if (index_2.TypeGuard.TUnion(schema)) {
|
|
340
|
+
return yield* Union(schema, value);
|
|
341
|
+
}
|
|
342
|
+
else if (index_2.TypeGuard.TUint8Array(schema)) {
|
|
343
|
+
return yield* Uint8Array(schema, value);
|
|
344
|
+
}
|
|
345
|
+
else if (index_2.TypeGuard.TUnknown(schema)) {
|
|
346
|
+
return yield* Unknown(schema, value);
|
|
347
|
+
}
|
|
348
|
+
else if (index_2.TypeGuard.TVoid(schema)) {
|
|
349
|
+
return yield* Void(schema, value);
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
throw new TypeCompilerInvalidTypeError(schema);
|
|
278
353
|
}
|
|
279
354
|
}
|
|
280
355
|
// -------------------------------------------------------------------
|
package/compiler/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { ValueError, ValueErrorType } from '../errors/errors';
|
|
2
2
|
export * from './compiler';
|
package/compiler/index.js
CHANGED
|
@@ -41,4 +41,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
41
41
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
42
42
|
};
|
|
43
43
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
exports.ValueErrorType = void 0;
|
|
45
|
+
var errors_1 = require("../errors/errors");
|
|
46
|
+
Object.defineProperty(exports, "ValueErrorType", { enumerable: true, get: function () { return errors_1.ValueErrorType; } });
|
|
44
47
|
__exportStar(require("./compiler"), exports);
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import * as Types from '../typebox';
|
|
2
|
+
export declare enum ValueErrorType {
|
|
3
|
+
Array = 0,
|
|
4
|
+
ArrayMinItems = 1,
|
|
5
|
+
ArrayMaxItems = 2,
|
|
6
|
+
ArrayUniqueItems = 3,
|
|
7
|
+
Boolean = 4,
|
|
8
|
+
Function = 5,
|
|
9
|
+
Integer = 6,
|
|
10
|
+
IntegerMultipleOf = 7,
|
|
11
|
+
IntegerExclusiveMinimum = 8,
|
|
12
|
+
IntegerExclusiveMaximum = 9,
|
|
13
|
+
IntegerMinimum = 10,
|
|
14
|
+
IntegerMaximum = 11,
|
|
15
|
+
Literal = 12,
|
|
16
|
+
Null = 13,
|
|
17
|
+
Number = 14,
|
|
18
|
+
NumberMultipleOf = 15,
|
|
19
|
+
NumberExclusiveMinimum = 16,
|
|
20
|
+
NumberExclusiveMaximum = 17,
|
|
21
|
+
NumberMinumum = 18,
|
|
22
|
+
NumberMaximum = 19,
|
|
23
|
+
Object = 20,
|
|
24
|
+
ObjectMinProperties = 21,
|
|
25
|
+
ObjectMaxProperties = 22,
|
|
26
|
+
ObjectAdditionalProperties = 23,
|
|
27
|
+
Promise = 24,
|
|
28
|
+
RecordKeyNumeric = 25,
|
|
29
|
+
RecordKeyString = 26,
|
|
30
|
+
String = 27,
|
|
31
|
+
StringMinLength = 28,
|
|
32
|
+
StringMaxLength = 29,
|
|
33
|
+
StringPattern = 30,
|
|
34
|
+
TupleZeroLength = 31,
|
|
35
|
+
TupleLength = 32,
|
|
36
|
+
Undefined = 33,
|
|
37
|
+
Union = 34,
|
|
38
|
+
Uint8Array = 35,
|
|
39
|
+
Uint8ArrayMinByteLength = 36,
|
|
40
|
+
Uint8ArrayMaxByteLength = 37,
|
|
41
|
+
Void = 38
|
|
42
|
+
}
|
|
43
|
+
export interface ValueError {
|
|
44
|
+
type: ValueErrorType;
|
|
45
|
+
schema: Types.TSchema;
|
|
46
|
+
path: string;
|
|
47
|
+
value: unknown;
|
|
48
|
+
message: string;
|
|
49
|
+
}
|
|
50
|
+
export declare class ValueErrorsInvalidTypeError extends Error {
|
|
51
|
+
readonly schema: Types.TSchema;
|
|
52
|
+
constructor(schema: Types.TSchema);
|
|
53
|
+
}
|
|
54
|
+
export declare namespace ValueErrors {
|
|
55
|
+
function Errors<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: any): IterableIterator<ValueError>;
|
|
56
|
+
}
|