@sinclair/typebox 0.30.3 → 0.31.0-dev-1

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/value/check.js CHANGED
@@ -27,28 +27,22 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.Check = exports.ValueCheckDereferenceError = exports.ValueCheckUnknownTypeError = void 0;
30
+ exports.Check = exports.ValueCheckUnknownTypeError = void 0;
31
+ const guard_1 = require("./guard");
31
32
  const index_1 = require("../system/index");
33
+ const deref_1 = require("./deref");
34
+ const hash_1 = require("./hash");
32
35
  const Types = require("../typebox");
33
- const ValueGuard = require("./guard");
34
- const ValueHash = require("./hash");
35
36
  // --------------------------------------------------------------------------
36
37
  // Errors
37
38
  // --------------------------------------------------------------------------
38
- class ValueCheckUnknownTypeError extends Error {
39
+ class ValueCheckUnknownTypeError extends Types.TypeBoxError {
39
40
  constructor(schema) {
40
- super(`ValueCheck: ${schema[Types.Kind] ? `Unknown type '${schema[Types.Kind]}'` : 'Unknown type'}`);
41
+ super(`Unknown type`);
41
42
  this.schema = schema;
42
43
  }
43
44
  }
44
45
  exports.ValueCheckUnknownTypeError = ValueCheckUnknownTypeError;
45
- class ValueCheckDereferenceError extends Error {
46
- constructor(schema) {
47
- super(`ValueCheck: Unable to dereference type with $id '${schema.$ref}'`);
48
- this.schema = schema;
49
- }
50
- }
51
- exports.ValueCheckDereferenceError = ValueCheckDereferenceError;
52
46
  // --------------------------------------------------------------------------
53
47
  // TypeGuards
54
48
  // --------------------------------------------------------------------------
@@ -62,36 +56,14 @@ function IsDefined(value) {
62
56
  return value !== undefined;
63
57
  }
64
58
  // --------------------------------------------------------------------------
65
- // Policies
66
- // --------------------------------------------------------------------------
67
- function IsExactOptionalProperty(value, key) {
68
- return index_1.TypeSystem.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined;
69
- }
70
- function IsObject(value) {
71
- const isObject = ValueGuard.IsObject(value);
72
- return index_1.TypeSystem.AllowArrayObjects ? isObject : isObject && !ValueGuard.IsArray(value);
73
- }
74
- function IsRecordObject(value) {
75
- return IsObject(value) && !(value instanceof Date) && !(value instanceof Uint8Array);
76
- }
77
- function IsNumber(value) {
78
- const isNumber = ValueGuard.IsNumber(value);
79
- return index_1.TypeSystem.AllowNaN ? isNumber : isNumber && Number.isFinite(value);
80
- }
81
- function IsVoid(value) {
82
- const isUndefined = ValueGuard.IsUndefined(value);
83
- return index_1.TypeSystem.AllowVoidNull ? isUndefined || value === null : isUndefined;
84
- }
85
- // --------------------------------------------------------------------------
86
59
  // Types
87
60
  // --------------------------------------------------------------------------
88
61
  function TAny(schema, references, value) {
89
62
  return true;
90
63
  }
91
64
  function TArray(schema, references, value) {
92
- if (!Array.isArray(value)) {
65
+ if (!(0, guard_1.IsArray)(value))
93
66
  return false;
94
- }
95
67
  if (IsDefined(schema.minItems) && !(value.length >= schema.minItems)) {
96
68
  return false;
97
69
  }
@@ -103,7 +75,7 @@ function TArray(schema, references, value) {
103
75
  }
104
76
  // prettier-ignore
105
77
  if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
106
- const hashed = ValueHash.Hash(element);
78
+ const hashed = (0, hash_1.Hash)(element);
107
79
  if (set.has(hashed)) {
108
80
  return false;
109
81
  }
@@ -114,7 +86,7 @@ function TArray(schema, references, value) {
114
86
  return false;
115
87
  }
116
88
  // contains
117
- if (!(IsDefined(schema.contains) || IsNumber(schema.minContains) || IsNumber(schema.maxContains))) {
89
+ if (!(IsDefined(schema.contains) || (0, guard_1.IsNumber)(schema.minContains) || (0, guard_1.IsNumber)(schema.maxContains))) {
118
90
  return true; // exit
119
91
  }
120
92
  const containsSchema = IsDefined(schema.contains) ? schema.contains : Types.Type.Never();
@@ -122,85 +94,83 @@ function TArray(schema, references, value) {
122
94
  if (containsCount === 0) {
123
95
  return false;
124
96
  }
125
- if (IsNumber(schema.minContains) && containsCount < schema.minContains) {
97
+ if ((0, guard_1.IsNumber)(schema.minContains) && containsCount < schema.minContains) {
126
98
  return false;
127
99
  }
128
- if (IsNumber(schema.maxContains) && containsCount > schema.maxContains) {
100
+ if ((0, guard_1.IsNumber)(schema.maxContains) && containsCount > schema.maxContains) {
129
101
  return false;
130
102
  }
131
103
  return true;
132
104
  }
133
105
  function TAsyncIterator(schema, references, value) {
134
- return IsObject(value) && Symbol.asyncIterator in value;
106
+ return (0, guard_1.IsAsyncIterator)(value);
135
107
  }
136
108
  function TBigInt(schema, references, value) {
137
- if (!ValueGuard.IsBigInt(value)) {
109
+ if (!(0, guard_1.IsBigInt)(value))
138
110
  return false;
139
- }
140
- if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === BigInt(0))) {
111
+ if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
141
112
  return false;
142
113
  }
143
114
  if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
144
115
  return false;
145
116
  }
146
- if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
117
+ if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
147
118
  return false;
148
119
  }
149
120
  if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
150
121
  return false;
151
122
  }
152
- if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
123
+ if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === BigInt(0))) {
153
124
  return false;
154
125
  }
155
126
  return true;
156
127
  }
157
128
  function TBoolean(schema, references, value) {
158
- return typeof value === 'boolean';
129
+ return (0, guard_1.IsBoolean)(value);
159
130
  }
160
131
  function TConstructor(schema, references, value) {
161
132
  return Visit(schema.returns, references, value.prototype);
162
133
  }
163
134
  function TDate(schema, references, value) {
164
- if (!(value instanceof Date)) {
135
+ if (!(0, guard_1.IsDate)(value))
165
136
  return false;
166
- }
167
- if (!IsNumber(value.getTime())) {
137
+ if (IsDefined(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
168
138
  return false;
169
139
  }
170
140
  if (IsDefined(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
171
141
  return false;
172
142
  }
173
- if (IsDefined(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
143
+ if (IsDefined(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) {
174
144
  return false;
175
145
  }
176
146
  if (IsDefined(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) {
177
147
  return false;
178
148
  }
179
- if (IsDefined(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) {
149
+ if (IsDefined(schema.multipleOfTimestamp) && !(value.getTime() % schema.multipleOfTimestamp === 0)) {
180
150
  return false;
181
151
  }
182
152
  return true;
183
153
  }
184
154
  function TFunction(schema, references, value) {
185
- return typeof value === 'function';
155
+ return (0, guard_1.IsFunction)(value);
186
156
  }
187
157
  function TInteger(schema, references, value) {
188
- if (!ValueGuard.IsInteger(value)) {
158
+ if (!(0, guard_1.IsInteger)(value)) {
189
159
  return false;
190
160
  }
191
- if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
161
+ if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
192
162
  return false;
193
163
  }
194
164
  if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
195
165
  return false;
196
166
  }
197
- if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
167
+ if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
198
168
  return false;
199
169
  }
200
170
  if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
201
171
  return false;
202
172
  }
203
- if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
173
+ if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
204
174
  return false;
205
175
  }
206
176
  return true;
@@ -222,7 +192,7 @@ function TIntersect(schema, references, value) {
222
192
  }
223
193
  }
224
194
  function TIterator(schema, references, value) {
225
- return IsObject(value) && Symbol.iterator in value;
195
+ return (0, guard_1.IsIterator)(value);
226
196
  }
227
197
  function TLiteral(schema, references, value) {
228
198
  return value === schema.const;
@@ -234,33 +204,31 @@ function TNot(schema, references, value) {
234
204
  return !Visit(schema.not, references, value);
235
205
  }
236
206
  function TNull(schema, references, value) {
237
- return value === null;
207
+ return (0, guard_1.IsNull)(value);
238
208
  }
239
209
  function TNumber(schema, references, value) {
240
- if (!IsNumber(value)) {
210
+ if (!index_1.TypeSystemPolicy.IsNumberLike(value))
241
211
  return false;
242
- }
243
- if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
212
+ if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
244
213
  return false;
245
214
  }
246
215
  if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
247
216
  return false;
248
217
  }
249
- if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
250
- return false;
251
- }
252
218
  if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
253
219
  return false;
254
220
  }
255
221
  if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
256
222
  return false;
257
223
  }
224
+ if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
225
+ return false;
226
+ }
258
227
  return true;
259
228
  }
260
229
  function TObject(schema, references, value) {
261
- if (!IsObject(value)) {
230
+ if (!index_1.TypeSystemPolicy.IsObjectLike(value))
262
231
  return false;
263
- }
264
232
  if (IsDefined(schema.minProperties) && !(Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
265
233
  return false;
266
234
  }
@@ -279,7 +247,7 @@ function TObject(schema, references, value) {
279
247
  }
280
248
  }
281
249
  else {
282
- if (IsExactOptionalProperty(value, knownKey) && !Visit(property, references, value[knownKey])) {
250
+ if (index_1.TypeSystemPolicy.IsExactOptionalProperty(value, knownKey) && !Visit(property, references, value[knownKey])) {
283
251
  return false;
284
252
  }
285
253
  }
@@ -303,10 +271,10 @@ function TObject(schema, references, value) {
303
271
  }
304
272
  }
305
273
  function TPromise(schema, references, value) {
306
- return typeof value === 'object' && typeof value.then === 'function';
274
+ return (0, guard_1.IsPromise)(value);
307
275
  }
308
276
  function TRecord(schema, references, value) {
309
- if (!IsRecordObject(value)) {
277
+ if (!index_1.TypeSystemPolicy.IsRecordLike(value)) {
310
278
  return false;
311
279
  }
312
280
  if (IsDefined(schema.minProperties) && !(Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
@@ -317,28 +285,26 @@ function TRecord(schema, references, value) {
317
285
  }
318
286
  const [patternKey, patternSchema] = Object.entries(schema.patternProperties)[0];
319
287
  const regex = new RegExp(patternKey);
320
- return Object.entries(value).every(([key, value]) => {
321
- if (regex.test(key)) {
322
- return Visit(patternSchema, references, value);
323
- }
324
- if (typeof schema.additionalProperties === 'object') {
325
- return Visit(schema.additionalProperties, references, value);
326
- }
327
- if (schema.additionalProperties === false) {
328
- return false;
329
- }
330
- return true;
288
+ // prettier-ignore
289
+ const check1 = Object.entries(value).every(([key, value]) => {
290
+ return (regex.test(key)) ? Visit(patternSchema, references, value) : true;
331
291
  });
292
+ // prettier-ignore
293
+ const check2 = typeof schema.additionalProperties === 'object' ? Object.entries(value).every(([key, value]) => {
294
+ return (!regex.test(key)) ? Visit(schema.additionalProperties, references, value) : true;
295
+ }) : true;
296
+ const check3 = schema.additionalProperties === false
297
+ ? Object.getOwnPropertyNames(value).every((key) => {
298
+ return regex.test(key);
299
+ })
300
+ : true;
301
+ return check1 && check2 && check3;
332
302
  }
333
303
  function TRef(schema, references, value) {
334
- const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
335
- if (index === -1)
336
- throw new ValueCheckDereferenceError(schema);
337
- const target = references[index];
338
- return Visit(target, references, value);
304
+ return Visit((0, deref_1.Deref)(schema, references), references, value);
339
305
  }
340
306
  function TString(schema, references, value) {
341
- if (!ValueGuard.IsString(value)) {
307
+ if (!(0, guard_1.IsString)(value)) {
342
308
  return false;
343
309
  }
344
310
  if (IsDefined(schema.minLength)) {
@@ -363,26 +329,16 @@ function TString(schema, references, value) {
363
329
  return true;
364
330
  }
365
331
  function TSymbol(schema, references, value) {
366
- if (!(typeof value === 'symbol')) {
367
- return false;
368
- }
369
- return true;
332
+ return (0, guard_1.IsSymbol)(value);
370
333
  }
371
334
  function TTemplateLiteral(schema, references, value) {
372
- if (!ValueGuard.IsString(value)) {
373
- return false;
374
- }
375
- return new RegExp(schema.pattern).test(value);
335
+ return (0, guard_1.IsString)(value) && new RegExp(schema.pattern).test(value);
376
336
  }
377
337
  function TThis(schema, references, value) {
378
- const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
379
- if (index === -1)
380
- throw new ValueCheckDereferenceError(schema);
381
- const target = references[index];
382
- return Visit(target, references, value);
338
+ return Visit((0, deref_1.Deref)(schema, references), references, value);
383
339
  }
384
340
  function TTuple(schema, references, value) {
385
- if (!ValueGuard.IsArray(value)) {
341
+ if (!(0, guard_1.IsArray)(value)) {
386
342
  return false;
387
343
  }
388
344
  if (schema.items === undefined && !(value.length === 0)) {
@@ -401,13 +357,13 @@ function TTuple(schema, references, value) {
401
357
  return true;
402
358
  }
403
359
  function TUndefined(schema, references, value) {
404
- return value === undefined;
360
+ return (0, guard_1.IsUndefined)(value);
405
361
  }
406
362
  function TUnion(schema, references, value) {
407
363
  return schema.anyOf.some((inner) => Visit(inner, references, value));
408
364
  }
409
365
  function TUint8Array(schema, references, value) {
410
- if (!(value instanceof Uint8Array)) {
366
+ if (!(0, guard_1.IsUint8Array)(value)) {
411
367
  return false;
412
368
  }
413
369
  if (IsDefined(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
@@ -422,7 +378,7 @@ function TUnknown(schema, references, value) {
422
378
  return true;
423
379
  }
424
380
  function TVoid(schema, references, value) {
425
- return IsVoid(value);
381
+ return index_1.TypeSystemPolicy.IsVoidLike(value);
426
382
  }
427
383
  function TKind(schema, references, value) {
428
384
  if (!Types.TypeRegistry.Has(schema[Types.Kind]))
package/value/clone.js CHANGED
@@ -28,7 +28,7 @@ THE SOFTWARE.
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.Clone = void 0;
31
- const ValueGuard = require("./guard");
31
+ const guard_1 = require("./guard");
32
32
  // --------------------------------------------------------------------------
33
33
  // Clonable
34
34
  // --------------------------------------------------------------------------
@@ -49,42 +49,19 @@ function ValueType(value) {
49
49
  return value;
50
50
  }
51
51
  // --------------------------------------------------------------------------
52
- // Non-Clonable
53
- // --------------------------------------------------------------------------
54
- function AsyncIteratorType(value) {
55
- return value;
56
- }
57
- function IteratorType(value) {
58
- return value;
59
- }
60
- function FunctionType(value) {
61
- return value;
62
- }
63
- function PromiseType(value) {
64
- return value;
65
- }
66
- // --------------------------------------------------------------------------
67
52
  // Clone
68
53
  // --------------------------------------------------------------------------
69
54
  /** Returns a clone of the given value */
70
55
  function Clone(value) {
71
- if (ValueGuard.IsArray(value))
56
+ if ((0, guard_1.IsArray)(value))
72
57
  return ArrayType(value);
73
- if (ValueGuard.IsAsyncIterator(value))
74
- return AsyncIteratorType(value);
75
- if (ValueGuard.IsFunction(value))
76
- return FunctionType(value);
77
- if (ValueGuard.IsIterator(value))
78
- return IteratorType(value);
79
- if (ValueGuard.IsPromise(value))
80
- return PromiseType(value);
81
- if (ValueGuard.IsDate(value))
58
+ if ((0, guard_1.IsDate)(value))
82
59
  return DateType(value);
83
- if (ValueGuard.IsPlainObject(value))
60
+ if ((0, guard_1.IsPlainObject)(value))
84
61
  return ObjectType(value);
85
- if (ValueGuard.IsTypedArray(value))
62
+ if ((0, guard_1.IsTypedArray)(value))
86
63
  return TypedArrayType(value);
87
- if (ValueGuard.IsValueType(value))
64
+ if ((0, guard_1.IsValueType)(value))
88
65
  return ValueType(value);
89
66
  throw new Error('ValueClone: Unable to clone value');
90
67
  }
@@ -1,12 +1,9 @@
1
1
  import * as Types from '../typebox';
2
- export declare class ValueConvertUnknownTypeError extends Error {
2
+ export declare class ValueConvertUnknownTypeError extends Types.TypeBoxError {
3
3
  readonly schema: Types.TSchema;
4
4
  constructor(schema: Types.TSchema);
5
5
  }
6
- export declare class ValueConvertDereferenceError extends Error {
7
- readonly schema: Types.TRef | Types.TThis;
8
- constructor(schema: Types.TRef | Types.TThis);
9
- }
6
+ export declare function Default(value: any): any;
10
7
  /** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
11
8
  export declare function Convert<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: unknown): unknown;
12
9
  /** Converts any type mismatched values to their target type if a reasonable conversion is possible. */