@sinclair/typebox 0.24.5 → 0.24.8

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,4 +1,4 @@
1
- import { TypeError } from './errors';
1
+ import { ValueError } from '../value/errors';
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> {
@@ -7,14 +7,14 @@ export declare class TypeCheck<T extends Types.TSchema> {
7
7
  private readonly checkFunc;
8
8
  private readonly code;
9
9
  constructor(schema: T, additional: Types.TSchema[], checkFunc: CheckFunction, code: string);
10
- /** Returns the generated validation code used to validate this type */
10
+ /** Returns the generated validation code used to validate this type. */
11
11
  Code(): string;
12
- /** Returns an iterator for each type error found in this value */
13
- Errors(value: unknown): Generator<TypeError>;
12
+ /** Returns an iterator for each error in this value. */
13
+ Errors(value: unknown): IterableIterator<ValueError>;
14
14
  /** Returns true if the value matches the given type. */
15
15
  Check(value: unknown): value is Types.Static<T>;
16
16
  }
17
17
  export declare namespace TypeCompiler {
18
18
  /** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */
19
- function Compile<T extends Types.TSchema>(schema: T, additional?: Types.TSchema[]): TypeCheck<T>;
19
+ function Compile<T extends Types.TSchema>(schema: T, references?: Types.TSchema[]): TypeCheck<T>;
20
20
  }
@@ -28,7 +28,7 @@ THE SOFTWARE.
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.TypeCompiler = exports.TypeCheck = void 0;
31
- const errors_1 = require("./errors");
31
+ const errors_1 = require("../value/errors");
32
32
  const Types = require("../typebox");
33
33
  // -------------------------------------------------------------------
34
34
  // TypeCheck
@@ -44,13 +44,13 @@ class TypeCheck {
44
44
  this.checkFunc = checkFunc;
45
45
  this.code = code;
46
46
  }
47
- /** Returns the generated validation code used to validate this type */
47
+ /** Returns the generated validation code used to validate this type. */
48
48
  Code() {
49
49
  return this.code;
50
50
  }
51
- /** Returns an iterator for each type error found in this value */
51
+ /** Returns an iterator for each error in this value. */
52
52
  Errors(value) {
53
- return errors_1.TypeErrors.Errors(this.schema, this.additional, value);
53
+ return errors_1.ValueErrors.Errors(this.schema, this.additional, value);
54
54
  }
55
55
  /** Returns true if the value matches the given type. */
56
56
  Check(value) {
@@ -66,65 +66,65 @@ var TypeCompiler;
66
66
  // -------------------------------------------------------------------
67
67
  // Schemas
68
68
  // -------------------------------------------------------------------
69
- function* Any(schema, path) {
69
+ function* Any(schema, value) {
70
70
  yield '(true)';
71
71
  }
72
- function* Array(schema, path) {
72
+ function* Array(schema, value) {
73
73
  const expr = [...Visit(schema.items, `value`)].map((condition) => condition).join(' && ');
74
- yield `(Array.isArray(${path}) && ${path}.every(value => ${expr}))`;
74
+ yield `(Array.isArray(${value}) && ${value}.every(value => ${expr}))`;
75
75
  }
76
- function* Boolean(schema, path) {
77
- yield `(typeof ${path} === 'boolean')`;
76
+ function* Boolean(schema, value) {
77
+ yield `(typeof ${value} === 'boolean')`;
78
78
  }
79
- function* Constructor(schema, path) {
80
- yield* Visit(schema.yields, path);
79
+ function* Constructor(schema, value) {
80
+ yield* Visit(schema.returns, value);
81
81
  }
82
- function* Function(schema, path) {
83
- yield `(typeof ${path} === 'function')`;
82
+ function* Function(schema, value) {
83
+ yield `(typeof ${value} === 'function')`;
84
84
  }
85
- function* Integer(schema, path) {
86
- yield `(typeof ${path} === 'number' && Number.isInteger(${path}))`;
85
+ function* Integer(schema, value) {
86
+ yield `(typeof ${value} === 'number' && Number.isInteger(${value}))`;
87
87
  if (schema.multipleOf)
88
- yield `(${path} % ${schema.multipleOf} === 0)`;
88
+ yield `(${value} % ${schema.multipleOf} === 0)`;
89
89
  if (schema.exclusiveMinimum)
90
- yield `(${path} > ${schema.exclusiveMinimum})`;
90
+ yield `(${value} > ${schema.exclusiveMinimum})`;
91
91
  if (schema.exclusiveMaximum)
92
- yield `(${path} < ${schema.exclusiveMaximum})`;
92
+ yield `(${value} < ${schema.exclusiveMaximum})`;
93
93
  if (schema.minimum)
94
- yield `(${path} >= ${schema.minimum})`;
94
+ yield `(${value} >= ${schema.minimum})`;
95
95
  if (schema.maximum)
96
- yield `(${path} <= ${schema.maximum})`;
96
+ yield `(${value} <= ${schema.maximum})`;
97
97
  }
98
- function* Literal(schema, path) {
98
+ function* Literal(schema, value) {
99
99
  if (typeof schema.const === 'string') {
100
- yield `(${path} === '${schema.const}')`;
100
+ yield `(${value} === '${schema.const}')`;
101
101
  }
102
102
  else {
103
- yield `(${path} === ${schema.const})`;
103
+ yield `(${value} === ${schema.const})`;
104
104
  }
105
105
  }
106
- function* Null(schema, path) {
107
- yield `(${path} === null)`;
106
+ function* Null(schema, value) {
107
+ yield `(${value} === null)`;
108
108
  }
109
- function* Number(schema, path) {
110
- yield `(typeof ${path} === 'number')`;
109
+ function* Number(schema, value) {
110
+ yield `(typeof ${value} === 'number')`;
111
111
  if (schema.multipleOf)
112
- yield `(${path} % ${schema.multipleOf} === 0)`;
112
+ yield `(${value} % ${schema.multipleOf} === 0)`;
113
113
  if (schema.exclusiveMinimum)
114
- yield `(${path} > ${schema.exclusiveMinimum})`;
114
+ yield `(${value} > ${schema.exclusiveMinimum})`;
115
115
  if (schema.exclusiveMaximum)
116
- yield `(${path} < ${schema.exclusiveMaximum})`;
116
+ yield `(${value} < ${schema.exclusiveMaximum})`;
117
117
  if (schema.minimum)
118
- yield `(${path} >= ${schema.minimum})`;
118
+ yield `(${value} >= ${schema.minimum})`;
119
119
  if (schema.maximum)
120
- yield `(${path} <= ${schema.maximum})`;
120
+ yield `(${value} <= ${schema.maximum})`;
121
121
  }
122
- function* Object(schema, path) {
123
- yield `(typeof ${path} === 'object' && ${path} !== null && !Array.isArray(${path}))`;
122
+ function* Object(schema, value) {
123
+ yield `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))`;
124
124
  if (schema.minProperties !== undefined)
125
- yield `(Object.keys(${path}).length >= ${schema.minProperties})`;
125
+ yield `(Object.keys(${value}).length >= ${schema.minProperties})`;
126
126
  if (schema.maxProperties !== undefined)
127
- yield `(Object.keys(${path}).length <= ${schema.maxProperties})`;
127
+ yield `(Object.keys(${value}).length <= ${schema.maxProperties})`;
128
128
  const propertyKeys = globalThis.Object.keys(schema.properties);
129
129
  if (schema.additionalProperties === false) {
130
130
  // optimization: If the property key length matches the required keys length
@@ -132,36 +132,36 @@ var TypeCompiler;
132
132
  // of the property key length. This is because exhaustive testing for values
133
133
  // will occur in subsequent property tests.
134
134
  if (schema.required && schema.required.length === propertyKeys.length) {
135
- yield `(Object.keys(${path}).length === ${propertyKeys.length})`;
135
+ yield `(Object.keys(${value}).length === ${propertyKeys.length})`;
136
136
  }
137
137
  else {
138
138
  const keys = `[${propertyKeys.map((key) => `'${key}'`).join(', ')}]`;
139
- yield `(Object.keys(${path}).every(key => ${keys}.includes(key)))`;
139
+ yield `(Object.keys(${value}).every(key => ${keys}.includes(key)))`;
140
140
  }
141
141
  }
142
142
  for (const propertyKey of propertyKeys) {
143
143
  const propertySchema = schema.properties[propertyKey];
144
144
  if (schema.required && schema.required.includes(propertyKey)) {
145
- yield* Visit(propertySchema, `${path}.${propertyKey}`);
145
+ yield* Visit(propertySchema, `${value}.${propertyKey}`);
146
146
  }
147
147
  else {
148
- const expr = [...Visit(propertySchema, `${path}.${propertyKey}`)].map((condition) => condition).join(' && ');
149
- yield `(${path}.${propertyKey} === undefined ? true : (${expr}))`;
148
+ const expr = [...Visit(propertySchema, `${value}.${propertyKey}`)].map((condition) => condition).join(' && ');
149
+ yield `(${value}.${propertyKey} === undefined ? true : (${expr}))`;
150
150
  }
151
151
  }
152
152
  }
153
- function* Promise(schema, path) {
154
- yield `(typeof value === 'object' && typeof ${path}.then === 'function')`;
153
+ function* Promise(schema, value) {
154
+ yield `(typeof value === 'object' && typeof ${value}.then === 'function')`;
155
155
  }
156
- function* Record(schema, path) {
157
- yield `(typeof ${path} === 'object' && ${path} !== null && !Array.isArray(${path}))`;
156
+ function* Record(schema, value) {
157
+ yield `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))`;
158
158
  const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
159
159
  const local = PushLocal(`const local = new RegExp(/${keyPattern}/)`);
160
- yield `(Object.keys(${path}).every(key => ${local}.test(key)))`;
160
+ yield `(Object.keys(${value}).every(key => ${local}.test(key)))`;
161
161
  const expr = [...Visit(valueSchema, 'value')].map((condition) => condition).join(' && ');
162
- yield `(Object.values(${path}).every(value => ${expr}))`;
162
+ yield `(Object.values(${value}).every(value => ${expr}))`;
163
163
  }
164
- function* Ref(schema, path) {
164
+ function* Ref(schema, value) {
165
165
  // reference: referenced schemas can originate from either additional
166
166
  // schemas or inline in the schema itself. Ideally the recursive
167
167
  // path should align to reference path. Consider for review.
@@ -174,50 +174,50 @@ var TypeCompiler;
174
174
  PushLocal(body);
175
175
  }
176
176
  const func = CreateFunctionName(schema.$ref);
177
- yield `(${func}(${path}))`;
177
+ yield `(${func}(${value}))`;
178
178
  }
179
- function* Self(schema, path) {
179
+ function* Self(schema, value) {
180
180
  const func = CreateFunctionName(schema.$ref);
181
- yield `(${func}(${path}))`;
181
+ yield `(${func}(${value}))`;
182
182
  }
183
- function* String(schema, path) {
184
- yield `(typeof ${path} === 'string')`;
183
+ function* String(schema, value) {
184
+ yield `(typeof ${value} === 'string')`;
185
185
  if (schema.pattern !== undefined) {
186
186
  const local = PushLocal(`const local = new RegExp('${schema.pattern}');`);
187
- yield `(${local}.test(${path}))`;
187
+ yield `(${local}.test(${value}))`;
188
188
  }
189
189
  }
190
- function* Tuple(schema, path) {
191
- yield `(Array.isArray(${path}))`;
190
+ function* Tuple(schema, value) {
191
+ yield `(Array.isArray(${value}))`;
192
192
  if (schema.items === undefined)
193
- return yield `(${path}.length === 0)`;
194
- yield `(${path}.length === ${schema.maxItems})`;
193
+ return yield `(${value}.length === 0)`;
194
+ yield `(${value}.length === ${schema.maxItems})`;
195
195
  for (let i = 0; i < schema.items.length; i++) {
196
- const expr = [...Visit(schema.items[i], `${path}[${i}]`)].map((condition) => condition).join(' && ');
196
+ const expr = [...Visit(schema.items[i], `${value}[${i}]`)].map((condition) => condition).join(' && ');
197
197
  yield `(${expr})`;
198
198
  }
199
199
  }
200
- function* Undefined(schema, path) {
201
- yield `${path} === undefined`;
200
+ function* Undefined(schema, value) {
201
+ yield `(${value} === undefined)`;
202
202
  }
203
- function* Union(schema, path) {
204
- const exprs = schema.anyOf.map((schema) => [...Visit(schema, path)].map((condition) => condition).join(' && '));
203
+ function* Union(schema, value) {
204
+ const exprs = schema.anyOf.map((schema) => [...Visit(schema, value)].map((condition) => condition).join(' && '));
205
205
  yield `(${exprs.join(' || ')})`;
206
206
  }
207
- function* Uint8Array(schema, path) {
208
- yield `(${path} instanceof Uint8Array)`;
207
+ function* Uint8Array(schema, value) {
208
+ yield `(${value} instanceof Uint8Array)`;
209
209
  if (schema.maxByteLength)
210
- yield `(${path}.length <= ${schema.maxByteLength})`;
210
+ yield `(${value}.length <= ${schema.maxByteLength})`;
211
211
  if (schema.minByteLength)
212
- yield `(${path}.length >= ${schema.minByteLength})`;
212
+ yield `(${value}.length >= ${schema.minByteLength})`;
213
213
  }
214
- function* Unknown(schema, path) {
214
+ function* Unknown(schema, value) {
215
215
  yield '(true)';
216
216
  }
217
- function* Void(schema, path) {
218
- yield `(${path} === null)`;
217
+ function* Void(schema, value) {
218
+ yield `(${value} === null)`;
219
219
  }
220
- function* Visit(schema, path) {
220
+ function* Visit(schema, value) {
221
221
  // reference: referenced schemas can originate from either additional
222
222
  // schemas or inline in the schema itself. Ideally the recursive
223
223
  // path should align to reference path. Consider for review.
@@ -227,53 +227,53 @@ var TypeCompiler;
227
227
  const name = CreateFunctionName(schema.$id);
228
228
  const body = CreateFunction(name, conditions);
229
229
  PushLocal(body);
230
- yield `(${name}(${path}))`;
230
+ yield `(${name}(${value}))`;
231
231
  return;
232
232
  }
233
233
  const anySchema = schema;
234
234
  switch (anySchema[Types.Kind]) {
235
235
  case 'Any':
236
- return yield* Any(anySchema, path);
236
+ return yield* Any(anySchema, value);
237
237
  case 'Array':
238
- return yield* Array(anySchema, path);
238
+ return yield* Array(anySchema, value);
239
239
  case 'Boolean':
240
- return yield* Boolean(anySchema, path);
240
+ return yield* Boolean(anySchema, value);
241
241
  case 'Constructor':
242
- return yield* Constructor(anySchema, path);
242
+ return yield* Constructor(anySchema, value);
243
243
  case 'Function':
244
- return yield* Function(anySchema, path);
244
+ return yield* Function(anySchema, value);
245
245
  case 'Integer':
246
- return yield* Integer(anySchema, path);
246
+ return yield* Integer(anySchema, value);
247
247
  case 'Literal':
248
- return yield* Literal(anySchema, path);
248
+ return yield* Literal(anySchema, value);
249
249
  case 'Null':
250
- return yield* Null(anySchema, path);
250
+ return yield* Null(anySchema, value);
251
251
  case 'Number':
252
- return yield* Number(anySchema, path);
252
+ return yield* Number(anySchema, value);
253
253
  case 'Object':
254
- return yield* Object(anySchema, path);
254
+ return yield* Object(anySchema, value);
255
255
  case 'Promise':
256
- return yield* Promise(anySchema, path);
256
+ return yield* Promise(anySchema, value);
257
257
  case 'Record':
258
- return yield* Record(anySchema, path);
258
+ return yield* Record(anySchema, value);
259
259
  case 'Ref':
260
- return yield* Ref(anySchema, path);
260
+ return yield* Ref(anySchema, value);
261
261
  case 'Self':
262
- return yield* Self(anySchema, path);
262
+ return yield* Self(anySchema, value);
263
263
  case 'String':
264
- return yield* String(anySchema, path);
264
+ return yield* String(anySchema, value);
265
265
  case 'Tuple':
266
- return yield* Tuple(anySchema, path);
266
+ return yield* Tuple(anySchema, value);
267
267
  case 'Undefined':
268
- return yield* Undefined(anySchema, path);
268
+ return yield* Undefined(anySchema, value);
269
269
  case 'Union':
270
- return yield* Union(anySchema, path);
270
+ return yield* Union(anySchema, value);
271
271
  case 'Uint8Array':
272
- return yield* Uint8Array(anySchema, path);
272
+ return yield* Uint8Array(anySchema, value);
273
273
  case 'Unknown':
274
- return yield* Unknown(anySchema, path);
274
+ return yield* Unknown(anySchema, value);
275
275
  case 'Void':
276
- return yield* Void(anySchema, path);
276
+ return yield* Void(anySchema, value);
277
277
  default:
278
278
  throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
279
279
  }
@@ -319,18 +319,18 @@ var TypeCompiler;
319
319
  // -------------------------------------------------------------------
320
320
  // Compile
321
321
  // -------------------------------------------------------------------
322
- function Build(schema, additional = []) {
322
+ function Build(schema, references = []) {
323
323
  ClearLocals();
324
- PushReferences(additional);
324
+ PushReferences(references);
325
325
  const conditions = [...Visit(schema, 'value')]; // locals populated during yield
326
326
  const locals = GetLocals();
327
327
  return `${locals.join('\n')}\nreturn ${CreateFunction('check', conditions)}`;
328
328
  }
329
329
  /** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */
330
- function Compile(schema, additional = []) {
331
- const code = Build(schema, additional);
330
+ function Compile(schema, references = []) {
331
+ const code = Build(schema, references);
332
332
  const func = globalThis.Function(code);
333
- return new TypeCheck(schema, additional, func(), code);
333
+ return new TypeCheck(schema, references, func(), code);
334
334
  }
335
335
  TypeCompiler.Compile = Compile;
336
336
  })(TypeCompiler = exports.TypeCompiler || (exports.TypeCompiler = {}));
@@ -1,2 +1,2 @@
1
1
  export * from './compiler';
2
- export * from './errors';
2
+ export * from '../value/errors';
package/compiler/index.js CHANGED
@@ -38,4 +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);
41
+ __exportStar(require("../value/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.TRef;
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';