@sinclair/typebox 0.24.3 → 0.24.6

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.
@@ -1,21 +1,18 @@
1
+ import { TypeError } from './errors';
1
2
  import * as Types from '../typebox';
2
3
  export declare type CheckFunction = (value: unknown) => boolean;
3
- export declare class TypeCheckAssertError extends Error {
4
- readonly schema: Types.TSchema;
5
- readonly value: unknown;
6
- constructor(schema: Types.TSchema, value: unknown);
7
- }
8
4
  export declare class TypeCheck<T extends Types.TSchema> {
9
5
  private readonly schema;
6
+ private readonly additional;
10
7
  private readonly checkFunc;
11
8
  private readonly code;
12
- constructor(schema: T, checkFunc: CheckFunction, code: string);
13
- /** Returns the compiled validation code used to check this type. */
9
+ constructor(schema: T, additional: Types.TSchema[], checkFunc: CheckFunction, code: string);
10
+ /** Returns the generated validation code used to validate this type */
14
11
  Code(): string;
15
- /** Returns true if the value is valid. */
12
+ /** Returns an iterator for each type error found in this value */
13
+ Errors(value: unknown): Generator<TypeError>;
14
+ /** Returns true if the value matches the given type. */
16
15
  Check(value: unknown): value is Types.Static<T>;
17
- /** Asserts the given value and throws a TypeCheckAssertError if invalid. */
18
- Assert(value: unknown): void;
19
16
  }
20
17
  export declare namespace TypeCompiler {
21
18
  /** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */
@@ -27,43 +27,35 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.TypeCompiler = exports.TypeCheck = exports.TypeCheckAssertError = void 0;
30
+ exports.TypeCompiler = exports.TypeCheck = void 0;
31
+ const errors_1 = require("./errors");
31
32
  const Types = require("../typebox");
32
33
  // -------------------------------------------------------------------
33
34
  // TypeCheck
34
35
  // -------------------------------------------------------------------
35
- class TypeCheckAssertError extends Error {
36
- schema;
37
- value;
38
- constructor(schema, value) {
39
- super(`TypeCheckAssertError`);
40
- this.schema = Types.Type.Strict(schema);
41
- this.value = value;
42
- }
43
- }
44
- exports.TypeCheckAssertError = TypeCheckAssertError;
45
36
  class TypeCheck {
46
37
  schema;
38
+ additional;
47
39
  checkFunc;
48
40
  code;
49
- constructor(schema, checkFunc, code) {
41
+ constructor(schema, additional, checkFunc, code) {
50
42
  this.schema = schema;
43
+ this.additional = additional;
51
44
  this.checkFunc = checkFunc;
52
45
  this.code = code;
53
46
  }
54
- /** Returns the compiled validation code used to check this type. */
47
+ /** Returns the generated validation code used to validate this type */
55
48
  Code() {
56
49
  return this.code;
57
50
  }
58
- /** Returns true if the value is valid. */
51
+ /** Returns an iterator for each type error found in this value */
52
+ Errors(value) {
53
+ return errors_1.TypeErrors.Errors(this.schema, this.additional, value);
54
+ }
55
+ /** Returns true if the value matches the given type. */
59
56
  Check(value) {
60
57
  return this.checkFunc(value);
61
58
  }
62
- /** Asserts the given value and throws a TypeCheckAssertError if invalid. */
63
- Assert(value) {
64
- if (!this.checkFunc(value))
65
- throw new TypeCheckAssertError(this.schema, value);
66
- }
67
59
  }
68
60
  exports.TypeCheck = TypeCheck;
69
61
  // -------------------------------------------------------------------
@@ -95,7 +87,7 @@ var TypeCompiler;
95
87
  if (schema.multipleOf)
96
88
  yield `(${path} % ${schema.multipleOf} === 0)`;
97
89
  if (schema.exclusiveMinimum)
98
- yield `(${path} < ${schema.exclusiveMinimum})`;
90
+ yield `(${path} > ${schema.exclusiveMinimum})`;
99
91
  if (schema.exclusiveMaximum)
100
92
  yield `(${path} < ${schema.exclusiveMaximum})`;
101
93
  if (schema.minimum)
@@ -119,7 +111,7 @@ var TypeCompiler;
119
111
  if (schema.multipleOf)
120
112
  yield `(${path} % ${schema.multipleOf} === 0)`;
121
113
  if (schema.exclusiveMinimum)
122
- yield `(${path} < ${schema.exclusiveMinimum})`;
114
+ yield `(${path} > ${schema.exclusiveMinimum})`;
123
115
  if (schema.exclusiveMaximum)
124
116
  yield `(${path} < ${schema.exclusiveMaximum})`;
125
117
  if (schema.minimum)
@@ -338,7 +330,7 @@ var TypeCompiler;
338
330
  function Compile(schema, additional = []) {
339
331
  const code = Build(schema, additional);
340
332
  const func = globalThis.Function(code);
341
- return new TypeCheck(schema, func(), code);
333
+ return new TypeCheck(schema, additional, func(), code);
342
334
  }
343
335
  TypeCompiler.Compile = Compile;
344
336
  })(TypeCompiler = exports.TypeCompiler || (exports.TypeCompiler = {}));
@@ -0,0 +1,10 @@
1
+ import * as Types from '../typebox';
2
+ export interface TypeError {
3
+ schema: Types.TSchema;
4
+ path: string;
5
+ value: unknown;
6
+ message: string;
7
+ }
8
+ export declare namespace TypeErrors {
9
+ function Errors<T extends Types.TSchema>(schema: T, additional: Types.TSchema[], value: any): Generator<TypeError>;
10
+ }
@@ -0,0 +1,311 @@
1
+ "use strict";
2
+ /*--------------------------------------------------------------------------
3
+
4
+ @sinclair/typebox/compiler
5
+
6
+ The MIT License (MIT)
7
+
8
+ Copyright (c) 2022 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.TypeErrors = void 0;
31
+ const Types = require("../typebox");
32
+ var TypeErrors;
33
+ (function (TypeErrors) {
34
+ function* Any(schema, path, value) { }
35
+ function* Array(schema, path, value) {
36
+ if (!globalThis.Array.isArray(value)) {
37
+ return yield { schema, path, value, message: `Expected array` };
38
+ }
39
+ for (let i = 0; i < value.length; i++) {
40
+ yield* Visit(schema.items, `${path}/${i}`, value[i]);
41
+ }
42
+ }
43
+ function* Boolean(schema, path, value) {
44
+ if (!(typeof value === 'boolean')) {
45
+ return yield { schema, path, value, message: `Expected boolean` };
46
+ }
47
+ }
48
+ function* Constructor(schema, path, value) {
49
+ yield* Visit(schema.yields, path, value);
50
+ }
51
+ function* Function(schema, path, value) {
52
+ if (!(typeof value === 'function')) {
53
+ return yield { schema, path, value, message: `Expected function` };
54
+ }
55
+ }
56
+ function* Integer(schema, path, value) {
57
+ if (!(typeof value === 'number')) {
58
+ return yield { schema, path, value, message: `Expected number` };
59
+ }
60
+ if (!globalThis.Number.isInteger(value)) {
61
+ yield { schema, path, value, message: `Expected integer` };
62
+ }
63
+ if (schema.multipleOf && !(value % schema.multipleOf === 0)) {
64
+ yield { schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` };
65
+ }
66
+ if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) {
67
+ yield { schema, path, value, message: `Expected number to be greater than ${schema.exclusiveMinimum}` };
68
+ }
69
+ if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) {
70
+ yield { schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` };
71
+ }
72
+ if (schema.minimum && !(value >= schema.minimum)) {
73
+ yield { schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` };
74
+ }
75
+ if (schema.maximum && !(value <= schema.maximum)) {
76
+ yield { schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
77
+ }
78
+ }
79
+ function* Literal(schema, path, value) {
80
+ if (!(value === schema.const)) {
81
+ const error = typeof schema.const === 'string' ? `'${schema.const}'` : schema.const;
82
+ return yield { schema, path, value, message: `Expected ${error}` };
83
+ }
84
+ }
85
+ function* Null(schema, path, value) {
86
+ if (!(value === null)) {
87
+ return yield { schema, path, value, message: `Expected null` };
88
+ }
89
+ }
90
+ function* Number(schema, path, value) {
91
+ if (!(typeof value === 'number')) {
92
+ return yield { schema, path, value, message: `Expected number` };
93
+ }
94
+ if (schema.multipleOf && !(value % schema.multipleOf === 0)) {
95
+ yield { schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` };
96
+ }
97
+ if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) {
98
+ yield { schema, path, value, message: `Expected number to be greater than ${schema.exclusiveMinimum}` };
99
+ }
100
+ if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) {
101
+ yield { schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` };
102
+ }
103
+ if (schema.minimum && !(value >= schema.minimum)) {
104
+ yield { schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` };
105
+ }
106
+ if (schema.maximum && !(value <= schema.maximum)) {
107
+ yield { schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
108
+ }
109
+ }
110
+ function* Object(schema, path, value) {
111
+ if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
112
+ return yield { schema, path, value, message: `Expected object` };
113
+ }
114
+ if (schema.minProperties !== undefined && !(globalThis.Object.keys(value).length >= schema.minProperties)) {
115
+ yield { schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
116
+ }
117
+ if (schema.maxProperties !== undefined && !(globalThis.Object.keys(value).length <= schema.maxProperties)) {
118
+ yield { schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` };
119
+ }
120
+ const propertyKeys = globalThis.Object.keys(schema.properties);
121
+ if (schema.additionalProperties === false) {
122
+ // optimization: If the property key length matches the required keys length
123
+ // then we only need check that the values property key length matches that
124
+ // of the property key length. This is because exhaustive testing for values
125
+ // will occur in subsequent property tests.
126
+ if (schema.required && schema.required.length === propertyKeys.length && !(globalThis.Object.keys(value).length === propertyKeys.length)) {
127
+ yield { schema, path, value, message: 'Expected object must not have additional properties' };
128
+ }
129
+ else {
130
+ if (!globalThis.Object.keys(value).every((key) => propertyKeys.includes(key))) {
131
+ yield { schema, path, value, message: 'Expected object must not have additional properties' };
132
+ }
133
+ }
134
+ }
135
+ for (const propertyKey of propertyKeys) {
136
+ const propertySchema = schema.properties[propertyKey];
137
+ if (schema.required && schema.required.includes(propertyKey)) {
138
+ yield* Visit(propertySchema, `${path}/${propertyKey}`, value[propertyKey]);
139
+ }
140
+ else {
141
+ if (value[propertyKey] !== undefined) {
142
+ yield* Visit(propertySchema, `${path}/${propertyKey}`, value[propertyKey]);
143
+ }
144
+ }
145
+ }
146
+ }
147
+ function* Promise(schema, path, value) {
148
+ if (!(typeof value === 'object' && typeof value.then === 'function')) {
149
+ yield { schema, path, value, message: `Expected Promise` };
150
+ }
151
+ }
152
+ function* Record(schema, path, value) {
153
+ if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
154
+ return yield { schema, path, value, message: `Expected object` };
155
+ }
156
+ const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
157
+ const regex = new RegExp(keyPattern);
158
+ if (!globalThis.Object.keys(value).every((key) => regex.test(key))) {
159
+ const message = keyPattern === '^(0|[1-9][0-9]*)$' ? 'Expected all object property keys to be numeric' : 'Expected all object property keys to be strings';
160
+ return yield { schema, path, value, message };
161
+ }
162
+ for (const [propKey, propValue] of globalThis.Object.entries(value)) {
163
+ yield* Visit(valueSchema, `${path}/${propKey}`, propValue);
164
+ }
165
+ }
166
+ function* Ref(schema, path, value) {
167
+ if (!referenceMap.has(schema.$ref)) {
168
+ throw Error(`TypeErrors: Cannot locate referenced schema for $id '${schema.$id}'`);
169
+ }
170
+ const referencedSchema = referenceMap.get(schema.$ref);
171
+ yield* Visit(referencedSchema, path, value);
172
+ }
173
+ function* Self(schema, path, value) {
174
+ if (!referenceMap.has(schema.$ref)) {
175
+ throw Error(`TypeErrors: Cannot locate referenced schema for $id '${schema.$id}'`);
176
+ }
177
+ const referencedSchema = referenceMap.get(schema.$ref);
178
+ yield* Visit(referencedSchema, path, value);
179
+ }
180
+ function* String(schema, path, value) {
181
+ if (!(typeof value === 'string')) {
182
+ return yield { schema, path, value, message: 'Expected string' };
183
+ }
184
+ if (schema.pattern !== undefined) {
185
+ const regex = new RegExp(schema.pattern);
186
+ if (!regex.test(value)) {
187
+ yield { schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
188
+ }
189
+ }
190
+ }
191
+ function* Tuple(schema, path, value) {
192
+ if (!global.Array.isArray(value)) {
193
+ return yield { schema, path, value, message: 'Expected Array' };
194
+ }
195
+ if (schema.items === undefined && !(value.length === 0)) {
196
+ return yield { schema, path, value, message: 'Expected tuple to have 0 elements' };
197
+ }
198
+ if (!(value.length === schema.maxItems)) {
199
+ yield { schema, path, value, message: `Expected tuple to have ${schema.maxItems} elements` };
200
+ }
201
+ if (!schema.items) {
202
+ return;
203
+ }
204
+ for (let i = 0; i < schema.items.length; i++) {
205
+ yield* Visit(schema.items[i], `${path}/${i}`, value[i]);
206
+ }
207
+ }
208
+ function* Undefined(schema, path, value) {
209
+ if (!(value === undefined)) {
210
+ yield { schema, path, value, message: `Expected undefined` };
211
+ }
212
+ }
213
+ function* Union(schema, path, value) {
214
+ const errors = [];
215
+ for (const inner of schema.anyOf) {
216
+ const variantErrors = [...Visit(inner, path, value)];
217
+ if (variantErrors.length === 0)
218
+ return;
219
+ errors.push(...variantErrors);
220
+ }
221
+ for (const error of errors) {
222
+ yield error;
223
+ }
224
+ if (errors.length > 0) {
225
+ yield { schema, path, value, message: 'Expected value of union' };
226
+ }
227
+ }
228
+ function* Uint8Array(schema, path, value) {
229
+ if (!(value instanceof globalThis.Uint8Array)) {
230
+ return yield { schema, path, value, message: `Expected Uint8Array` };
231
+ }
232
+ if (schema.maxByteLength && !(value.length <= schema.maxByteLength)) {
233
+ yield { schema, path, value, message: `Expected Uint8Array to have a byte length less or equal to ${schema.maxByteLength}` };
234
+ }
235
+ if (schema.minByteLength && !(value.length >= schema.minByteLength)) {
236
+ yield { schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
237
+ }
238
+ }
239
+ function* Unknown(schema, path, value) { }
240
+ function* Void(schema, path, value) {
241
+ if (!(value === null)) {
242
+ return yield { schema, path, value, message: `Expected null` };
243
+ }
244
+ }
245
+ function* Visit(schema, path, value) {
246
+ if (schema.$id !== undefined) {
247
+ referenceMap.set(schema.$id, schema);
248
+ }
249
+ const anySchema = schema;
250
+ switch (anySchema[Types.Kind]) {
251
+ case 'Any':
252
+ return yield* Any(anySchema, path, value);
253
+ case 'Array':
254
+ return yield* Array(anySchema, path, value);
255
+ case 'Boolean':
256
+ return yield* Boolean(anySchema, path, value);
257
+ case 'Constructor':
258
+ return yield* Constructor(anySchema, path, value);
259
+ case 'Function':
260
+ return yield* Function(anySchema, path, value);
261
+ case 'Integer':
262
+ return yield* Integer(anySchema, path, value);
263
+ case 'Literal':
264
+ return yield* Literal(anySchema, path, value);
265
+ case 'Null':
266
+ return yield* Null(anySchema, path, value);
267
+ case 'Number':
268
+ return yield* Number(anySchema, path, value);
269
+ case 'Object':
270
+ return yield* Object(anySchema, path, value);
271
+ case 'Promise':
272
+ return yield* Promise(anySchema, path, value);
273
+ case 'Record':
274
+ return yield* Record(anySchema, path, value);
275
+ case 'Ref':
276
+ return yield* Ref(anySchema, path, value);
277
+ case 'Self':
278
+ return yield* Self(anySchema, path, value);
279
+ case 'String':
280
+ return yield* String(anySchema, path, value);
281
+ case 'Tuple':
282
+ return yield* Tuple(anySchema, path, value);
283
+ case 'Undefined':
284
+ return yield* Undefined(anySchema, path, value);
285
+ case 'Union':
286
+ return yield* Union(anySchema, path, value);
287
+ case 'Uint8Array':
288
+ return yield* Uint8Array(anySchema, path, value);
289
+ case 'Unknown':
290
+ return yield* Unknown(anySchema, path, value);
291
+ case 'Void':
292
+ return yield* Void(anySchema, path, value);
293
+ default:
294
+ throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
295
+ }
296
+ }
297
+ const referenceMap = new Map();
298
+ function SetAdditional(additional = []) {
299
+ referenceMap.clear();
300
+ for (const schema of additional) {
301
+ if (!schema.$id)
302
+ throw Error('TypeErrors: Referenced additional schemas must have an $id');
303
+ referenceMap.set(schema.$id, schema);
304
+ }
305
+ }
306
+ function* Errors(schema, additional, value) {
307
+ SetAdditional(additional);
308
+ yield* Visit(schema, '', value);
309
+ }
310
+ TypeErrors.Errors = Errors;
311
+ })(TypeErrors = exports.TypeErrors || (exports.TypeErrors = {}));
@@ -1 +1,2 @@
1
1
  export * from './compiler';
2
+ export * from './errors';
package/compiler/index.js CHANGED
@@ -38,3 +38,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
38
38
  };
39
39
  Object.defineProperty(exports, "__esModule", { value: true });
40
40
  __exportStar(require("./compiler"), exports);
41
+ __exportStar(require("./errors"), exports);
@@ -0,0 +1,48 @@
1
+ import * as Types from '../typebox';
2
+ /** Structural checks for TypeBox types */
3
+ export declare namespace TypeGuard {
4
+ /** Returns true if the given schema is TAny */
5
+ function TAny(schema: any): schema is Types.TAny;
6
+ /** Returns true if the given schema is TArray */
7
+ function TArray(schema: any): schema is Types.TArray;
8
+ /** Returns true if the given schema is TBoolean */
9
+ function TBoolean(schema: any): schema is Types.TBoolean;
10
+ /** Returns true if the given schema is TConstructor */
11
+ function TConstructor(schema: any): schema is Types.TConstructor;
12
+ /** Returns true if the given schema is TFunction */
13
+ function TFunction(schema: any): schema is Types.TFunction;
14
+ /** Returns true if the given schema is TInteger */
15
+ function TInteger(schema: any): schema is Types.TInteger;
16
+ /** Returns true if the given schema is TLiteral */
17
+ function TLiteral(schema: any): schema is Types.TLiteral;
18
+ /** Returns true if the given schema is TNull */
19
+ function TNull(schema: any): schema is Types.TNull;
20
+ /** Returns true if the given schema is TNumber */
21
+ function TNumber(schema: any): schema is Types.TNumber;
22
+ /** Returns true if the given schema is TObject */
23
+ function TObject(schema: any): schema is Types.TObject;
24
+ /** Returns true if the given schema is TPromise */
25
+ function TPromise(schema: any): schema is Types.TPromise;
26
+ /** Returns true if the given schema is TRecord */
27
+ function TRecord(schema: any): schema is Types.TRecord;
28
+ /** Returns true if the given schema is TSelf */
29
+ function TSelf(schema: any): schema is Types.TSelf;
30
+ /** Returns true if the given schema is TRef */
31
+ function TRef(schema: any): schema is Types.TSelf;
32
+ /** Returns true if the given schema is TString */
33
+ function TString(schema: any): schema is Types.TString;
34
+ /** Returns true if the given schema is TTuple */
35
+ function TTuple(schema: any): schema is Types.TTuple;
36
+ /** Returns true if the given schema is TUndefined */
37
+ function TUndefined(schema: any): schema is Types.TUndefined;
38
+ /** Returns true if the given schema is TUnion */
39
+ function TUnion(schema: any): schema is Types.TUnion;
40
+ /** Returns true if the given schema is TUint8Array */
41
+ function TUint8Array(schema: any): schema is Types.TUint8Array;
42
+ /** Returns true if the given schema is TUnknown */
43
+ function TUnknown(schema: any): schema is Types.TUnknown;
44
+ /** Returns true if the given schema is TVoid */
45
+ function TVoid(schema: any): schema is Types.TVoid;
46
+ /** Returns true if the given schema is TSchema */
47
+ function TSchema(schema: any): schema is Types.TSchema;
48
+ }
package/guard/guard.js ADDED
@@ -0,0 +1,222 @@
1
+ "use strict";
2
+ /*--------------------------------------------------------------------------
3
+
4
+ @sinclair/typebox/guard
5
+
6
+ The MIT License (MIT)
7
+
8
+ Copyright (c) 2022 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, dTribute, 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.TypeGuard = void 0;
31
+ const Types = require("../typebox");
32
+ /** Structural checks for TypeBox types */
33
+ var TypeGuard;
34
+ (function (TypeGuard) {
35
+ function IsObject(schema) {
36
+ return typeof schema === 'object' && schema !== null && !Array.isArray(schema);
37
+ }
38
+ function IsArray(schema) {
39
+ return typeof schema === 'object' && schema !== null && Array.isArray(schema);
40
+ }
41
+ /** Returns true if the given schema is TAny */
42
+ function TAny(schema) {
43
+ return IsObject(schema) && schema[Types.Kind] === 'Any';
44
+ }
45
+ TypeGuard.TAny = TAny;
46
+ /** Returns true if the given schema is TArray */
47
+ function TArray(schema) {
48
+ return IsObject(schema) && schema[Types.Kind] === 'Array' && schema.type === 'array' && TSchema(schema.items);
49
+ }
50
+ TypeGuard.TArray = TArray;
51
+ /** Returns true if the given schema is TBoolean */
52
+ function TBoolean(schema) {
53
+ return IsObject(schema) && schema[Types.Kind] === 'Boolean' && schema.type === 'boolean';
54
+ }
55
+ TypeGuard.TBoolean = TBoolean;
56
+ /** Returns true if the given schema is TConstructor */
57
+ function TConstructor(schema) {
58
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Constructor' && schema.type === 'constructor' && IsArray(schema.parameters) && TSchema(schema.returns))) {
59
+ return false;
60
+ }
61
+ for (const parameter of schema.parameters) {
62
+ if (!TSchema(parameter))
63
+ return false;
64
+ }
65
+ return true;
66
+ }
67
+ TypeGuard.TConstructor = TConstructor;
68
+ /** Returns true if the given schema is TFunction */
69
+ function TFunction(schema) {
70
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Function' && schema.type === 'function' && IsArray(schema.parameters) && TSchema(schema.returns))) {
71
+ return false;
72
+ }
73
+ for (const parameter of schema.parameters) {
74
+ if (!TSchema(parameter))
75
+ return false;
76
+ }
77
+ return true;
78
+ }
79
+ TypeGuard.TFunction = TFunction;
80
+ /** Returns true if the given schema is TInteger */
81
+ function TInteger(schema) {
82
+ return IsObject(schema) && schema[Types.Kind] === 'Integer' && schema.type === 'integer';
83
+ }
84
+ TypeGuard.TInteger = TInteger;
85
+ /** Returns true if the given schema is TLiteral */
86
+ function TLiteral(schema) {
87
+ return IsObject(schema) && schema[Types.Kind] === 'Literal' && (typeof schema.const === 'string' || typeof schema.const === 'number' || typeof schema.const === 'boolean');
88
+ }
89
+ TypeGuard.TLiteral = TLiteral;
90
+ /** Returns true if the given schema is TNull */
91
+ function TNull(schema) {
92
+ return IsObject(schema) && schema[Types.Kind] === 'Null' && schema.type === 'null';
93
+ }
94
+ TypeGuard.TNull = TNull;
95
+ /** Returns true if the given schema is TNumber */
96
+ function TNumber(schema) {
97
+ return IsObject(schema) && schema[Types.Kind] === 'Number' && schema.type === 'number';
98
+ }
99
+ TypeGuard.TNumber = TNumber;
100
+ /** Returns true if the given schema is TObject */
101
+ function TObject(schema) {
102
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Object' && schema.type === 'object' && IsObject(schema.properties))) {
103
+ return false;
104
+ }
105
+ for (const property of Object.values(schema.properties)) {
106
+ if (!TSchema(property))
107
+ return false;
108
+ }
109
+ return true;
110
+ }
111
+ TypeGuard.TObject = TObject;
112
+ /** Returns true if the given schema is TPromise */
113
+ function TPromise(schema) {
114
+ return IsObject(schema) && schema[Types.Kind] === 'Promise' && schema.type === 'promise' && TSchema(schema.item);
115
+ }
116
+ TypeGuard.TPromise = TPromise;
117
+ /** Returns true if the given schema is TRecord */
118
+ function TRecord(schema) {
119
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Record' && schema.type === 'object' && IsObject(schema.patternProperties))) {
120
+ return false;
121
+ }
122
+ const keys = Object.keys(schema.patternProperties);
123
+ if (keys.length !== 1) {
124
+ return false;
125
+ }
126
+ if (!TSchema(schema.patternProperties[keys[0]])) {
127
+ return false;
128
+ }
129
+ return true;
130
+ }
131
+ TypeGuard.TRecord = TRecord;
132
+ /** Returns true if the given schema is TSelf */
133
+ function TSelf(schema) {
134
+ return IsObject(schema) && schema[Types.Kind] === 'Self' && typeof schema.$ref === 'string';
135
+ }
136
+ TypeGuard.TSelf = TSelf;
137
+ /** Returns true if the given schema is TRef */
138
+ function TRef(schema) {
139
+ return IsObject(schema) && schema[Types.Kind] === 'Ref' && typeof schema.$ref === 'string';
140
+ }
141
+ TypeGuard.TRef = TRef;
142
+ /** Returns true if the given schema is TString */
143
+ function TString(schema) {
144
+ return IsObject(schema) && schema[Types.Kind] === 'String' && schema.type === 'string';
145
+ }
146
+ TypeGuard.TString = TString;
147
+ /** Returns true if the given schema is TTuple */
148
+ function TTuple(schema) {
149
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Tuple' && schema.type === 'array' && schema.additionalItems === false && typeof schema.minItems === 'number' && typeof schema.maxItems === 'number' && schema.minItems === schema.maxItems)) {
150
+ return false;
151
+ }
152
+ if (schema.items === undefined && schema.minItems === 0) {
153
+ return true;
154
+ }
155
+ if (!IsArray(schema.items)) {
156
+ return false;
157
+ }
158
+ for (const inner of schema.items) {
159
+ if (!TSchema(inner))
160
+ return false;
161
+ }
162
+ return true;
163
+ }
164
+ TypeGuard.TTuple = TTuple;
165
+ /** Returns true if the given schema is TUndefined */
166
+ function TUndefined(schema) {
167
+ return IsObject(schema) && schema[Types.Kind] === 'Undefined' && schema.type === 'object' && schema.specialized === 'Undefined';
168
+ }
169
+ TypeGuard.TUndefined = TUndefined;
170
+ /** Returns true if the given schema is TUnion */
171
+ function TUnion(schema) {
172
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Union' && IsArray(schema.anyOf))) {
173
+ return false;
174
+ }
175
+ for (const inner of schema.anyOf) {
176
+ if (!TSchema(inner))
177
+ return false;
178
+ }
179
+ return true;
180
+ }
181
+ TypeGuard.TUnion = TUnion;
182
+ /** Returns true if the given schema is TUint8Array */
183
+ function TUint8Array(schema) {
184
+ return IsObject(schema) && schema[Types.Kind] === 'Uint8Array' && schema.type === 'object' && schema.specialized === 'Uint8Array';
185
+ }
186
+ TypeGuard.TUint8Array = TUint8Array;
187
+ /** Returns true if the given schema is TUnknown */
188
+ function TUnknown(schema) {
189
+ return IsObject(schema) && schema[Types.Kind] === 'Unknown';
190
+ }
191
+ TypeGuard.TUnknown = TUnknown;
192
+ /** Returns true if the given schema is TVoid */
193
+ function TVoid(schema) {
194
+ return IsObject(schema) && schema[Types.Kind] === 'Void' && schema.type === 'null';
195
+ }
196
+ TypeGuard.TVoid = TVoid;
197
+ /** Returns true if the given schema is TSchema */
198
+ function TSchema(schema) {
199
+ return (TAny(schema) ||
200
+ TArray(schema) ||
201
+ TBoolean(schema) ||
202
+ TConstructor(schema) ||
203
+ TFunction(schema) ||
204
+ TInteger(schema) ||
205
+ TLiteral(schema) ||
206
+ TNull(schema) ||
207
+ TNumber(schema) ||
208
+ TObject(schema) ||
209
+ TPromise(schema) ||
210
+ TRecord(schema) ||
211
+ TSelf(schema) ||
212
+ TRef(schema) ||
213
+ TString(schema) ||
214
+ TTuple(schema) ||
215
+ TUndefined(schema) ||
216
+ TUnion(schema) ||
217
+ TUint8Array(schema) ||
218
+ TUnknown(schema) ||
219
+ TVoid(schema));
220
+ }
221
+ TypeGuard.TSchema = TSchema;
222
+ })(TypeGuard = exports.TypeGuard || (exports.TypeGuard = {}));
@@ -0,0 +1 @@
1
+ export * from './guard';
package/guard/index.js ADDED
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ /*--------------------------------------------------------------------------
3
+
4
+ @sinclair/typebox/guards
5
+
6
+ The MIT License (MIT)
7
+
8
+ Copyright (c) 2022 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
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
30
+ if (k2 === undefined) k2 = k;
31
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
32
+ }) : (function(o, m, k, k2) {
33
+ if (k2 === undefined) k2 = k;
34
+ o[k2] = m[k];
35
+ }));
36
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
37
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
38
+ };
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ __exportStar(require("./guard"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.24.3",
3
+ "version": "0.24.6",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "json-schema",
package/readme.md CHANGED
@@ -62,6 +62,7 @@ License MIT
62
62
  - [Generic Types](#Generic-Types)
63
63
  - [Unsafe Types](#Unsafe-Types)
64
64
  - [Values](#Values)
65
+ - [Guards](#Guards)
65
66
  - [Strict](#Strict)
66
67
  - [Validation](#Validation)
67
68
  - [Compiler](#Compiler)
@@ -435,7 +436,7 @@ In addition to JSON schema types, TypeBox provides several extended types that a
435
436
  │ │ │ │
436
437
  ├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
437
438
  │ const T = Type.Uint8Array() │ type T = Uint8Array │ const T = { │
438
- │ │ │ type: 'Uint8Array',
439
+ │ │ │ type: 'object',
439
440
  │ │ │ specialized: 'Uint8Array' │
440
441
  │ │ │ } │
441
442
  │ │ │ │
@@ -600,6 +601,7 @@ const T = StringEnum(['A', 'B', 'C']) // const T = {
600
601
 
601
602
  type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
602
603
  ```
604
+
603
605
  <a name="Values"></a>
604
606
 
605
607
  ### Values
@@ -623,6 +625,29 @@ const V = Value.Create(T) // const V = {
623
625
  // }
624
626
  ```
625
627
 
628
+ <a name="Guards"></a>
629
+
630
+ ### Guards
631
+
632
+ In some scenarios it may be helpful to test if an object is a valid TypeBox type. You can use the TypeGuard module to check an object conforms to a valid TypeBox schema representation. Consider the following.
633
+
634
+ ```typescript
635
+ import { TypeGuard } from '@sinclair/typebox/guard'
636
+ import { Type } from '@sinclair/typebox'
637
+
638
+ const T: any = Type.String() // T is any
639
+
640
+ const { type } = T // unsafe: type is any
641
+
642
+ if(TypeGuard.TString(T)) {
643
+
644
+ const { type } = T // safe: type is 'string'
645
+ }
646
+
647
+ ```
648
+
649
+
650
+
626
651
  <a name="Strict"></a>
627
652
 
628
653
  ### Strict
@@ -727,7 +752,7 @@ Please refer to the official AJV [documentation](https://ajv.js.org/guide/gettin
727
752
 
728
753
  ### Compiler
729
754
 
730
- TypeBox provides an optional type compiler that can be used as a runtime type checker in absense of a JSON Schema validator. Please note that this compiler is not fully JSON Schema compliant and only permits compilation of TypeBox types only (where the schema representation is known in advance). The `TypeCompiler` contains a `TypeCompiler.Compile(T)` method that returns a `TypeCheck<T>` object that can be used to test the validity of a value.
755
+ TypeBox includes a specialized type compiler that can be used as a runtime type checker in absense of a JSON Schema validator. This compiler is optimized for high throughput validation scenarios and generally performs better than AJV for most structural checks. Please note that this compiler is not fully JSON Schema compliant and is limited to TypeBox types only. The `TypeCompiler` contains a `Compile(T)` function that returns a `TypeCheck<T>` object that can be used to test the validity of a value as well as obtain errors.
731
756
 
732
757
  ```typescript
733
758
  import { TypeCompiler } from '@sinclair/typebox/compiler'
@@ -747,9 +772,40 @@ const OK = C.Check({
747
772
  z: 3
748
773
  }) // -> true
749
774
  ```
775
+ Errors can be obtained by calling the `Errors(...)` function. This function returns an iterator that may contain zero or more errors for the given value. For performance, you should only call `Errors(V)` if the `Check(V)` function returns false.
776
+ ```typescript
777
+ const C = TypeCompiler.Compile(Type.Object({
778
+ x: Type.Number(),
779
+ y: Type.Number(),
780
+ z: Type.Number()
781
+ }))
782
+
783
+ const V = { } // invalid
784
+
785
+ if(!C.Check(V)) {
786
+ for(const error of C.Errors(V)) {
787
+ console.log(error)
788
+ }
789
+ }
790
+ ```
791
+ To inspect the generated validation code created by the compiler. You can call the `Code()` function on the `TypeCheck<T>` object.
792
+
793
+ ```typescript
794
+ const C = TypeCompiler.Compile(Type.String())
795
+
796
+ console.log(C.Code())
797
+ //
798
+ // outputs:
799
+ //
800
+ // return function check(value) {
801
+ // return (
802
+ // (typeof value === 'string')
803
+ // )
804
+ // }
805
+ ```
750
806
 
751
807
  <a name="Contribute"></a>
752
808
 
753
809
  ### Contribute
754
810
 
755
- TypeBox is open to community contribution, however please ensure you submit an open issue before submitting your pull request. The TypeBox project does preference open community discussion prior to accepting new features.
811
+ TypeBox is open to community contribution, however please ensure you submit an open issue before submitting your pull request. The TypeBox project does preference open community discussion prior to accepting new features.
package/typebox.js CHANGED
@@ -107,7 +107,6 @@ class TypeBuilder {
107
107
  const properties = {};
108
108
  for (const object of objects) {
109
109
  for (const [key, schema] of Object.entries(object.properties)) {
110
- delete schema[exports.Modifier];
111
110
  properties[key] = properties[key] === undefined ? schema : { [exports.Kind]: 'Union', anyOf: [properties[key], { ...schema }] };
112
111
  }
113
112
  }