@sinclair/typebox 0.25.23 → 0.26.0-dev

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 (44) hide show
  1. package/compiler/compiler.d.ts +9 -4
  2. package/compiler/compiler.js +160 -119
  3. package/errors/errors.d.ts +56 -46
  4. package/errors/errors.js +234 -149
  5. package/package.json +1 -6
  6. package/readme.md +395 -396
  7. package/system/system.d.ts +9 -6
  8. package/system/system.js +17 -17
  9. package/typebox.d.ts +386 -162
  10. package/typebox.js +1710 -229
  11. package/value/cast.d.ts +2 -2
  12. package/value/cast.js +120 -192
  13. package/value/check.d.ts +1 -1
  14. package/value/check.js +162 -107
  15. package/value/convert.d.ts +13 -0
  16. package/value/convert.js +345 -0
  17. package/value/create.d.ts +6 -2
  18. package/value/create.js +149 -107
  19. package/{hash → value}/hash.js +39 -14
  20. package/value/index.d.ts +1 -0
  21. package/value/index.js +3 -1
  22. package/value/value.d.ts +2 -8
  23. package/value/value.js +20 -14
  24. package/conditional/conditional.d.ts +0 -17
  25. package/conditional/conditional.js +0 -91
  26. package/conditional/index.d.ts +0 -2
  27. package/conditional/index.js +0 -45
  28. package/conditional/structural.d.ts +0 -11
  29. package/conditional/structural.js +0 -685
  30. package/custom/custom.d.ts +0 -12
  31. package/custom/custom.js +0 -55
  32. package/custom/index.d.ts +0 -1
  33. package/custom/index.js +0 -44
  34. package/format/format.d.ts +0 -12
  35. package/format/format.js +0 -55
  36. package/format/index.d.ts +0 -1
  37. package/format/index.js +0 -44
  38. package/guard/guard.d.ts +0 -60
  39. package/guard/guard.js +0 -440
  40. package/guard/index.d.ts +0 -1
  41. package/guard/index.js +0 -44
  42. package/hash/index.d.ts +0 -1
  43. package/hash/index.js +0 -44
  44. /package/{hash → value}/hash.d.ts +0 -0
package/errors/errors.js CHANGED
@@ -30,9 +30,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.ValueErrors = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = void 0;
31
31
  const Types = require("../typebox");
32
32
  const index_1 = require("../system/index");
33
- const index_2 = require("../format/index");
34
- const index_3 = require("../custom/index");
35
- const index_4 = require("../hash/index");
33
+ const hash_1 = require("../value/hash");
36
34
  // -------------------------------------------------------------------
37
35
  // ValueErrorType
38
36
  // -------------------------------------------------------------------
@@ -42,51 +40,61 @@ var ValueErrorType;
42
40
  ValueErrorType[ValueErrorType["ArrayMinItems"] = 1] = "ArrayMinItems";
43
41
  ValueErrorType[ValueErrorType["ArrayMaxItems"] = 2] = "ArrayMaxItems";
44
42
  ValueErrorType[ValueErrorType["ArrayUniqueItems"] = 3] = "ArrayUniqueItems";
45
- ValueErrorType[ValueErrorType["Boolean"] = 4] = "Boolean";
46
- ValueErrorType[ValueErrorType["Date"] = 5] = "Date";
47
- ValueErrorType[ValueErrorType["DateExclusiveMinimumTimestamp"] = 6] = "DateExclusiveMinimumTimestamp";
48
- ValueErrorType[ValueErrorType["DateExclusiveMaximumTimestamp"] = 7] = "DateExclusiveMaximumTimestamp";
49
- ValueErrorType[ValueErrorType["DateMinimumTimestamp"] = 8] = "DateMinimumTimestamp";
50
- ValueErrorType[ValueErrorType["DateMaximumTimestamp"] = 9] = "DateMaximumTimestamp";
51
- ValueErrorType[ValueErrorType["Function"] = 10] = "Function";
52
- ValueErrorType[ValueErrorType["Integer"] = 11] = "Integer";
53
- ValueErrorType[ValueErrorType["IntegerMultipleOf"] = 12] = "IntegerMultipleOf";
54
- ValueErrorType[ValueErrorType["IntegerExclusiveMinimum"] = 13] = "IntegerExclusiveMinimum";
55
- ValueErrorType[ValueErrorType["IntegerExclusiveMaximum"] = 14] = "IntegerExclusiveMaximum";
56
- ValueErrorType[ValueErrorType["IntegerMinimum"] = 15] = "IntegerMinimum";
57
- ValueErrorType[ValueErrorType["IntegerMaximum"] = 16] = "IntegerMaximum";
58
- ValueErrorType[ValueErrorType["Literal"] = 17] = "Literal";
59
- ValueErrorType[ValueErrorType["Never"] = 18] = "Never";
60
- ValueErrorType[ValueErrorType["Null"] = 19] = "Null";
61
- ValueErrorType[ValueErrorType["Number"] = 20] = "Number";
62
- ValueErrorType[ValueErrorType["NumberMultipleOf"] = 21] = "NumberMultipleOf";
63
- ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 22] = "NumberExclusiveMinimum";
64
- ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 23] = "NumberExclusiveMaximum";
65
- ValueErrorType[ValueErrorType["NumberMinumum"] = 24] = "NumberMinumum";
66
- ValueErrorType[ValueErrorType["NumberMaximum"] = 25] = "NumberMaximum";
67
- ValueErrorType[ValueErrorType["Object"] = 26] = "Object";
68
- ValueErrorType[ValueErrorType["ObjectMinProperties"] = 27] = "ObjectMinProperties";
69
- ValueErrorType[ValueErrorType["ObjectMaxProperties"] = 28] = "ObjectMaxProperties";
70
- ValueErrorType[ValueErrorType["ObjectAdditionalProperties"] = 29] = "ObjectAdditionalProperties";
71
- ValueErrorType[ValueErrorType["ObjectRequiredProperties"] = 30] = "ObjectRequiredProperties";
72
- ValueErrorType[ValueErrorType["Promise"] = 31] = "Promise";
73
- ValueErrorType[ValueErrorType["RecordKeyNumeric"] = 32] = "RecordKeyNumeric";
74
- ValueErrorType[ValueErrorType["RecordKeyString"] = 33] = "RecordKeyString";
75
- ValueErrorType[ValueErrorType["String"] = 34] = "String";
76
- ValueErrorType[ValueErrorType["StringMinLength"] = 35] = "StringMinLength";
77
- ValueErrorType[ValueErrorType["StringMaxLength"] = 36] = "StringMaxLength";
78
- ValueErrorType[ValueErrorType["StringPattern"] = 37] = "StringPattern";
79
- ValueErrorType[ValueErrorType["StringFormatUnknown"] = 38] = "StringFormatUnknown";
80
- ValueErrorType[ValueErrorType["StringFormat"] = 39] = "StringFormat";
81
- ValueErrorType[ValueErrorType["TupleZeroLength"] = 40] = "TupleZeroLength";
82
- ValueErrorType[ValueErrorType["TupleLength"] = 41] = "TupleLength";
83
- ValueErrorType[ValueErrorType["Undefined"] = 42] = "Undefined";
84
- ValueErrorType[ValueErrorType["Union"] = 43] = "Union";
85
- ValueErrorType[ValueErrorType["Uint8Array"] = 44] = "Uint8Array";
86
- ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 45] = "Uint8ArrayMinByteLength";
87
- ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 46] = "Uint8ArrayMaxByteLength";
88
- ValueErrorType[ValueErrorType["Void"] = 47] = "Void";
89
- ValueErrorType[ValueErrorType["Custom"] = 48] = "Custom";
43
+ ValueErrorType[ValueErrorType["BigInt"] = 4] = "BigInt";
44
+ ValueErrorType[ValueErrorType["BigIntMultipleOf"] = 5] = "BigIntMultipleOf";
45
+ ValueErrorType[ValueErrorType["BigIntExclusiveMinimum"] = 6] = "BigIntExclusiveMinimum";
46
+ ValueErrorType[ValueErrorType["BigIntExclusiveMaximum"] = 7] = "BigIntExclusiveMaximum";
47
+ ValueErrorType[ValueErrorType["BigIntMinimum"] = 8] = "BigIntMinimum";
48
+ ValueErrorType[ValueErrorType["BigIntMaximum"] = 9] = "BigIntMaximum";
49
+ ValueErrorType[ValueErrorType["Boolean"] = 10] = "Boolean";
50
+ ValueErrorType[ValueErrorType["Date"] = 11] = "Date";
51
+ ValueErrorType[ValueErrorType["DateExclusiveMinimumTimestamp"] = 12] = "DateExclusiveMinimumTimestamp";
52
+ ValueErrorType[ValueErrorType["DateExclusiveMaximumTimestamp"] = 13] = "DateExclusiveMaximumTimestamp";
53
+ ValueErrorType[ValueErrorType["DateMinimumTimestamp"] = 14] = "DateMinimumTimestamp";
54
+ ValueErrorType[ValueErrorType["DateMaximumTimestamp"] = 15] = "DateMaximumTimestamp";
55
+ ValueErrorType[ValueErrorType["Function"] = 16] = "Function";
56
+ ValueErrorType[ValueErrorType["Integer"] = 17] = "Integer";
57
+ ValueErrorType[ValueErrorType["IntegerMultipleOf"] = 18] = "IntegerMultipleOf";
58
+ ValueErrorType[ValueErrorType["IntegerExclusiveMinimum"] = 19] = "IntegerExclusiveMinimum";
59
+ ValueErrorType[ValueErrorType["IntegerExclusiveMaximum"] = 20] = "IntegerExclusiveMaximum";
60
+ ValueErrorType[ValueErrorType["IntegerMinimum"] = 21] = "IntegerMinimum";
61
+ ValueErrorType[ValueErrorType["IntegerMaximum"] = 22] = "IntegerMaximum";
62
+ ValueErrorType[ValueErrorType["Intersect"] = 23] = "Intersect";
63
+ ValueErrorType[ValueErrorType["IntersectUnevaluatedProperties"] = 24] = "IntersectUnevaluatedProperties";
64
+ ValueErrorType[ValueErrorType["Literal"] = 25] = "Literal";
65
+ ValueErrorType[ValueErrorType["Never"] = 26] = "Never";
66
+ ValueErrorType[ValueErrorType["Not"] = 27] = "Not";
67
+ ValueErrorType[ValueErrorType["Null"] = 28] = "Null";
68
+ ValueErrorType[ValueErrorType["Number"] = 29] = "Number";
69
+ ValueErrorType[ValueErrorType["NumberMultipleOf"] = 30] = "NumberMultipleOf";
70
+ ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 31] = "NumberExclusiveMinimum";
71
+ ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 32] = "NumberExclusiveMaximum";
72
+ ValueErrorType[ValueErrorType["NumberMinumum"] = 33] = "NumberMinumum";
73
+ ValueErrorType[ValueErrorType["NumberMaximum"] = 34] = "NumberMaximum";
74
+ ValueErrorType[ValueErrorType["Object"] = 35] = "Object";
75
+ ValueErrorType[ValueErrorType["ObjectMinProperties"] = 36] = "ObjectMinProperties";
76
+ ValueErrorType[ValueErrorType["ObjectMaxProperties"] = 37] = "ObjectMaxProperties";
77
+ ValueErrorType[ValueErrorType["ObjectAdditionalProperties"] = 38] = "ObjectAdditionalProperties";
78
+ ValueErrorType[ValueErrorType["ObjectRequiredProperties"] = 39] = "ObjectRequiredProperties";
79
+ ValueErrorType[ValueErrorType["Promise"] = 40] = "Promise";
80
+ ValueErrorType[ValueErrorType["RecordKeyNumeric"] = 41] = "RecordKeyNumeric";
81
+ ValueErrorType[ValueErrorType["RecordKeyString"] = 42] = "RecordKeyString";
82
+ ValueErrorType[ValueErrorType["String"] = 43] = "String";
83
+ ValueErrorType[ValueErrorType["StringMinLength"] = 44] = "StringMinLength";
84
+ ValueErrorType[ValueErrorType["StringMaxLength"] = 45] = "StringMaxLength";
85
+ ValueErrorType[ValueErrorType["StringPattern"] = 46] = "StringPattern";
86
+ ValueErrorType[ValueErrorType["StringFormatUnknown"] = 47] = "StringFormatUnknown";
87
+ ValueErrorType[ValueErrorType["StringFormat"] = 48] = "StringFormat";
88
+ ValueErrorType[ValueErrorType["Symbol"] = 49] = "Symbol";
89
+ ValueErrorType[ValueErrorType["TupleZeroLength"] = 50] = "TupleZeroLength";
90
+ ValueErrorType[ValueErrorType["TupleLength"] = 51] = "TupleLength";
91
+ ValueErrorType[ValueErrorType["Undefined"] = 52] = "Undefined";
92
+ ValueErrorType[ValueErrorType["Union"] = 53] = "Union";
93
+ ValueErrorType[ValueErrorType["Uint8Array"] = 54] = "Uint8Array";
94
+ ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 55] = "Uint8ArrayMinByteLength";
95
+ ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 56] = "Uint8ArrayMaxByteLength";
96
+ ValueErrorType[ValueErrorType["Void"] = 57] = "Void";
97
+ ValueErrorType[ValueErrorType["Custom"] = 58] = "Custom";
90
98
  })(ValueErrorType = exports.ValueErrorType || (exports.ValueErrorType = {}));
91
99
  // -------------------------------------------------------------------
92
100
  // ValueErrors
@@ -101,11 +109,14 @@ exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError;
101
109
  /** Provides functionality to generate a sequence of errors against a TypeBox type. */
102
110
  var ValueErrors;
103
111
  (function (ValueErrors) {
112
+ function IsBigInt(value) {
113
+ return typeof value === 'bigint';
114
+ }
104
115
  function IsNumber(value) {
105
- return typeof value === 'number' && !isNaN(value);
116
+ return typeof value === 'number' && globalThis.isFinite(value);
106
117
  }
107
- function* Any(schema, references, path, value) { }
108
- function* Array(schema, references, path, value) {
118
+ function* Any(schema, path, value) { }
119
+ function* Array(schema, path, value) {
109
120
  if (!globalThis.Array.isArray(value)) {
110
121
  return yield { type: ValueErrorType.Array, schema, path, value, message: `Expected array` };
111
122
  }
@@ -117,7 +128,7 @@ var ValueErrors;
117
128
  }
118
129
  // prettier-ignore
119
130
  if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
120
- const hashed = index_4.ValueHash.Create(element);
131
+ const hashed = hash_1.ValueHash.Create(element);
121
132
  if (set.has(hashed)) {
122
133
  return false;
123
134
  }
@@ -128,22 +139,42 @@ var ValueErrors;
128
139
  yield { type: ValueErrorType.ArrayUniqueItems, schema, path, value, message: `Expected array elements to be unique` };
129
140
  }
130
141
  for (let i = 0; i < value.length; i++) {
131
- yield* Visit(schema.items, references, `${path}/${i}`, value[i]);
142
+ yield* Visit(schema.items, `${path}/${i}`, value[i]);
143
+ }
144
+ }
145
+ function* BigInt(schema, path, value) {
146
+ if (!(typeof value === 'bigint')) {
147
+ return yield { type: ValueErrorType.BigInt, schema, path, value, message: `Expected bigint` };
148
+ }
149
+ if (IsBigInt(schema.multipleOf) && !(value % schema.multipleOf === globalThis.BigInt(0))) {
150
+ yield { type: ValueErrorType.BigIntMultipleOf, schema, path, value, message: `Expected bigint to be a multiple of ${schema.multipleOf}` };
151
+ }
152
+ if (IsBigInt(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
153
+ yield { type: ValueErrorType.BigIntExclusiveMinimum, schema, path, value, message: `Expected bigint to be greater than ${schema.exclusiveMinimum}` };
154
+ }
155
+ if (IsBigInt(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
156
+ yield { type: ValueErrorType.BigIntExclusiveMaximum, schema, path, value, message: `Expected bigint to be less than ${schema.exclusiveMaximum}` };
157
+ }
158
+ if (IsBigInt(schema.minimum) && !(value >= schema.minimum)) {
159
+ yield { type: ValueErrorType.BigIntMinimum, schema, path, value, message: `Expected bigint to be greater or equal to ${schema.minimum}` };
160
+ }
161
+ if (IsBigInt(schema.maximum) && !(value <= schema.maximum)) {
162
+ yield { type: ValueErrorType.BigIntMaximum, schema, path, value, message: `Expected bigint to be less or equal to ${schema.maximum}` };
132
163
  }
133
164
  }
134
- function* Boolean(schema, references, path, value) {
165
+ function* Boolean(schema, path, value) {
135
166
  if (!(typeof value === 'boolean')) {
136
167
  return yield { type: ValueErrorType.Boolean, schema, path, value, message: `Expected boolean` };
137
168
  }
138
169
  }
139
- function* Constructor(schema, references, path, value) {
140
- yield* Visit(schema.returns, references, path, value.prototype);
170
+ function* Constructor(schema, path, value) {
171
+ yield* Visit(schema.returns, path, value.prototype);
141
172
  }
142
- function* Date(schema, references, path, value) {
173
+ function* Date(schema, path, value) {
143
174
  if (!(value instanceof globalThis.Date)) {
144
175
  return yield { type: ValueErrorType.Date, schema, path, value, message: `Expected Date object` };
145
176
  }
146
- if (isNaN(value.getTime())) {
177
+ if (!globalThis.isFinite(value.getTime())) {
147
178
  return yield { type: ValueErrorType.Date, schema, path, value, message: `Invalid Date` };
148
179
  }
149
180
  if (IsNumber(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
@@ -159,12 +190,12 @@ var ValueErrors;
159
190
  yield { type: ValueErrorType.DateMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less or equal to ${schema.maximum}` };
160
191
  }
161
192
  }
162
- function* Function(schema, references, path, value) {
193
+ function* Function(schema, path, value) {
163
194
  if (!(typeof value === 'function')) {
164
195
  return yield { type: ValueErrorType.Function, schema, path, value, message: `Expected function` };
165
196
  }
166
197
  }
167
- function* Integer(schema, references, path, value) {
198
+ function* Integer(schema, path, value) {
168
199
  if (!(typeof value === 'number' && globalThis.Number.isInteger(value))) {
169
200
  return yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` };
170
201
  }
@@ -184,28 +215,67 @@ var ValueErrors;
184
215
  yield { type: ValueErrorType.IntegerMaximum, schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` };
185
216
  }
186
217
  }
187
- function* Literal(schema, references, path, value) {
218
+ function* Intersect(schema, path, value) {
219
+ for (const subschema of schema.allOf) {
220
+ const next = Visit(subschema, path, value).next();
221
+ if (!next.done) {
222
+ yield next.value;
223
+ yield { type: ValueErrorType.Intersect, schema, path, value, message: `Expected all sub schemas to be valid` };
224
+ return;
225
+ }
226
+ }
227
+ if (schema.unevaluatedProperties === false) {
228
+ const schemaKeys = Types.KeyResolver.Resolve(schema);
229
+ const valueKeys = globalThis.Object.getOwnPropertyNames(value);
230
+ for (const valueKey of valueKeys) {
231
+ if (!schemaKeys.includes(valueKey)) {
232
+ yield { type: ValueErrorType.IntersectUnevaluatedProperties, schema, path: `${path}/${valueKey}`, value, message: `Unexpected property` };
233
+ }
234
+ }
235
+ }
236
+ if (typeof schema.unevaluatedProperties === 'object') {
237
+ const schemaKeys = Types.KeyResolver.Resolve(schema);
238
+ const valueKeys = globalThis.Object.getOwnPropertyNames(value);
239
+ for (const valueKey of valueKeys) {
240
+ if (!schemaKeys.includes(valueKey)) {
241
+ const next = Visit(schema.unevaluatedProperties, `${path}/${valueKey}`, value[valueKey]).next();
242
+ if (!next.done) {
243
+ yield next.value;
244
+ yield { type: ValueErrorType.IntersectUnevaluatedProperties, schema, path: `${path}/${valueKey}`, value, message: `Invalid additional property` };
245
+ return;
246
+ }
247
+ }
248
+ }
249
+ }
250
+ }
251
+ function* Literal(schema, path, value) {
188
252
  if (!(value === schema.const)) {
189
253
  const error = typeof schema.const === 'string' ? `'${schema.const}'` : schema.const;
190
254
  return yield { type: ValueErrorType.Literal, schema, path, value, message: `Expected ${error}` };
191
255
  }
192
256
  }
193
- function* Never(schema, references, path, value) {
257
+ function* Never(schema, path, value) {
194
258
  yield { type: ValueErrorType.Never, schema, path, value, message: `Value cannot be validated` };
195
259
  }
196
- function* Null(schema, references, path, value) {
260
+ function* Not(schema, path, value) {
261
+ if (Visit(schema.allOf[0].not, path, value).next().done === true) {
262
+ yield { type: ValueErrorType.Not, schema, path, value, message: `Value should not validate` };
263
+ }
264
+ yield* Visit(schema.allOf[1], path, value);
265
+ }
266
+ function* Null(schema, path, value) {
197
267
  if (!(value === null)) {
198
268
  return yield { type: ValueErrorType.Null, schema, path, value, message: `Expected null` };
199
269
  }
200
270
  }
201
- function* Number(schema, references, path, value) {
271
+ function* Number(schema, path, value) {
202
272
  if (index_1.TypeSystem.AllowNaN) {
203
273
  if (!(typeof value === 'number')) {
204
274
  return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
205
275
  }
206
276
  }
207
277
  else {
208
- if (!(typeof value === 'number' && !isNaN(value))) {
278
+ if (!(typeof value === 'number' && globalThis.Number.isFinite(value))) {
209
279
  return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
210
280
  }
211
281
  }
@@ -225,7 +295,7 @@ var ValueErrors;
225
295
  yield { type: ValueErrorType.NumberMinumum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
226
296
  }
227
297
  }
228
- function* Object(schema, references, path, value) {
298
+ function* Object(schema, path, value) {
229
299
  if (index_1.TypeSystem.AllowArrayObjects) {
230
300
  if (!(typeof value === 'object' && value !== null)) {
231
301
  return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
@@ -242,47 +312,49 @@ var ValueErrors;
242
312
  if (IsNumber(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
243
313
  yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` };
244
314
  }
245
- const propertyNames = globalThis.Object.getOwnPropertyNames(schema.properties);
246
- if (schema.additionalProperties === false) {
247
- for (const propertyName of globalThis.Object.getOwnPropertyNames(value)) {
248
- if (!propertyNames.includes(propertyName)) {
249
- yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: `${path}/${propertyName}`, value: value[propertyName], message: `Unexpected property` };
315
+ const requiredKeys = globalThis.Array.isArray(schema.required) ? schema.required : [];
316
+ const schemaKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
317
+ const valueKeys = globalThis.Object.getOwnPropertyNames(value);
318
+ for (const schemaKey of schemaKeys) {
319
+ const property = schema.properties[schemaKey];
320
+ if (schema.required && schema.required.includes(schemaKey)) {
321
+ yield* Visit(property, `${path}/${schemaKey}`, value[schemaKey]);
322
+ if (Types.ExtendsUndefined.Check(schema) && !(schemaKey in value)) {
323
+ yield { type: ValueErrorType.ObjectRequiredProperties, schema: property, path: `${path}/${schemaKey}`, value: undefined, message: `Expected required property` };
324
+ }
325
+ }
326
+ else {
327
+ if (schemaKey in value) {
328
+ yield* Visit(property, `${path}/${schemaKey}`, value[schemaKey]);
250
329
  }
251
330
  }
252
331
  }
253
- if (schema.required && schema.required.length > 0) {
254
- const propertyNames = globalThis.Object.getOwnPropertyNames(value);
255
- for (const requiredKey of schema.required) {
256
- if (propertyNames.includes(requiredKey))
257
- continue;
258
- yield { type: ValueErrorType.ObjectRequiredProperties, schema: schema.properties[requiredKey], path: `${path}/${requiredKey}`, value: undefined, message: `Expected required property` };
332
+ for (const requiredKey of requiredKeys) {
333
+ if (valueKeys.includes(requiredKey))
334
+ continue;
335
+ yield { type: ValueErrorType.ObjectRequiredProperties, schema: schema.properties[requiredKey], path: `${path}/${requiredKey}`, value: undefined, message: `Expected required property` };
336
+ }
337
+ if (schema.additionalProperties === false) {
338
+ for (const valueKey of valueKeys) {
339
+ if (!schemaKeys.includes(valueKey)) {
340
+ yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: `${path}/${valueKey}`, value: value[valueKey], message: `Unexpected property` };
341
+ }
259
342
  }
260
343
  }
261
344
  if (typeof schema.additionalProperties === 'object') {
262
- for (const propertyName of globalThis.Object.getOwnPropertyNames(value)) {
263
- if (propertyNames.includes(propertyName))
345
+ for (const valueKey of valueKeys) {
346
+ if (schemaKeys.includes(valueKey))
264
347
  continue;
265
- yield* Visit(schema.additionalProperties, references, `${path}/${propertyName}`, value[propertyName]);
266
- }
267
- }
268
- for (const propertyKey of propertyNames) {
269
- const propertySchema = schema.properties[propertyKey];
270
- if (schema.required && schema.required.includes(propertyKey)) {
271
- yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]);
272
- }
273
- else {
274
- if (value[propertyKey] !== undefined) {
275
- yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]);
276
- }
348
+ yield* Visit(schema.additionalProperties, `${path}/${valueKey}`, value[valueKey]);
277
349
  }
278
350
  }
279
351
  }
280
- function* Promise(schema, references, path, value) {
352
+ function* Promise(schema, path, value) {
281
353
  if (!(typeof value === 'object' && typeof value.then === 'function')) {
282
354
  yield { type: ValueErrorType.Promise, schema, path, value, message: `Expected Promise` };
283
355
  }
284
356
  }
285
- function* Record(schema, references, path, value) {
357
+ function* Record(schema, path, value) {
286
358
  if (index_1.TypeSystem.AllowArrayObjects) {
287
359
  if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date))) {
288
360
  return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
@@ -302,22 +374,16 @@ var ValueErrors;
302
374
  return yield { type, schema, path, value, message };
303
375
  }
304
376
  for (const [propKey, propValue] of globalThis.Object.entries(value)) {
305
- yield* Visit(valueSchema, references, `${path}/${propKey}`, propValue);
377
+ yield* Visit(valueSchema, `${path}/${propKey}`, propValue);
306
378
  }
307
379
  }
308
- function* Ref(schema, references, path, value) {
309
- const reference = references.find((reference) => reference.$id === schema.$ref);
310
- if (reference === undefined)
311
- throw new Error(`ValueErrors.Ref: Cannot find schema with $id '${schema.$ref}'.`);
312
- yield* Visit(reference, references, path, value);
380
+ function* Ref(schema, path, value) {
381
+ yield* Visit(Types.ReferenceRegistry.DerefOne(schema), path, value);
313
382
  }
314
- function* Self(schema, references, path, value) {
315
- const reference = references.find((reference) => reference.$id === schema.$ref);
316
- if (reference === undefined)
317
- throw new Error(`ValueErrors.Self: Cannot find schema with $id '${schema.$ref}'.`);
318
- yield* Visit(reference, references, path, value);
383
+ function* Self(schema, path, value) {
384
+ yield* Visit(Types.ReferenceRegistry.DerefOne(schema), path, value);
319
385
  }
320
- function* String(schema, references, path, value) {
386
+ function* String(schema, path, value) {
321
387
  if (!(typeof value === 'string')) {
322
388
  return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
323
389
  }
@@ -334,18 +400,23 @@ var ValueErrors;
334
400
  }
335
401
  }
336
402
  if (schema.format !== undefined) {
337
- if (!index_2.Format.Has(schema.format)) {
403
+ if (!Types.FormatRegistry.Has(schema.format)) {
338
404
  yield { type: ValueErrorType.StringFormatUnknown, schema, path, value, message: `Unknown string format '${schema.format}'` };
339
405
  }
340
406
  else {
341
- const format = index_2.Format.Get(schema.format);
407
+ const format = Types.FormatRegistry.Get(schema.format);
342
408
  if (!format(value)) {
343
409
  yield { type: ValueErrorType.StringFormat, schema, path, value, message: `Expected string to match format '${schema.format}'` };
344
410
  }
345
411
  }
346
412
  }
347
413
  }
348
- function* Tuple(schema, references, path, value) {
414
+ function* Symbol(schema, path, value) {
415
+ if (!(typeof value === 'symbol')) {
416
+ return yield { type: ValueErrorType.Symbol, schema, path, value, message: 'Expected symbol' };
417
+ }
418
+ }
419
+ function* Tuple(schema, path, value) {
349
420
  if (!globalThis.Array.isArray(value)) {
350
421
  return yield { type: ValueErrorType.Array, schema, path, value, message: 'Expected Array' };
351
422
  }
@@ -359,18 +430,18 @@ var ValueErrors;
359
430
  return;
360
431
  }
361
432
  for (let i = 0; i < schema.items.length; i++) {
362
- yield* Visit(schema.items[i], references, `${path}/${i}`, value[i]);
433
+ yield* Visit(schema.items[i], `${path}/${i}`, value[i]);
363
434
  }
364
435
  }
365
- function* Undefined(schema, references, path, value) {
436
+ function* Undefined(schema, path, value) {
366
437
  if (!(value === undefined)) {
367
438
  yield { type: ValueErrorType.Undefined, schema, path, value, message: `Expected undefined` };
368
439
  }
369
440
  }
370
- function* Union(schema, references, path, value) {
441
+ function* Union(schema, path, value) {
371
442
  const errors = [];
372
443
  for (const inner of schema.anyOf) {
373
- const variantErrors = [...Visit(inner, references, path, value)];
444
+ const variantErrors = [...Visit(inner, path, value)];
374
445
  if (variantErrors.length === 0)
375
446
  return;
376
447
  errors.push(...variantErrors);
@@ -382,7 +453,7 @@ var ValueErrors;
382
453
  yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' };
383
454
  }
384
455
  }
385
- function* Uint8Array(schema, references, path, value) {
456
+ function* Uint8Array(schema, path, value) {
386
457
  if (!(value instanceof globalThis.Uint8Array)) {
387
458
  return yield { type: ValueErrorType.Uint8Array, schema, path, value, message: `Expected Uint8Array` };
388
459
  }
@@ -393,76 +464,90 @@ var ValueErrors;
393
464
  yield { type: ValueErrorType.Uint8ArrayMinByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
394
465
  }
395
466
  }
396
- function* Unknown(schema, references, path, value) { }
397
- function* Void(schema, references, path, value) {
398
- if (!(value === null)) {
399
- return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected null` };
467
+ function* Unknown(schema, path, value) { }
468
+ function* Void(schema, path, value) {
469
+ if (index_1.TypeSystem.AllowVoidNull) {
470
+ if (!(value === undefined || value === null)) {
471
+ return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected null or undefined` };
472
+ }
473
+ }
474
+ else {
475
+ if (!(value === undefined)) {
476
+ return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected undefined` };
477
+ }
400
478
  }
401
479
  }
402
- function* UserDefined(schema, references, path, value) {
403
- const func = index_3.Custom.Get(schema[Types.Kind]);
404
- if (!func(schema, value)) {
480
+ function* UserDefined(schema, path, value) {
481
+ const check = Types.TypeRegistry.Get(schema[Types.Kind]);
482
+ if (!check(schema, value)) {
405
483
  return yield { type: ValueErrorType.Custom, schema, path, value, message: `Expected kind ${schema[Types.Kind]}` };
406
484
  }
407
485
  }
408
- function* Visit(schema, references, path, value) {
409
- const anyReferences = schema.$id === undefined ? references : [schema, ...references];
486
+ function* Visit(schema, path, value) {
410
487
  const anySchema = schema;
411
488
  switch (anySchema[Types.Kind]) {
412
489
  case 'Any':
413
- return yield* Any(anySchema, anyReferences, path, value);
490
+ return yield* Any(anySchema, path, value);
414
491
  case 'Array':
415
- return yield* Array(anySchema, anyReferences, path, value);
492
+ return yield* Array(anySchema, path, value);
493
+ case 'BigInt':
494
+ return yield* BigInt(anySchema, path, value);
416
495
  case 'Boolean':
417
- return yield* Boolean(anySchema, anyReferences, path, value);
496
+ return yield* Boolean(anySchema, path, value);
418
497
  case 'Constructor':
419
- return yield* Constructor(anySchema, anyReferences, path, value);
498
+ return yield* Constructor(anySchema, path, value);
420
499
  case 'Date':
421
- return yield* Date(anySchema, anyReferences, path, value);
500
+ return yield* Date(anySchema, path, value);
422
501
  case 'Function':
423
- return yield* Function(anySchema, anyReferences, path, value);
502
+ return yield* Function(anySchema, path, value);
424
503
  case 'Integer':
425
- return yield* Integer(anySchema, anyReferences, path, value);
504
+ return yield* Integer(anySchema, path, value);
505
+ case 'Intersect':
506
+ return yield* Intersect(anySchema, path, value);
426
507
  case 'Literal':
427
- return yield* Literal(anySchema, anyReferences, path, value);
508
+ return yield* Literal(anySchema, path, value);
428
509
  case 'Never':
429
- return yield* Never(anySchema, anyReferences, path, value);
510
+ return yield* Never(anySchema, path, value);
511
+ case 'Not':
512
+ return yield* Not(anySchema, path, value);
430
513
  case 'Null':
431
- return yield* Null(anySchema, anyReferences, path, value);
514
+ return yield* Null(anySchema, path, value);
432
515
  case 'Number':
433
- return yield* Number(anySchema, anyReferences, path, value);
516
+ return yield* Number(anySchema, path, value);
434
517
  case 'Object':
435
- return yield* Object(anySchema, anyReferences, path, value);
518
+ return yield* Object(anySchema, path, value);
436
519
  case 'Promise':
437
- return yield* Promise(anySchema, anyReferences, path, value);
520
+ return yield* Promise(anySchema, path, value);
438
521
  case 'Record':
439
- return yield* Record(anySchema, anyReferences, path, value);
522
+ return yield* Record(anySchema, path, value);
440
523
  case 'Ref':
441
- return yield* Ref(anySchema, anyReferences, path, value);
524
+ return yield* Ref(anySchema, path, value);
442
525
  case 'Self':
443
- return yield* Self(anySchema, anyReferences, path, value);
526
+ return yield* Self(anySchema, path, value);
444
527
  case 'String':
445
- return yield* String(anySchema, anyReferences, path, value);
528
+ return yield* String(anySchema, path, value);
529
+ case 'Symbol':
530
+ return yield* Symbol(anySchema, path, value);
446
531
  case 'Tuple':
447
- return yield* Tuple(anySchema, anyReferences, path, value);
532
+ return yield* Tuple(anySchema, path, value);
448
533
  case 'Undefined':
449
- return yield* Undefined(anySchema, anyReferences, path, value);
534
+ return yield* Undefined(anySchema, path, value);
450
535
  case 'Union':
451
- return yield* Union(anySchema, anyReferences, path, value);
536
+ return yield* Union(anySchema, path, value);
452
537
  case 'Uint8Array':
453
- return yield* Uint8Array(anySchema, anyReferences, path, value);
538
+ return yield* Uint8Array(anySchema, path, value);
454
539
  case 'Unknown':
455
- return yield* Unknown(anySchema, anyReferences, path, value);
540
+ return yield* Unknown(anySchema, path, value);
456
541
  case 'Void':
457
- return yield* Void(anySchema, anyReferences, path, value);
542
+ return yield* Void(anySchema, path, value);
458
543
  default:
459
- if (!index_3.Custom.Has(anySchema[Types.Kind]))
544
+ if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
460
545
  throw new ValueErrorsUnknownTypeError(schema);
461
- return yield* UserDefined(anySchema, anyReferences, path, value);
546
+ return yield* UserDefined(anySchema, path, value);
462
547
  }
463
548
  }
464
- function* Errors(schema, references, value) {
465
- yield* Visit(schema, references, '', value);
549
+ function* Errors(schema, value) {
550
+ yield* Visit(schema, '', value);
466
551
  }
467
552
  ValueErrors.Errors = Errors;
468
553
  })(ValueErrors = exports.ValueErrors || (exports.ValueErrors = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.25.23",
3
+ "version": "0.26.0-dev",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
@@ -14,12 +14,7 @@
14
14
  "types": "./typebox.d.ts",
15
15
  "exports": {
16
16
  "./compiler": "./compiler/index.js",
17
- "./conditional": "./conditional/index.js",
18
- "./custom": "./custom/index.js",
19
17
  "./errors": "./errors/index.js",
20
- "./format": "./format/index.js",
21
- "./guard": "./guard/index.js",
22
- "./hash": "./hash/index.js",
23
18
  "./system": "./system/index.js",
24
19
  "./value": "./value/index.js",
25
20
  ".": "./typebox.js"