@sinclair/typebox 0.24.7 → 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.
package/value/create.js CHANGED
@@ -27,13 +27,11 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.CreateValue = void 0;
30
+ exports.ValueCreate = void 0;
31
31
  const Types = require("../typebox");
32
- var CreateValue;
33
- (function (CreateValue) {
34
- const referenceMap = new Map();
35
- let recursionDepth = 0;
36
- function Any(schema) {
32
+ var ValueCreate;
33
+ (function (ValueCreate) {
34
+ function Any(schema, references) {
37
35
  if (schema.default !== undefined) {
38
36
  return schema.default;
39
37
  }
@@ -41,20 +39,20 @@ var CreateValue;
41
39
  return {};
42
40
  }
43
41
  }
44
- function Array(schema) {
42
+ function Array(schema, references) {
45
43
  if (schema.default !== undefined) {
46
44
  return schema.default;
47
45
  }
48
46
  else if (schema.minItems !== undefined) {
49
47
  return globalThis.Array.from({ length: schema.minItems }).map((item) => {
50
- return CreateValue.Create(schema.items);
48
+ return ValueCreate.Create(schema.items, references);
51
49
  });
52
50
  }
53
51
  else {
54
52
  return [];
55
53
  }
56
54
  }
57
- function Boolean(schema) {
55
+ function Boolean(schema, references) {
58
56
  if (schema.default !== undefined) {
59
57
  return schema.default;
60
58
  }
@@ -62,12 +60,12 @@ var CreateValue;
62
60
  return false;
63
61
  }
64
62
  }
65
- function Constructor(schema) {
63
+ function Constructor(schema, references) {
66
64
  if (schema.default !== undefined) {
67
65
  return schema.default;
68
66
  }
69
67
  else {
70
- const value = CreateValue.Create(schema.returns);
68
+ const value = ValueCreate.Create(schema.returns, references);
71
69
  if (typeof value === 'object' && !globalThis.Array.isArray(value)) {
72
70
  return class {
73
71
  constructor() {
@@ -84,7 +82,7 @@ var CreateValue;
84
82
  }
85
83
  }
86
84
  }
87
- function Enum(schema) {
85
+ function Enum(schema, references) {
88
86
  if (schema.default !== undefined) {
89
87
  return schema.default;
90
88
  }
@@ -95,15 +93,15 @@ var CreateValue;
95
93
  return schema.anyOf[0].const;
96
94
  }
97
95
  }
98
- function Function(schema) {
96
+ function Function(schema, references) {
99
97
  if (schema.default !== undefined) {
100
98
  return schema.default;
101
99
  }
102
100
  else {
103
- return () => CreateValue.Create(schema.returns);
101
+ return () => ValueCreate.Create(schema.returns, references);
104
102
  }
105
103
  }
106
- function Integer(schema) {
104
+ function Integer(schema, references) {
107
105
  if (schema.default !== undefined) {
108
106
  return schema.default;
109
107
  }
@@ -114,24 +112,13 @@ var CreateValue;
114
112
  return 0;
115
113
  }
116
114
  }
117
- function Intersect(schema) {
118
- if (schema.default !== undefined) {
119
- return schema.default;
120
- }
121
- else {
122
- return (schema.default ||
123
- globalThis.Object.entries(schema.properties).reduce((acc, [key, schema]) => {
124
- return { ...acc, [key]: CreateValue.Create(schema) };
125
- }, {}));
126
- }
127
- }
128
- function Literal(schema) {
115
+ function Literal(schema, references) {
129
116
  return schema.const;
130
117
  }
131
- function Null(schema) {
118
+ function Null(schema, references) {
132
119
  return null;
133
120
  }
134
- function Number(schema) {
121
+ function Number(schema, references) {
135
122
  if (schema.default !== undefined) {
136
123
  return schema.default;
137
124
  }
@@ -142,35 +129,27 @@ var CreateValue;
142
129
  return 0;
143
130
  }
144
131
  }
145
- function Object(schema) {
132
+ function Object(schema, references) {
146
133
  if (schema.default !== undefined) {
147
134
  return schema.default;
148
135
  }
149
136
  else {
150
- // StackOverflow Prevention
151
- if (schema['$dynamicAnchor'] !== undefined) {
152
- for (const [key, value] of globalThis.Object.entries(schema.properties)) {
153
- if (value['$dynamicRef'] !== undefined && schema.required && schema.required.includes(key)) {
154
- throw Error(`Cannot create recursive object with immediate recursive property`);
155
- }
156
- }
157
- }
158
137
  const required = new Set(schema.required);
159
138
  return (schema.default ||
160
139
  globalThis.Object.entries(schema.properties).reduce((acc, [key, schema]) => {
161
- return required.has(key) ? { ...acc, [key]: CreateValue.Create(schema) } : { ...acc };
140
+ return required.has(key) ? { ...acc, [key]: ValueCreate.Create(schema, references) } : { ...acc };
162
141
  }, {}));
163
142
  }
164
143
  }
165
- function Promise(schema) {
144
+ function Promise(schema, references) {
166
145
  if (schema.default !== undefined) {
167
146
  return schema.default;
168
147
  }
169
148
  else {
170
- return globalThis.Promise.resolve(CreateValue.Create(schema.item));
149
+ return globalThis.Promise.resolve(ValueCreate.Create(schema.item, references));
171
150
  }
172
151
  }
173
- function Record(schema) {
152
+ function Record(schema, references) {
174
153
  const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
175
154
  if (schema.default !== undefined) {
176
155
  return schema.default;
@@ -178,14 +157,14 @@ var CreateValue;
178
157
  else if (!(keyPattern === '^.*$' || keyPattern === '^(0|[1-9][0-9]*)$')) {
179
158
  const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
180
159
  return propertyKeys.reduce((acc, key) => {
181
- return { ...acc, [key]: Create(valueSchema) };
160
+ return { ...acc, [key]: Create(valueSchema, references) };
182
161
  }, {});
183
162
  }
184
163
  else {
185
164
  return {};
186
165
  }
187
166
  }
188
- function Recursive(schema) {
167
+ function Recursive(schema, references) {
189
168
  if (schema.default !== undefined) {
190
169
  return schema.default;
191
170
  }
@@ -193,15 +172,29 @@ var CreateValue;
193
172
  throw new Error('Rec types require a default value');
194
173
  }
195
174
  }
196
- function Ref(schema) {
175
+ function Ref(schema, references) {
176
+ if (schema.default !== undefined) {
177
+ return schema.default;
178
+ }
179
+ else {
180
+ const reference = references.find((reference) => reference.$id === schema.$ref);
181
+ if (reference === undefined)
182
+ throw new Error(`ValueCreate.Ref: Cannot find schema with $id '${schema.$ref}'.`);
183
+ return Visit(reference, references);
184
+ }
185
+ }
186
+ function Self(schema, references) {
197
187
  if (schema.default !== undefined) {
198
188
  return schema.default;
199
189
  }
200
190
  else {
201
- throw new Error('Ref types require a default value');
191
+ const reference = references.find((reference) => reference.$id === schema.$ref);
192
+ if (reference === undefined)
193
+ throw new Error(`ValueCreate.Self: Cannot locate schema with $id '${schema.$ref}'`);
194
+ return Visit(reference, references);
202
195
  }
203
196
  }
204
- function String(schema) {
197
+ function String(schema, references) {
205
198
  if (schema.pattern !== undefined) {
206
199
  if (schema.default === undefined) {
207
200
  throw Error('String types with patterns must specify a default value');
@@ -219,7 +212,7 @@ var CreateValue;
219
212
  }
220
213
  }
221
214
  }
222
- function Tuple(schema) {
215
+ function Tuple(schema, references) {
223
216
  if (schema.default !== undefined) {
224
217
  return schema.default;
225
218
  }
@@ -227,13 +220,13 @@ var CreateValue;
227
220
  return [];
228
221
  }
229
222
  else {
230
- return globalThis.Array.from({ length: schema.minItems }).map((_, index) => CreateValue.Create(schema.items[index]));
223
+ return globalThis.Array.from({ length: schema.minItems }).map((_, index) => ValueCreate.Create(schema.items[index], references));
231
224
  }
232
225
  }
233
- function Undefined(schema) {
226
+ function Undefined(schema, references) {
234
227
  return undefined;
235
228
  }
236
- function Union(schema) {
229
+ function Union(schema, references) {
237
230
  if (schema.default !== undefined) {
238
231
  return schema.default;
239
232
  }
@@ -241,10 +234,10 @@ var CreateValue;
241
234
  throw Error('Cannot generate Union with empty set');
242
235
  }
243
236
  else {
244
- return CreateValue.Create(schema.anyOf[0]);
237
+ return ValueCreate.Create(schema.anyOf[0], references);
245
238
  }
246
239
  }
247
- function Uint8Array(schema) {
240
+ function Uint8Array(schema, references) {
248
241
  if (schema.default !== undefined) {
249
242
  return schema.default;
250
243
  }
@@ -255,7 +248,7 @@ var CreateValue;
255
248
  return new globalThis.Uint8Array(0);
256
249
  }
257
250
  }
258
- function Unknown(schema) {
251
+ function Unknown(schema, references) {
259
252
  if (schema.default !== undefined) {
260
253
  return schema.default;
261
254
  }
@@ -263,75 +256,67 @@ var CreateValue;
263
256
  return {};
264
257
  }
265
258
  }
266
- function Void(schema) {
259
+ function Void(schema, references) {
267
260
  return null;
268
261
  }
269
262
  /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
270
- function Visit(schema) {
271
- recursionDepth += 1;
272
- if (recursionDepth >= 1000)
273
- throw new Error('Cannot create value as schema contains a infinite expansion');
263
+ function Visit(schema, references) {
264
+ const anyReferences = schema.$id === undefined ? references : [schema, ...references];
274
265
  const anySchema = schema;
275
- if (anySchema.$id !== undefined)
276
- referenceMap.set(anySchema.$id, anySchema);
277
266
  switch (anySchema[Types.Kind]) {
278
267
  case 'Any':
279
- return Any(anySchema);
268
+ return Any(anySchema, anyReferences);
280
269
  case 'Array':
281
- return Array(anySchema);
270
+ return Array(anySchema, anyReferences);
282
271
  case 'Boolean':
283
- return Boolean(anySchema);
272
+ return Boolean(anySchema, anyReferences);
284
273
  case 'Constructor':
285
- return Constructor(anySchema);
274
+ return Constructor(anySchema, anyReferences);
286
275
  case 'Enum':
287
- return Enum(anySchema);
276
+ return Enum(anySchema, anyReferences);
288
277
  case 'Function':
289
- return Function(anySchema);
278
+ return Function(anySchema, anyReferences);
290
279
  case 'Integer':
291
- return Integer(anySchema);
292
- case 'Intersect':
293
- return Intersect(anySchema);
280
+ return Integer(anySchema, anyReferences);
294
281
  case 'Literal':
295
- return Literal(anySchema);
282
+ return Literal(anySchema, anyReferences);
296
283
  case 'Null':
297
- return Null(anySchema);
284
+ return Null(anySchema, anyReferences);
298
285
  case 'Number':
299
- return Number(anySchema);
286
+ return Number(anySchema, anyReferences);
300
287
  case 'Object':
301
- return Object(anySchema);
288
+ return Object(anySchema, anyReferences);
302
289
  case 'Promise':
303
- return Promise(anySchema);
290
+ return Promise(anySchema, anyReferences);
304
291
  case 'Record':
305
- return Record(anySchema);
292
+ return Record(anySchema, anyReferences);
306
293
  case 'Rec':
307
- return Recursive(anySchema);
294
+ return Recursive(anySchema, anyReferences);
308
295
  case 'Ref':
309
- return Ref(anySchema);
296
+ return Ref(anySchema, anyReferences);
297
+ case 'Self':
298
+ return Self(anySchema, anyReferences);
310
299
  case 'String':
311
- return String(anySchema);
300
+ return String(anySchema, anyReferences);
312
301
  case 'Tuple':
313
- return Tuple(anySchema);
302
+ return Tuple(anySchema, anyReferences);
314
303
  case 'Undefined':
315
- return Undefined(anySchema);
304
+ return Undefined(anySchema, anyReferences);
316
305
  case 'Union':
317
- return Union(anySchema);
306
+ return Union(anySchema, anyReferences);
318
307
  case 'Uint8Array':
319
- return Uint8Array(anySchema);
308
+ return Uint8Array(anySchema, anyReferences);
320
309
  case 'Unknown':
321
- return Unknown(anySchema);
310
+ return Unknown(anySchema, anyReferences);
322
311
  case 'Void':
323
- return Void(anySchema);
324
- case 'Self':
325
- return Visit(referenceMap.get(anySchema.$ref));
312
+ return Void(anySchema, anyReferences);
326
313
  default:
327
314
  throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
328
315
  }
329
316
  }
330
- CreateValue.Visit = Visit;
331
- /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
332
- function Create(schema) {
333
- recursionDepth = 0;
334
- return Visit(schema);
317
+ ValueCreate.Visit = Visit;
318
+ function Create(schema, references) {
319
+ return Visit(schema, references);
335
320
  }
336
- CreateValue.Create = Create;
337
- })(CreateValue = exports.CreateValue || (exports.CreateValue = {}));
321
+ ValueCreate.Create = Create;
322
+ })(ValueCreate = exports.ValueCreate || (exports.ValueCreate = {}));
@@ -0,0 +1,10 @@
1
+ import * as Types from '../typebox';
2
+ export interface ValueError {
3
+ schema: Types.TSchema;
4
+ path: string;
5
+ value: unknown;
6
+ message: string;
7
+ }
8
+ export declare namespace ValueErrors {
9
+ function Errors<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: any): IterableIterator<ValueError>;
10
+ }