@sinclair/typebox 0.25.23 → 0.25.25

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.
Files changed (61) hide show
  1. package/compiler/compiler.d.ts +25 -28
  2. package/compiler/compiler.js +495 -495
  3. package/compiler/index.d.ts +2 -2
  4. package/compiler/index.js +47 -47
  5. package/conditional/conditional.d.ts +17 -17
  6. package/conditional/conditional.js +91 -91
  7. package/conditional/index.d.ts +2 -2
  8. package/conditional/index.js +45 -45
  9. package/conditional/structural.d.ts +11 -11
  10. package/conditional/structural.js +685 -685
  11. package/custom/custom.d.ts +12 -12
  12. package/custom/custom.js +55 -55
  13. package/custom/index.d.ts +1 -1
  14. package/custom/index.js +44 -44
  15. package/errors/errors.d.ts +67 -67
  16. package/errors/errors.js +472 -468
  17. package/errors/index.d.ts +1 -1
  18. package/errors/index.js +44 -44
  19. package/format/format.d.ts +12 -12
  20. package/format/format.js +55 -55
  21. package/format/index.d.ts +1 -1
  22. package/format/index.js +44 -44
  23. package/guard/extends.d.ts +10 -0
  24. package/guard/extends.js +50 -0
  25. package/guard/guard.d.ts +60 -60
  26. package/guard/guard.js +440 -440
  27. package/guard/index.d.ts +2 -1
  28. package/guard/index.js +45 -44
  29. package/hash/hash.d.ts +8 -8
  30. package/hash/hash.js +183 -183
  31. package/hash/index.d.ts +1 -1
  32. package/hash/index.js +44 -44
  33. package/license +22 -22
  34. package/package.json +55 -52
  35. package/readme.md +1237 -1325
  36. package/system/index.d.ts +1 -1
  37. package/system/index.js +44 -44
  38. package/system/system.d.ts +17 -17
  39. package/system/system.js +69 -69
  40. package/typebox.d.ts +422 -422
  41. package/typebox.js +388 -388
  42. package/value/cast.d.ts +26 -26
  43. package/value/cast.js +415 -420
  44. package/value/check.d.ts +8 -8
  45. package/value/check.js +405 -395
  46. package/value/clone.d.ts +3 -3
  47. package/value/clone.js +71 -71
  48. package/value/create.d.ts +14 -14
  49. package/value/create.js +378 -388
  50. package/value/delta.d.ts +43 -43
  51. package/value/delta.js +204 -204
  52. package/value/equal.d.ts +3 -3
  53. package/value/equal.js +80 -80
  54. package/value/index.d.ts +4 -4
  55. package/value/index.js +53 -53
  56. package/value/is.d.ts +11 -11
  57. package/value/is.js +53 -53
  58. package/value/pointer.d.ts +24 -24
  59. package/value/pointer.js +142 -142
  60. package/value/value.d.ts +32 -32
  61. package/value/value.js +87 -87
package/typebox.js CHANGED
@@ -1,388 +1,388 @@
1
- "use strict";
2
- /*--------------------------------------------------------------------------
3
-
4
- @sinclair/typebox
5
-
6
- The MIT License (MIT)
7
-
8
- Copyright (c) 2017-2023 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.Type = exports.TypeBuilder = exports.Modifier = exports.Hint = exports.Kind = void 0;
31
- // --------------------------------------------------------------------------
32
- // Symbols
33
- // --------------------------------------------------------------------------
34
- exports.Kind = Symbol.for('TypeBox.Kind');
35
- exports.Hint = Symbol.for('TypeBox.Hint');
36
- exports.Modifier = Symbol.for('TypeBox.Modifier');
37
- // --------------------------------------------------------------------------
38
- // TypeBuilder
39
- // --------------------------------------------------------------------------
40
- let TypeOrdinal = 0;
41
- class TypeBuilder {
42
- // ----------------------------------------------------------------------
43
- // Modifiers
44
- // ----------------------------------------------------------------------
45
- /** Creates a readonly optional property */
46
- ReadonlyOptional(item) {
47
- return { [exports.Modifier]: 'ReadonlyOptional', ...item };
48
- }
49
- /** Creates a readonly property */
50
- Readonly(item) {
51
- return { [exports.Modifier]: 'Readonly', ...item };
52
- }
53
- /** Creates a optional property */
54
- Optional(item) {
55
- return { [exports.Modifier]: 'Optional', ...item };
56
- }
57
- // ----------------------------------------------------------------------
58
- // Types
59
- // ----------------------------------------------------------------------
60
- /** `Standard` Creates a any type */
61
- Any(options = {}) {
62
- return this.Create({ ...options, [exports.Kind]: 'Any' });
63
- }
64
- /** `Standard` Creates a array type */
65
- Array(items, options = {}) {
66
- return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items });
67
- }
68
- /** `Standard` Creates a boolean type */
69
- Boolean(options = {}) {
70
- return this.Create({ ...options, [exports.Kind]: 'Boolean', type: 'boolean' });
71
- }
72
- /** `Extended` Creates a tuple type from this constructors parameters */
73
- ConstructorParameters(schema, options = {}) {
74
- return this.Tuple([...schema.parameters], { ...options });
75
- }
76
- /** `Extended` Creates a constructor type */
77
- Constructor(parameters, returns, options = {}) {
78
- if (parameters[exports.Kind] === 'Tuple') {
79
- const inner = parameters.items === undefined ? [] : parameters.items;
80
- return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: inner, returns });
81
- }
82
- else if (globalThis.Array.isArray(parameters)) {
83
- return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters, returns });
84
- }
85
- else {
86
- throw new Error('TypeBuilder.Constructor: Invalid parameters');
87
- }
88
- }
89
- /** `Extended` Creates a Date type */
90
- Date(options = {}) {
91
- return this.Create({ ...options, [exports.Kind]: 'Date', type: 'object', instanceOf: 'Date' });
92
- }
93
- /** `Standard` Creates a enum type */
94
- Enum(item, options = {}) {
95
- const values = Object.keys(item)
96
- .filter((key) => isNaN(key))
97
- .map((key) => item[key]);
98
- const anyOf = values.map((value) => (typeof value === 'string' ? { [exports.Kind]: 'Literal', type: 'string', const: value } : { [exports.Kind]: 'Literal', type: 'number', const: value }));
99
- return this.Create({ ...options, [exports.Kind]: 'Union', [exports.Hint]: 'Enum', anyOf });
100
- }
101
- /** `Extended` Creates a function type */
102
- Function(parameters, returns, options = {}) {
103
- if (parameters[exports.Kind] === 'Tuple') {
104
- const inner = parameters.items === undefined ? [] : parameters.items;
105
- return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters: inner, returns });
106
- }
107
- else if (globalThis.Array.isArray(parameters)) {
108
- return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters, returns });
109
- }
110
- else {
111
- throw new Error('TypeBuilder.Function: Invalid parameters');
112
- }
113
- }
114
- /** `Extended` Creates a type from this constructors instance type */
115
- InstanceType(schema, options = {}) {
116
- return { ...options, ...this.Clone(schema.returns) };
117
- }
118
- /** `Standard` Creates a integer type */
119
- Integer(options = {}) {
120
- return this.Create({ ...options, [exports.Kind]: 'Integer', type: 'integer' });
121
- }
122
- /** `Standard` Creates a intersect type. */
123
- Intersect(objects, options = {}) {
124
- const isOptional = (schema) => (schema[exports.Modifier] && schema[exports.Modifier] === 'Optional') || schema[exports.Modifier] === 'ReadonlyOptional';
125
- const [required, optional] = [new Set(), new Set()];
126
- for (const object of objects) {
127
- for (const [key, schema] of Object.entries(object.properties)) {
128
- if (isOptional(schema))
129
- optional.add(key);
130
- }
131
- }
132
- for (const object of objects) {
133
- for (const key of Object.keys(object.properties)) {
134
- if (!optional.has(key))
135
- required.add(key);
136
- }
137
- }
138
- const properties = {};
139
- for (const object of objects) {
140
- for (const [key, schema] of Object.entries(object.properties)) {
141
- properties[key] = properties[key] === undefined ? schema : { [exports.Kind]: 'Union', anyOf: [properties[key], { ...schema }] };
142
- }
143
- }
144
- if (required.size > 0) {
145
- return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties, required: [...required] });
146
- }
147
- else {
148
- return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties });
149
- }
150
- }
151
- /** `Standard` Creates a keyof type */
152
- KeyOf(object, options = {}) {
153
- const items = Object.keys(object.properties).map((key) => this.Create({ ...options, [exports.Kind]: 'Literal', type: 'string', const: key }));
154
- return this.Create({ ...options, [exports.Kind]: 'Union', [exports.Hint]: 'KeyOf', anyOf: items });
155
- }
156
- /** `Standard` Creates a literal type. */
157
- Literal(value, options = {}) {
158
- return this.Create({ ...options, [exports.Kind]: 'Literal', const: value, type: typeof value });
159
- }
160
- /** `Standard` Creates a never type */
161
- Never(options = {}) {
162
- return this.Create({
163
- ...options,
164
- [exports.Kind]: 'Never',
165
- allOf: [
166
- { type: 'boolean', const: false },
167
- { type: 'boolean', const: true },
168
- ],
169
- });
170
- }
171
- /** `Standard` Creates a null type */
172
- Null(options = {}) {
173
- return this.Create({ ...options, [exports.Kind]: 'Null', type: 'null' });
174
- }
175
- /** `Standard` Creates a number type */
176
- Number(options = {}) {
177
- return this.Create({ ...options, [exports.Kind]: 'Number', type: 'number' });
178
- }
179
- /** `Standard` Creates an object type */
180
- Object(properties, options = {}) {
181
- const property_names = Object.keys(properties);
182
- const optional = property_names.filter((name) => {
183
- const property = properties[name];
184
- const modifier = property[exports.Modifier];
185
- return modifier && (modifier === 'Optional' || modifier === 'ReadonlyOptional');
186
- });
187
- const required = property_names.filter((name) => !optional.includes(name));
188
- if (required.length > 0) {
189
- return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties, required });
190
- }
191
- else {
192
- return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties });
193
- }
194
- }
195
- /** `Standard` Creates a new object type whose keys are omitted from the given source type */
196
- Omit(schema, keys, options = {}) {
197
- const select = keys[exports.Kind] === 'Union' ? keys.anyOf.map((schema) => schema.const) : keys;
198
- const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Omit' };
199
- if (next.required) {
200
- next.required = next.required.filter((key) => !select.includes(key));
201
- if (next.required.length === 0)
202
- delete next.required;
203
- }
204
- for (const key of Object.keys(next.properties)) {
205
- if (select.includes(key))
206
- delete next.properties[key];
207
- }
208
- return this.Create(next);
209
- }
210
- /** `Extended` Creates a tuple type from this functions parameters */
211
- Parameters(schema, options = {}) {
212
- return exports.Type.Tuple(schema.parameters, { ...options });
213
- }
214
- /** `Standard` Creates an object type whose properties are all optional */
215
- Partial(schema, options = {}) {
216
- const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Partial' };
217
- delete next.required;
218
- for (const key of Object.keys(next.properties)) {
219
- const property = next.properties[key];
220
- const modifer = property[exports.Modifier];
221
- switch (modifer) {
222
- case 'ReadonlyOptional':
223
- property[exports.Modifier] = 'ReadonlyOptional';
224
- break;
225
- case 'Readonly':
226
- property[exports.Modifier] = 'ReadonlyOptional';
227
- break;
228
- case 'Optional':
229
- property[exports.Modifier] = 'Optional';
230
- break;
231
- default:
232
- property[exports.Modifier] = 'Optional';
233
- break;
234
- }
235
- }
236
- return this.Create(next);
237
- }
238
- /** `Standard` Creates a new object type whose keys are picked from the given source type */
239
- Pick(schema, keys, options = {}) {
240
- const select = keys[exports.Kind] === 'Union' ? keys.anyOf.map((schema) => schema.const) : keys;
241
- const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Pick' };
242
- if (next.required) {
243
- next.required = next.required.filter((key) => select.includes(key));
244
- if (next.required.length === 0)
245
- delete next.required;
246
- }
247
- for (const key of Object.keys(next.properties)) {
248
- if (!select.includes(key))
249
- delete next.properties[key];
250
- }
251
- return this.Create(next);
252
- }
253
- /** `Extended` Creates a Promise type */
254
- Promise(item, options = {}) {
255
- return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'object', instanceOf: 'Promise', item });
256
- }
257
- /** `Standard` Creates a record type */
258
- Record(key, value, options = {}) {
259
- // If string literal union return TObject with properties extracted from union.
260
- if (key[exports.Kind] === 'Union') {
261
- return this.Object(key.anyOf.reduce((acc, literal) => {
262
- return { ...acc, [literal.const]: value };
263
- }, {}), { ...options, [exports.Hint]: 'Record' });
264
- }
265
- // otherwise return TRecord with patternProperties
266
- const pattern = ['Integer', 'Number'].includes(key[exports.Kind]) ? '^(0|[1-9][0-9]*)$' : key[exports.Kind] === 'String' && key.pattern ? key.pattern : '^.*$';
267
- return this.Create({
268
- ...options,
269
- [exports.Kind]: 'Record',
270
- type: 'object',
271
- patternProperties: { [pattern]: value },
272
- additionalProperties: false,
273
- });
274
- }
275
- /** `Standard` Creates recursive type */
276
- Recursive(callback, options = {}) {
277
- if (options.$id === undefined)
278
- options.$id = `T${TypeOrdinal++}`;
279
- const self = callback({ [exports.Kind]: 'Self', $ref: `${options.$id}` });
280
- self.$id = options.$id;
281
- return this.Create({ ...options, ...self });
282
- }
283
- /** `Standard` Creates a reference type. The referenced type must contain a $id. */
284
- Ref(schema, options = {}) {
285
- if (schema.$id === undefined)
286
- throw Error('TypeBuilder.Ref: Referenced schema must specify an $id');
287
- return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: schema.$id });
288
- }
289
- /** `Standard` Creates a string type from a regular expression */
290
- RegEx(regex, options = {}) {
291
- return this.Create({ ...options, [exports.Kind]: 'String', type: 'string', pattern: regex.source });
292
- }
293
- /** `Standard` Creates an object type whose properties are all required */
294
- Required(schema, options = {}) {
295
- const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Required' };
296
- next.required = Object.keys(next.properties);
297
- for (const key of Object.keys(next.properties)) {
298
- const property = next.properties[key];
299
- const modifier = property[exports.Modifier];
300
- switch (modifier) {
301
- case 'ReadonlyOptional':
302
- property[exports.Modifier] = 'Readonly';
303
- break;
304
- case 'Readonly':
305
- property[exports.Modifier] = 'Readonly';
306
- break;
307
- case 'Optional':
308
- delete property[exports.Modifier];
309
- break;
310
- default:
311
- delete property[exports.Modifier];
312
- break;
313
- }
314
- }
315
- return this.Create(next);
316
- }
317
- /** `Extended` Creates a type from this functions return type */
318
- ReturnType(schema, options = {}) {
319
- return { ...options, ...this.Clone(schema.returns) };
320
- }
321
- /** Removes Kind and Modifier symbol property keys from this schema */
322
- Strict(schema) {
323
- return JSON.parse(JSON.stringify(schema));
324
- }
325
- /** `Standard` Creates a string type */
326
- String(options = {}) {
327
- return this.Create({ ...options, [exports.Kind]: 'String', type: 'string' });
328
- }
329
- /** `Standard` Creates a tuple type */
330
- Tuple(items, options = {}) {
331
- const additionalItems = false;
332
- const minItems = items.length;
333
- const maxItems = items.length;
334
- const schema = (items.length > 0 ? { ...options, [exports.Kind]: 'Tuple', type: 'array', items, additionalItems, minItems, maxItems } : { ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
335
- return this.Create(schema);
336
- }
337
- /** `Extended` Creates a undefined type */
338
- Undefined(options = {}) {
339
- return this.Create({ ...options, [exports.Kind]: 'Undefined', type: 'null', typeOf: 'Undefined' });
340
- }
341
- /** `Standard` Creates a union type */
342
- Union(items, options = {}) {
343
- return items.length === 0 ? exports.Type.Never({ ...options }) : this.Create({ ...options, [exports.Kind]: 'Union', anyOf: items });
344
- }
345
- /** `Extended` Creates a Uint8Array type */
346
- Uint8Array(options = {}) {
347
- return this.Create({ ...options, [exports.Kind]: 'Uint8Array', type: 'object', instanceOf: 'Uint8Array' });
348
- }
349
- /** `Standard` Creates an unknown type */
350
- Unknown(options = {}) {
351
- return this.Create({ ...options, [exports.Kind]: 'Unknown' });
352
- }
353
- /** `Standard` Creates a user defined schema that infers as type T */
354
- Unsafe(options = {}) {
355
- return this.Create({ ...options, [exports.Kind]: options[exports.Kind] || 'Unsafe' });
356
- }
357
- /** `Extended` Creates a void type */
358
- Void(options = {}) {
359
- return this.Create({ ...options, [exports.Kind]: 'Void', type: 'null', typeOf: 'Void' });
360
- }
361
- /** Use this function to return TSchema with static and params omitted */
362
- Create(schema) {
363
- return schema;
364
- }
365
- /** Clones the given value */
366
- Clone(value) {
367
- const isObject = (object) => typeof object === 'object' && object !== null && !Array.isArray(object);
368
- const isArray = (object) => typeof object === 'object' && object !== null && Array.isArray(object);
369
- if (isObject(value)) {
370
- return Object.keys(value).reduce((acc, key) => ({
371
- ...acc,
372
- [key]: this.Clone(value[key]),
373
- }), Object.getOwnPropertySymbols(value).reduce((acc, key) => ({
374
- ...acc,
375
- [key]: this.Clone(value[key]),
376
- }), {}));
377
- }
378
- else if (isArray(value)) {
379
- return value.map((item) => this.Clone(item));
380
- }
381
- else {
382
- return value;
383
- }
384
- }
385
- }
386
- exports.TypeBuilder = TypeBuilder;
387
- /** JSON Schema Type Builder with Static Type Resolution for TypeScript */
388
- exports.Type = new TypeBuilder();
1
+ "use strict";
2
+ /*--------------------------------------------------------------------------
3
+
4
+ @sinclair/typebox
5
+
6
+ The MIT License (MIT)
7
+
8
+ Copyright (c) 2017-2023 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.Type = exports.TypeBuilder = exports.Modifier = exports.Hint = exports.Kind = void 0;
31
+ // --------------------------------------------------------------------------
32
+ // Symbols
33
+ // --------------------------------------------------------------------------
34
+ exports.Kind = Symbol.for('TypeBox.Kind');
35
+ exports.Hint = Symbol.for('TypeBox.Hint');
36
+ exports.Modifier = Symbol.for('TypeBox.Modifier');
37
+ // --------------------------------------------------------------------------
38
+ // TypeBuilder
39
+ // --------------------------------------------------------------------------
40
+ let TypeOrdinal = 0;
41
+ class TypeBuilder {
42
+ // ----------------------------------------------------------------------
43
+ // Modifiers
44
+ // ----------------------------------------------------------------------
45
+ /** Creates a readonly optional property */
46
+ ReadonlyOptional(item) {
47
+ return { [exports.Modifier]: 'ReadonlyOptional', ...item };
48
+ }
49
+ /** Creates a readonly property */
50
+ Readonly(item) {
51
+ return { [exports.Modifier]: 'Readonly', ...item };
52
+ }
53
+ /** Creates a optional property */
54
+ Optional(item) {
55
+ return { [exports.Modifier]: 'Optional', ...item };
56
+ }
57
+ // ----------------------------------------------------------------------
58
+ // Types
59
+ // ----------------------------------------------------------------------
60
+ /** `Standard` Creates a any type */
61
+ Any(options = {}) {
62
+ return this.Create({ ...options, [exports.Kind]: 'Any' });
63
+ }
64
+ /** `Standard` Creates a array type */
65
+ Array(items, options = {}) {
66
+ return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items });
67
+ }
68
+ /** `Standard` Creates a boolean type */
69
+ Boolean(options = {}) {
70
+ return this.Create({ ...options, [exports.Kind]: 'Boolean', type: 'boolean' });
71
+ }
72
+ /** `Extended` Creates a tuple type from this constructors parameters */
73
+ ConstructorParameters(schema, options = {}) {
74
+ return this.Tuple([...schema.parameters], { ...options });
75
+ }
76
+ /** `Extended` Creates a constructor type */
77
+ Constructor(parameters, returns, options = {}) {
78
+ if (parameters[exports.Kind] === 'Tuple') {
79
+ const inner = parameters.items === undefined ? [] : parameters.items;
80
+ return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: inner, returns });
81
+ }
82
+ else if (globalThis.Array.isArray(parameters)) {
83
+ return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters, returns });
84
+ }
85
+ else {
86
+ throw new Error('TypeBuilder.Constructor: Invalid parameters');
87
+ }
88
+ }
89
+ /** `Extended` Creates a Date type */
90
+ Date(options = {}) {
91
+ return this.Create({ ...options, [exports.Kind]: 'Date', type: 'object', instanceOf: 'Date' });
92
+ }
93
+ /** `Standard` Creates a enum type */
94
+ Enum(item, options = {}) {
95
+ const values = Object.keys(item)
96
+ .filter((key) => isNaN(key))
97
+ .map((key) => item[key]);
98
+ const anyOf = values.map((value) => (typeof value === 'string' ? { [exports.Kind]: 'Literal', type: 'string', const: value } : { [exports.Kind]: 'Literal', type: 'number', const: value }));
99
+ return this.Create({ ...options, [exports.Kind]: 'Union', [exports.Hint]: 'Enum', anyOf });
100
+ }
101
+ /** `Extended` Creates a function type */
102
+ Function(parameters, returns, options = {}) {
103
+ if (parameters[exports.Kind] === 'Tuple') {
104
+ const inner = parameters.items === undefined ? [] : parameters.items;
105
+ return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters: inner, returns });
106
+ }
107
+ else if (globalThis.Array.isArray(parameters)) {
108
+ return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters, returns });
109
+ }
110
+ else {
111
+ throw new Error('TypeBuilder.Function: Invalid parameters');
112
+ }
113
+ }
114
+ /** `Extended` Creates a type from this constructors instance type */
115
+ InstanceType(schema, options = {}) {
116
+ return { ...options, ...this.Clone(schema.returns) };
117
+ }
118
+ /** `Standard` Creates a integer type */
119
+ Integer(options = {}) {
120
+ return this.Create({ ...options, [exports.Kind]: 'Integer', type: 'integer' });
121
+ }
122
+ /** `Standard` Creates a intersect type. */
123
+ Intersect(objects, options = {}) {
124
+ const isOptional = (schema) => (schema[exports.Modifier] && schema[exports.Modifier] === 'Optional') || schema[exports.Modifier] === 'ReadonlyOptional';
125
+ const [required, optional] = [new Set(), new Set()];
126
+ for (const object of objects) {
127
+ for (const [key, schema] of Object.entries(object.properties)) {
128
+ if (isOptional(schema))
129
+ optional.add(key);
130
+ }
131
+ }
132
+ for (const object of objects) {
133
+ for (const key of Object.keys(object.properties)) {
134
+ if (!optional.has(key))
135
+ required.add(key);
136
+ }
137
+ }
138
+ const properties = {};
139
+ for (const object of objects) {
140
+ for (const [key, schema] of Object.entries(object.properties)) {
141
+ properties[key] = properties[key] === undefined ? schema : { [exports.Kind]: 'Union', anyOf: [properties[key], { ...schema }] };
142
+ }
143
+ }
144
+ if (required.size > 0) {
145
+ return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties, required: [...required] });
146
+ }
147
+ else {
148
+ return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties });
149
+ }
150
+ }
151
+ /** `Standard` Creates a keyof type */
152
+ KeyOf(object, options = {}) {
153
+ const items = Object.keys(object.properties).map((key) => this.Create({ ...options, [exports.Kind]: 'Literal', type: 'string', const: key }));
154
+ return this.Create({ ...options, [exports.Kind]: 'Union', [exports.Hint]: 'KeyOf', anyOf: items });
155
+ }
156
+ /** `Standard` Creates a literal type. */
157
+ Literal(value, options = {}) {
158
+ return this.Create({ ...options, [exports.Kind]: 'Literal', const: value, type: typeof value });
159
+ }
160
+ /** `Standard` Creates a never type */
161
+ Never(options = {}) {
162
+ return this.Create({
163
+ ...options,
164
+ [exports.Kind]: 'Never',
165
+ allOf: [
166
+ { type: 'boolean', const: false },
167
+ { type: 'boolean', const: true },
168
+ ],
169
+ });
170
+ }
171
+ /** `Standard` Creates a null type */
172
+ Null(options = {}) {
173
+ return this.Create({ ...options, [exports.Kind]: 'Null', type: 'null' });
174
+ }
175
+ /** `Standard` Creates a number type */
176
+ Number(options = {}) {
177
+ return this.Create({ ...options, [exports.Kind]: 'Number', type: 'number' });
178
+ }
179
+ /** `Standard` Creates an object type */
180
+ Object(properties, options = {}) {
181
+ const property_names = Object.keys(properties);
182
+ const optional = property_names.filter((name) => {
183
+ const property = properties[name];
184
+ const modifier = property[exports.Modifier];
185
+ return modifier && (modifier === 'Optional' || modifier === 'ReadonlyOptional');
186
+ });
187
+ const required = property_names.filter((name) => !optional.includes(name));
188
+ if (required.length > 0) {
189
+ return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties, required });
190
+ }
191
+ else {
192
+ return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties });
193
+ }
194
+ }
195
+ /** `Standard` Creates a new object type whose keys are omitted from the given source type */
196
+ Omit(schema, keys, options = {}) {
197
+ const select = keys[exports.Kind] === 'Union' ? keys.anyOf.map((schema) => schema.const) : keys;
198
+ const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Omit' };
199
+ if (next.required) {
200
+ next.required = next.required.filter((key) => !select.includes(key));
201
+ if (next.required.length === 0)
202
+ delete next.required;
203
+ }
204
+ for (const key of Object.keys(next.properties)) {
205
+ if (select.includes(key))
206
+ delete next.properties[key];
207
+ }
208
+ return this.Create(next);
209
+ }
210
+ /** `Extended` Creates a tuple type from this functions parameters */
211
+ Parameters(schema, options = {}) {
212
+ return exports.Type.Tuple(schema.parameters, { ...options });
213
+ }
214
+ /** `Standard` Creates an object type whose properties are all optional */
215
+ Partial(schema, options = {}) {
216
+ const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Partial' };
217
+ delete next.required;
218
+ for (const key of Object.keys(next.properties)) {
219
+ const property = next.properties[key];
220
+ const modifer = property[exports.Modifier];
221
+ switch (modifer) {
222
+ case 'ReadonlyOptional':
223
+ property[exports.Modifier] = 'ReadonlyOptional';
224
+ break;
225
+ case 'Readonly':
226
+ property[exports.Modifier] = 'ReadonlyOptional';
227
+ break;
228
+ case 'Optional':
229
+ property[exports.Modifier] = 'Optional';
230
+ break;
231
+ default:
232
+ property[exports.Modifier] = 'Optional';
233
+ break;
234
+ }
235
+ }
236
+ return this.Create(next);
237
+ }
238
+ /** `Standard` Creates a new object type whose keys are picked from the given source type */
239
+ Pick(schema, keys, options = {}) {
240
+ const select = keys[exports.Kind] === 'Union' ? keys.anyOf.map((schema) => schema.const) : keys;
241
+ const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Pick' };
242
+ if (next.required) {
243
+ next.required = next.required.filter((key) => select.includes(key));
244
+ if (next.required.length === 0)
245
+ delete next.required;
246
+ }
247
+ for (const key of Object.keys(next.properties)) {
248
+ if (!select.includes(key))
249
+ delete next.properties[key];
250
+ }
251
+ return this.Create(next);
252
+ }
253
+ /** `Extended` Creates a Promise type */
254
+ Promise(item, options = {}) {
255
+ return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'object', instanceOf: 'Promise', item });
256
+ }
257
+ /** `Standard` Creates a record type */
258
+ Record(key, value, options = {}) {
259
+ // If string literal union return TObject with properties extracted from union.
260
+ if (key[exports.Kind] === 'Union') {
261
+ return this.Object(key.anyOf.reduce((acc, literal) => {
262
+ return { ...acc, [literal.const]: value };
263
+ }, {}), { ...options, [exports.Hint]: 'Record' });
264
+ }
265
+ // otherwise return TRecord with patternProperties
266
+ const pattern = ['Integer', 'Number'].includes(key[exports.Kind]) ? '^(0|[1-9][0-9]*)$' : key[exports.Kind] === 'String' && key.pattern ? key.pattern : '^.*$';
267
+ return this.Create({
268
+ ...options,
269
+ [exports.Kind]: 'Record',
270
+ type: 'object',
271
+ patternProperties: { [pattern]: value },
272
+ additionalProperties: false,
273
+ });
274
+ }
275
+ /** `Standard` Creates recursive type */
276
+ Recursive(callback, options = {}) {
277
+ if (options.$id === undefined)
278
+ options.$id = `T${TypeOrdinal++}`;
279
+ const self = callback({ [exports.Kind]: 'Self', $ref: `${options.$id}` });
280
+ self.$id = options.$id;
281
+ return this.Create({ ...options, ...self });
282
+ }
283
+ /** `Standard` Creates a reference type. The referenced type must contain a $id. */
284
+ Ref(schema, options = {}) {
285
+ if (schema.$id === undefined)
286
+ throw Error('TypeBuilder.Ref: Referenced schema must specify an $id');
287
+ return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: schema.$id });
288
+ }
289
+ /** `Standard` Creates a string type from a regular expression */
290
+ RegEx(regex, options = {}) {
291
+ return this.Create({ ...options, [exports.Kind]: 'String', type: 'string', pattern: regex.source });
292
+ }
293
+ /** `Standard` Creates an object type whose properties are all required */
294
+ Required(schema, options = {}) {
295
+ const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Required' };
296
+ next.required = Object.keys(next.properties);
297
+ for (const key of Object.keys(next.properties)) {
298
+ const property = next.properties[key];
299
+ const modifier = property[exports.Modifier];
300
+ switch (modifier) {
301
+ case 'ReadonlyOptional':
302
+ property[exports.Modifier] = 'Readonly';
303
+ break;
304
+ case 'Readonly':
305
+ property[exports.Modifier] = 'Readonly';
306
+ break;
307
+ case 'Optional':
308
+ delete property[exports.Modifier];
309
+ break;
310
+ default:
311
+ delete property[exports.Modifier];
312
+ break;
313
+ }
314
+ }
315
+ return this.Create(next);
316
+ }
317
+ /** `Extended` Creates a type from this functions return type */
318
+ ReturnType(schema, options = {}) {
319
+ return { ...options, ...this.Clone(schema.returns) };
320
+ }
321
+ /** Removes Kind and Modifier symbol property keys from this schema */
322
+ Strict(schema) {
323
+ return JSON.parse(JSON.stringify(schema));
324
+ }
325
+ /** `Standard` Creates a string type */
326
+ String(options = {}) {
327
+ return this.Create({ ...options, [exports.Kind]: 'String', type: 'string' });
328
+ }
329
+ /** `Standard` Creates a tuple type */
330
+ Tuple(items, options = {}) {
331
+ const additionalItems = false;
332
+ const minItems = items.length;
333
+ const maxItems = items.length;
334
+ const schema = (items.length > 0 ? { ...options, [exports.Kind]: 'Tuple', type: 'array', items, additionalItems, minItems, maxItems } : { ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
335
+ return this.Create(schema);
336
+ }
337
+ /** `Extended` Creates a undefined type */
338
+ Undefined(options = {}) {
339
+ return this.Create({ ...options, [exports.Kind]: 'Undefined', type: 'null', typeOf: 'Undefined' });
340
+ }
341
+ /** `Standard` Creates a union type */
342
+ Union(items, options = {}) {
343
+ return items.length === 0 ? exports.Type.Never({ ...options }) : this.Create({ ...options, [exports.Kind]: 'Union', anyOf: items });
344
+ }
345
+ /** `Extended` Creates a Uint8Array type */
346
+ Uint8Array(options = {}) {
347
+ return this.Create({ ...options, [exports.Kind]: 'Uint8Array', type: 'object', instanceOf: 'Uint8Array' });
348
+ }
349
+ /** `Standard` Creates an unknown type */
350
+ Unknown(options = {}) {
351
+ return this.Create({ ...options, [exports.Kind]: 'Unknown' });
352
+ }
353
+ /** `Standard` Creates a user defined schema that infers as type T */
354
+ Unsafe(options = {}) {
355
+ return this.Create({ ...options, [exports.Kind]: options[exports.Kind] || 'Unsafe' });
356
+ }
357
+ /** `Extended` Creates a void type */
358
+ Void(options = {}) {
359
+ return this.Create({ ...options, [exports.Kind]: 'Void', type: 'null', typeOf: 'Void' });
360
+ }
361
+ /** Use this function to return TSchema with static and params omitted */
362
+ Create(schema) {
363
+ return schema;
364
+ }
365
+ /** Clones the given value */
366
+ Clone(value) {
367
+ const isObject = (object) => typeof object === 'object' && object !== null && !Array.isArray(object);
368
+ const isArray = (object) => typeof object === 'object' && object !== null && Array.isArray(object);
369
+ if (isObject(value)) {
370
+ return Object.keys(value).reduce((acc, key) => ({
371
+ ...acc,
372
+ [key]: this.Clone(value[key]),
373
+ }), Object.getOwnPropertySymbols(value).reduce((acc, key) => ({
374
+ ...acc,
375
+ [key]: this.Clone(value[key]),
376
+ }), {}));
377
+ }
378
+ else if (isArray(value)) {
379
+ return value.map((item) => this.Clone(item));
380
+ }
381
+ else {
382
+ return value;
383
+ }
384
+ }
385
+ }
386
+ exports.TypeBuilder = TypeBuilder;
387
+ /** JSON Schema Type Builder with Static Type Resolution for TypeScript */
388
+ exports.Type = new TypeBuilder();