@sinclair/typebox 0.32.7 → 0.32.9

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.
@@ -2,13 +2,13 @@
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.SetUnionMany = exports.SetIntersectMany = exports.SetComplement = exports.SetUnion = exports.SetIntersect = exports.SetDistinct = exports.SetIsSubset = exports.SetIncludes = void 0;
5
- /** Returns true if element S is in the set of T */
5
+ /** Returns true if element right is in the set of left */
6
6
  // prettier-ignore
7
7
  function SetIncludes(T, S) {
8
8
  return T.includes(S);
9
9
  }
10
10
  exports.SetIncludes = SetIncludes;
11
- /** Returns true if T is a subset of S */
11
+ /** Returns true if left is a subset of right */
12
12
  function SetIsSubset(T, S) {
13
13
  return T.every((L) => SetIncludes(S, L));
14
14
  }
@@ -34,10 +34,20 @@ function SetComplement(T, S) {
34
34
  return T.filter(L => !S.includes(L));
35
35
  }
36
36
  exports.SetComplement = SetComplement;
37
- /** Returns the Intersect of multiple sets */
37
+ // prettier-ignore
38
+ function SetIntersectManyResolve(T, Init) {
39
+ return T.reduce((Acc, L) => {
40
+ return SetIntersect(Acc, L);
41
+ }, Init);
42
+ }
38
43
  // prettier-ignore
39
44
  function SetIntersectMany(T) {
40
- return (T.length === 1 ? T[0] : T.reduce((Acc, L) => [...SetIntersect(Acc, L)], []));
45
+ return (T.length === 1
46
+ ? T[0]
47
+ // Use left to initialize the accumulator for resolve
48
+ : T.length > 1
49
+ ? SetIntersectManyResolve(T.slice(1), T[0])
50
+ : []);
41
51
  }
42
52
  exports.SetIntersectMany = SetIntersectMany;
43
53
  /** Returns the Union of multiple sets */
@@ -71,7 +71,7 @@ export declare class JsonTypeBuilder {
71
71
  /** `[Json]` Intrinsic function to Capitalize LiteralString types */
72
72
  Capitalize<T extends TSchema>(schema: T, options?: SchemaOptions): TCapitalize<T>;
73
73
  /** `[Json]` Creates a Composite object type */
74
- Composite<T extends TObject[]>(objects: [...T], options?: ObjectOptions): TComposite<T>;
74
+ Composite<T extends TSchema[]>(schemas: [...T], options?: ObjectOptions): TComposite<T>;
75
75
  /** `[JavaScript]` Creates a readonly const type from the given value. */
76
76
  Const</* const (not supported in 4.0) */ T>(value: T, options?: SchemaOptions): TConst<T>;
77
77
  /** `[Json]` Creates a dereferenced type */
@@ -87,8 +87,8 @@ class JsonTypeBuilder {
87
87
  return (0, index_14.Capitalize)(schema, options);
88
88
  }
89
89
  /** `[Json]` Creates a Composite object type */
90
- Composite(objects, options) {
91
- return (0, index_4.Composite)(objects, options); // (error) TS 5.4.0-dev - review TComposite implementation
90
+ Composite(schemas, options) {
91
+ return (0, index_4.Composite)(schemas, options); // (error) TS 5.4.0-dev - review TComposite implementation
92
92
  }
93
93
  /** `[JavaScript]` Creates a readonly const type from the given value. */
94
94
  Const(value, options = {}) {
@@ -57,7 +57,7 @@ function SelectUnion(union, references, value) {
57
57
  }
58
58
  function CastUnion(union, references, value) {
59
59
  if ('default' in union) {
60
- return union.default;
60
+ return typeof value === 'function' ? union.default : (0, index_6.Clone)(union.default);
61
61
  }
62
62
  else {
63
63
  const schema = SelectUnion(union, references, value);
@@ -4,16 +4,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.Create = exports.ValueCreateError = void 0;
5
5
  const index_1 = require("../guard/index");
6
6
  const index_2 = require("../check/index");
7
- const index_3 = require("../deref/index");
8
- const index_4 = require("../../type/template-literal/index");
9
- const index_5 = require("../../type/patterns/index");
10
- const index_6 = require("../../type/registry/index");
11
- const index_7 = require("../../type/symbols/index");
12
- const index_8 = require("../../type/error/index");
7
+ const index_3 = require("../clone/index");
8
+ const index_4 = require("../deref/index");
9
+ const index_5 = require("../../type/template-literal/index");
10
+ const index_6 = require("../../type/patterns/index");
11
+ const index_7 = require("../../type/registry/index");
12
+ const index_8 = require("../../type/symbols/index");
13
+ const index_9 = require("../../type/error/index");
13
14
  // ------------------------------------------------------------------
14
15
  // Errors
15
16
  // ------------------------------------------------------------------
16
- class ValueCreateError extends index_8.TypeBoxError {
17
+ class ValueCreateError extends index_9.TypeBoxError {
17
18
  constructor(schema, message) {
18
19
  super(message);
19
20
  this.schema = schema;
@@ -21,11 +22,17 @@ class ValueCreateError extends index_8.TypeBoxError {
21
22
  }
22
23
  exports.ValueCreateError = ValueCreateError;
23
24
  // ------------------------------------------------------------------
25
+ // Default
26
+ // ------------------------------------------------------------------
27
+ function FromDefault(value) {
28
+ return typeof value === 'function' ? value : (0, index_3.Clone)(value);
29
+ }
30
+ // ------------------------------------------------------------------
24
31
  // Create
25
32
  // ------------------------------------------------------------------
26
33
  function FromAny(schema, references) {
27
34
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
28
- return schema.default;
35
+ return FromDefault(schema.default);
29
36
  }
30
37
  else {
31
38
  return {};
@@ -39,7 +46,7 @@ function FromArray(schema, references) {
39
46
  throw new ValueCreateError(schema, 'Array with the contains constraint requires a default value');
40
47
  }
41
48
  else if ('default' in schema) {
42
- return schema.default;
49
+ return FromDefault(schema.default);
43
50
  }
44
51
  else if (schema.minItems !== undefined) {
45
52
  return Array.from({ length: schema.minItems }).map((item) => {
@@ -52,7 +59,7 @@ function FromArray(schema, references) {
52
59
  }
53
60
  function FromAsyncIterator(schema, references) {
54
61
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
55
- return schema.default;
62
+ return FromDefault(schema.default);
56
63
  }
57
64
  else {
58
65
  return (async function* () { })();
@@ -60,7 +67,7 @@ function FromAsyncIterator(schema, references) {
60
67
  }
61
68
  function FromBigInt(schema, references) {
62
69
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
63
- return schema.default;
70
+ return FromDefault(schema.default);
64
71
  }
65
72
  else {
66
73
  return BigInt(0);
@@ -68,7 +75,7 @@ function FromBigInt(schema, references) {
68
75
  }
69
76
  function FromBoolean(schema, references) {
70
77
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
71
- return schema.default;
78
+ return FromDefault(schema.default);
72
79
  }
73
80
  else {
74
81
  return false;
@@ -76,7 +83,7 @@ function FromBoolean(schema, references) {
76
83
  }
77
84
  function FromConstructor(schema, references) {
78
85
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
79
- return schema.default;
86
+ return FromDefault(schema.default);
80
87
  }
81
88
  else {
82
89
  const value = Visit(schema.returns, references);
@@ -98,7 +105,7 @@ function FromConstructor(schema, references) {
98
105
  }
99
106
  function FromDate(schema, references) {
100
107
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
101
- return schema.default;
108
+ return FromDefault(schema.default);
102
109
  }
103
110
  else if (schema.minimumTimestamp !== undefined) {
104
111
  return new Date(schema.minimumTimestamp);
@@ -109,7 +116,7 @@ function FromDate(schema, references) {
109
116
  }
110
117
  function FromFunction(schema, references) {
111
118
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
112
- return schema.default;
119
+ return FromDefault(schema.default);
113
120
  }
114
121
  else {
115
122
  return () => Visit(schema.returns, references);
@@ -117,7 +124,7 @@ function FromFunction(schema, references) {
117
124
  }
118
125
  function FromInteger(schema, references) {
119
126
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
120
- return schema.default;
127
+ return FromDefault(schema.default);
121
128
  }
122
129
  else if (schema.minimum !== undefined) {
123
130
  return schema.minimum;
@@ -128,7 +135,7 @@ function FromInteger(schema, references) {
128
135
  }
129
136
  function FromIntersect(schema, references) {
130
137
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
131
- return schema.default;
138
+ return FromDefault(schema.default);
132
139
  }
133
140
  else {
134
141
  // --------------------------------------------------------------
@@ -149,7 +156,7 @@ function FromIntersect(schema, references) {
149
156
  }
150
157
  function FromIterator(schema, references) {
151
158
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
152
- return schema.default;
159
+ return FromDefault(schema.default);
153
160
  }
154
161
  else {
155
162
  return (function* () { })();
@@ -157,7 +164,7 @@ function FromIterator(schema, references) {
157
164
  }
158
165
  function FromLiteral(schema, references) {
159
166
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
160
- return schema.default;
167
+ return FromDefault(schema.default);
161
168
  }
162
169
  else {
163
170
  return schema.const;
@@ -165,7 +172,7 @@ function FromLiteral(schema, references) {
165
172
  }
166
173
  function FromNever(schema, references) {
167
174
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
168
- return schema.default;
175
+ return FromDefault(schema.default);
169
176
  }
170
177
  else {
171
178
  throw new ValueCreateError(schema, 'Never types cannot be created. Consider using a default value.');
@@ -173,7 +180,7 @@ function FromNever(schema, references) {
173
180
  }
174
181
  function FromNot(schema, references) {
175
182
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
176
- return schema.default;
183
+ return FromDefault(schema.default);
177
184
  }
178
185
  else {
179
186
  throw new ValueCreateError(schema, 'Not types must have a default value');
@@ -181,7 +188,7 @@ function FromNot(schema, references) {
181
188
  }
182
189
  function FromNull(schema, references) {
183
190
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
184
- return schema.default;
191
+ return FromDefault(schema.default);
185
192
  }
186
193
  else {
187
194
  return null;
@@ -189,7 +196,7 @@ function FromNull(schema, references) {
189
196
  }
190
197
  function FromNumber(schema, references) {
191
198
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
192
- return schema.default;
199
+ return FromDefault(schema.default);
193
200
  }
194
201
  else if (schema.minimum !== undefined) {
195
202
  return schema.minimum;
@@ -200,11 +207,11 @@ function FromNumber(schema, references) {
200
207
  }
201
208
  function FromObject(schema, references) {
202
209
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
203
- return schema.default;
210
+ return FromDefault(schema.default);
204
211
  }
205
212
  else {
206
213
  const required = new Set(schema.required);
207
- return (schema.default ||
214
+ return (FromDefault(schema.default) ||
208
215
  Object.entries(schema.properties).reduce((acc, [key, schema]) => {
209
216
  return required.has(key) ? { ...acc, [key]: Visit(schema, references) } : { ...acc };
210
217
  }, {}));
@@ -212,7 +219,7 @@ function FromObject(schema, references) {
212
219
  }
213
220
  function FromPromise(schema, references) {
214
221
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
215
- return schema.default;
222
+ return FromDefault(schema.default);
216
223
  }
217
224
  else {
218
225
  return Promise.resolve(Visit(schema.item, references));
@@ -221,9 +228,9 @@ function FromPromise(schema, references) {
221
228
  function FromRecord(schema, references) {
222
229
  const [keyPattern, valueSchema] = Object.entries(schema.patternProperties)[0];
223
230
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
224
- return schema.default;
231
+ return FromDefault(schema.default);
225
232
  }
226
- else if (!(keyPattern === index_5.PatternStringExact || keyPattern === index_5.PatternNumberExact)) {
233
+ else if (!(keyPattern === index_6.PatternStringExact || keyPattern === index_6.PatternNumberExact)) {
227
234
  const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
228
235
  return propertyKeys.reduce((acc, key) => {
229
236
  return { ...acc, [key]: Visit(valueSchema, references) };
@@ -235,15 +242,15 @@ function FromRecord(schema, references) {
235
242
  }
236
243
  function FromRef(schema, references) {
237
244
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
238
- return schema.default;
245
+ return FromDefault(schema.default);
239
246
  }
240
247
  else {
241
- return Visit((0, index_3.Deref)(schema, references), references);
248
+ return Visit((0, index_4.Deref)(schema, references), references);
242
249
  }
243
250
  }
244
251
  function FromRegExp(schema, references) {
245
252
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
246
- return schema.default;
253
+ return FromDefault(schema.default);
247
254
  }
248
255
  else {
249
256
  throw new ValueCreateError(schema, 'RegExp types cannot be created. Consider using a default value.');
@@ -255,7 +262,7 @@ function FromString(schema, references) {
255
262
  throw new ValueCreateError(schema, 'String types with patterns must specify a default value');
256
263
  }
257
264
  else {
258
- return schema.default;
265
+ return FromDefault(schema.default);
259
266
  }
260
267
  }
261
268
  else if (schema.format !== undefined) {
@@ -263,12 +270,12 @@ function FromString(schema, references) {
263
270
  throw new ValueCreateError(schema, 'String types with formats must specify a default value');
264
271
  }
265
272
  else {
266
- return schema.default;
273
+ return FromDefault(schema.default);
267
274
  }
268
275
  }
269
276
  else {
270
277
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
271
- return schema.default;
278
+ return FromDefault(schema.default);
272
279
  }
273
280
  else if (schema.minLength !== undefined) {
274
281
  // prettier-ignore
@@ -281,7 +288,7 @@ function FromString(schema, references) {
281
288
  }
282
289
  function FromSymbol(schema, references) {
283
290
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
284
- return schema.default;
291
+ return FromDefault(schema.default);
285
292
  }
286
293
  else if ('value' in schema) {
287
294
  return Symbol.for(schema.value);
@@ -292,26 +299,26 @@ function FromSymbol(schema, references) {
292
299
  }
293
300
  function FromTemplateLiteral(schema, references) {
294
301
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
295
- return schema.default;
302
+ return FromDefault(schema.default);
296
303
  }
297
- if (!(0, index_4.IsTemplateLiteralFinite)(schema))
304
+ if (!(0, index_5.IsTemplateLiteralFinite)(schema))
298
305
  throw new ValueCreateError(schema, 'Can only create template literals that produce a finite variants. Consider using a default value.');
299
- const generated = (0, index_4.TemplateLiteralGenerate)(schema);
306
+ const generated = (0, index_5.TemplateLiteralGenerate)(schema);
300
307
  return generated[0];
301
308
  }
302
309
  function FromThis(schema, references) {
303
310
  if (recursiveDepth++ > recursiveMaxDepth)
304
311
  throw new ValueCreateError(schema, 'Cannot create recursive type as it appears possibly infinite. Consider using a default.');
305
312
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
306
- return schema.default;
313
+ return FromDefault(schema.default);
307
314
  }
308
315
  else {
309
- return Visit((0, index_3.Deref)(schema, references), references);
316
+ return Visit((0, index_4.Deref)(schema, references), references);
310
317
  }
311
318
  }
312
319
  function FromTuple(schema, references) {
313
320
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
314
- return schema.default;
321
+ return FromDefault(schema.default);
315
322
  }
316
323
  if (schema.items === undefined) {
317
324
  return [];
@@ -322,7 +329,7 @@ function FromTuple(schema, references) {
322
329
  }
323
330
  function FromUndefined(schema, references) {
324
331
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
325
- return schema.default;
332
+ return FromDefault(schema.default);
326
333
  }
327
334
  else {
328
335
  return undefined;
@@ -330,7 +337,7 @@ function FromUndefined(schema, references) {
330
337
  }
331
338
  function FromUnion(schema, references) {
332
339
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
333
- return schema.default;
340
+ return FromDefault(schema.default);
334
341
  }
335
342
  else if (schema.anyOf.length === 0) {
336
343
  throw new Error('ValueCreate.Union: Cannot create Union with zero variants');
@@ -341,7 +348,7 @@ function FromUnion(schema, references) {
341
348
  }
342
349
  function FromUint8Array(schema, references) {
343
350
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
344
- return schema.default;
351
+ return FromDefault(schema.default);
345
352
  }
346
353
  else if (schema.minByteLength !== undefined) {
347
354
  return new Uint8Array(schema.minByteLength);
@@ -352,7 +359,7 @@ function FromUint8Array(schema, references) {
352
359
  }
353
360
  function FromUnknown(schema, references) {
354
361
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
355
- return schema.default;
362
+ return FromDefault(schema.default);
356
363
  }
357
364
  else {
358
365
  return {};
@@ -360,7 +367,7 @@ function FromUnknown(schema, references) {
360
367
  }
361
368
  function FromVoid(schema, references) {
362
369
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
363
- return schema.default;
370
+ return FromDefault(schema.default);
364
371
  }
365
372
  else {
366
373
  return void 0;
@@ -368,7 +375,7 @@ function FromVoid(schema, references) {
368
375
  }
369
376
  function FromKind(schema, references) {
370
377
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
371
- return schema.default;
378
+ return FromDefault(schema.default);
372
379
  }
373
380
  else {
374
381
  throw new Error('User defined types must specify a default value');
@@ -377,7 +384,7 @@ function FromKind(schema, references) {
377
384
  function Visit(schema, references) {
378
385
  const references_ = (0, index_1.IsString)(schema.$id) ? [...references, schema] : references;
379
386
  const schema_ = schema;
380
- switch (schema_[index_7.Kind]) {
387
+ switch (schema_[index_8.Kind]) {
381
388
  case 'Any':
382
389
  return FromAny(schema_, references_);
383
390
  case 'Array':
@@ -441,7 +448,7 @@ function Visit(schema, references) {
441
448
  case 'Void':
442
449
  return FromVoid(schema_, references_);
443
450
  default:
444
- if (!index_6.TypeRegistry.Has(schema_[index_7.Kind]))
451
+ if (!index_7.TypeRegistry.Has(schema_[index_8.Kind]))
445
452
  throw new ValueCreateError(schema_, 'Unknown type');
446
453
  return FromKind(schema_, references_);
447
454
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.32.7",
3
+ "version": "0.32.9",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -53,7 +53,7 @@ type T = Static<typeof T> // type T = {
53
53
 
54
54
  TypeBox is a runtime type builder that creates in-memory Json Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type that can be statically checked by TypeScript and runtime asserted using standard Json Schema validation.
55
55
 
56
- This library is designed to be a runtime type system providing similar capabilities to TypeScript's programmable type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
56
+ This library is designed to allow Json Schema to compose with the same flexibility as TypeScript's programmable type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
57
57
 
58
58
  License MIT
59
59
 
@@ -1814,4 +1814,4 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
1814
1814
 
1815
1815
  ## Contribute
1816
1816
 
1817
- TypeBox is open to community contribution. Please ensure you submit an open issue before submitting your pull request. The TypeBox project preferences open community discussion prior to accepting new features.
1817
+ TypeBox is open to community contribution. Please ensure you submit an open issue before submitting your pull request. The TypeBox project prefers open community discussion before accepting new features.