@sinclair/typebox 0.24.48 → 0.24.50

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.
@@ -190,6 +190,11 @@ var TypeCompiler;
190
190
  yield `(Object.keys(${value}).every(key => ${keys}.includes(key)))`;
191
191
  }
192
192
  }
193
+ if (index_2.TypeGuard.TSchema(schema.additionalProperties)) {
194
+ const expression = CreateExpression(schema.additionalProperties, 'value[key]');
195
+ const keys = `[${propertyKeys.map((key) => `'${key}'`).join(', ')}]`;
196
+ yield `(Object.keys(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
197
+ }
193
198
  for (const propertyKey of propertyKeys) {
194
199
  const memberExpression = Property.Check(propertyKey) ? `${value}.${propertyKey}` : `${value}['${propertyKey}']`;
195
200
  const propertySchema = schema.properties[propertyKey];
package/errors/errors.js CHANGED
@@ -205,6 +205,13 @@ var ValueErrors;
205
205
  yield { type: ValueErrorType.ObjectRequiredProperties, schema: schema.properties[requiredKey], path: `${path}/${requiredKey}`, value: undefined, message: `Expected required property` };
206
206
  }
207
207
  }
208
+ if (typeof schema.additionalProperties === 'object') {
209
+ for (const objectKey of globalThis.Object.keys(value)) {
210
+ if (propertyKeys.includes(objectKey))
211
+ continue;
212
+ yield* Visit(schema.additionalProperties, references, `${path}/${objectKey}`, value[objectKey]);
213
+ }
214
+ }
208
215
  for (const propertyKey of propertyKeys) {
209
216
  const propertySchema = schema.properties[propertyKey];
210
217
  if (schema.required && schema.required.includes(propertyKey)) {
package/guard/guard.js CHANGED
@@ -89,6 +89,9 @@ var TypeGuard;
89
89
  function IsOptionalFormat(value) {
90
90
  return value === undefined || (value !== undefined && IsString(value) && IsControlCharacterFree(value));
91
91
  }
92
+ function IsOptionalSchema(value) {
93
+ return value === undefined || TSchema(value);
94
+ }
92
95
  /** Returns true if the given schema is TAny */
93
96
  function TAny(schema) {
94
97
  return IsObject(schema) && schema[Types.Kind] === 'Any' && IsOptionalString(schema.$id);
@@ -194,7 +197,7 @@ var TypeGuard;
194
197
  schema.type === 'object' &&
195
198
  IsOptionalString(schema.$id) &&
196
199
  IsObject(schema.properties) &&
197
- IsOptionalBoolean(schema.additionalProperties) &&
200
+ (IsOptionalBoolean(schema.additionalProperties) || IsOptionalSchema(schema.additionalProperties)) &&
198
201
  IsOptionalNumber(schema.minProperties) &&
199
202
  IsOptionalNumber(schema.maxProperties))) {
200
203
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.24.48",
3
+ "version": "0.24.50",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -1062,29 +1062,29 @@ This benchmark measures compilation performance for varying types. You can revie
1062
1062
  ┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
1063
1063
  │ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
1064
1064
  ├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
1065
- │ Number │ 2000 │ ' 384 ms' │ ' 9 ms' │ ' 42.67 x' │
1066
- │ String │ 2000 │ ' 322 ms' │ ' 8 ms' │ ' 40.25 x' │
1067
- │ Boolean │ 2000 │ ' 310 ms' │ ' 6 ms' │ ' 51.67 x' │
1068
- │ Null │ 2000 │ ' 271 ms' │ ' 6 ms' │ ' 45.17 x' │
1069
- │ RegEx │ 2000 │ ' 491 ms' │ ' 12 ms' │ ' 40.92 x' │
1070
- │ ObjectA │ 2000 │ ' 2935 ms' │ ' 44 ms' │ ' 66.70 x' │
1071
- │ ObjectB │ 2000 │ ' 3076 ms' │ ' 30 ms' │ ' 102.53 x' │
1072
- │ Tuple │ 2000 │ ' 1274 ms' │ ' 22 ms' │ ' 57.91 x' │
1073
- │ Union │ 2000 │ ' 1281 ms' │ ' 22 ms' │ ' 58.23 x' │
1074
- │ Vector4 │ 2000 │ ' 1602 ms' │ ' 17 ms' │ ' 94.24 x' │
1075
- │ Matrix4 │ 2000 │ ' 959 ms' │ ' 10 ms' │ ' 95.90 x' │
1076
- │ Literal_String │ 2000 │ ' 337 ms' │ ' 6 ms' │ ' 56.17 x' │
1077
- │ Literal_Number │ 2000 │ ' 376 ms' │ ' 5 ms' │ ' 75.20 x' │
1078
- │ Literal_Boolean │ 2000 │ ' 370 ms' │ ' 5 ms' │ ' 74.00 x' │
1079
- │ Array_Number │ 2000 │ ' 733 ms' │ ' 9 ms' │ ' 81.44 x' │
1080
- │ Array_String │ 2000 │ ' 764 ms' │ ' 10 ms' │ ' 76.40 x' │
1081
- │ Array_Boolean │ 2000 │ ' 818 ms' │ ' 6 ms' │ ' 136.33 x' │
1082
- │ Array_ObjectA │ 2000 │ ' 3744 ms' │ ' 35 ms' │ ' 106.97 x' │
1083
- │ Array_ObjectB │ 2000 │ ' 3893 ms' │ ' 34 ms' │ ' 114.50 x' │
1084
- │ Array_Tuple │ 2000 │ ' 2229 ms' │ ' 16 ms' │ ' 139.31 x' │
1085
- │ Array_Union │ 2000 │ ' 1705 ms' │ ' 18 ms' │ ' 94.72 x' │
1086
- │ Array_Vector4 │ 2000 │ ' 2261 ms' │ ' 17 ms' │ ' 133.00 x' │
1087
- │ Array_Matrix4 │ 2000 │ ' 1574 ms' │ ' 14 ms' │ ' 112.43 x' │
1065
+ │ Number │ 2000 │ ' 428 ms' │ ' 12 ms' │ ' 35.67 x' │
1066
+ │ String │ 2000 │ ' 337 ms' │ ' 12 ms' │ ' 28.08 x' │
1067
+ │ Boolean │ 2000 │ ' 317 ms' │ ' 11 ms' │ ' 28.82 x' │
1068
+ │ Null │ 2000 │ ' 274 ms' │ ' 10 ms' │ ' 27.40 x' │
1069
+ │ RegEx │ 2000 │ ' 500 ms' │ ' 18 ms' │ ' 27.78 x' │
1070
+ │ ObjectA │ 2000 │ ' 2717 ms' │ ' 49 ms' │ ' 55.45 x' │
1071
+ │ ObjectB │ 2000 │ ' 2854 ms' │ ' 37 ms' │ ' 77.14 x' │
1072
+ │ Tuple │ 2000 │ ' 1224 ms' │ ' 21 ms' │ ' 58.29 x' │
1073
+ │ Union │ 2000 │ ' 1266 ms' │ ' 23 ms' │ ' 55.04 x' │
1074
+ │ Vector4 │ 2000 │ ' 1513 ms' │ ' 19 ms' │ ' 79.63 x' │
1075
+ │ Matrix4 │ 2000 │ ' 841 ms' │ ' 12 ms' │ ' 70.08 x' │
1076
+ │ Literal_String │ 2000 │ ' 327 ms' │ ' 8 ms' │ ' 40.88 x' │
1077
+ │ Literal_Number │ 2000 │ ' 358 ms' │ ' 6 ms' │ ' 59.67 x' │
1078
+ │ Literal_Boolean │ 2000 │ ' 355 ms' │ ' 5 ms' │ ' 71.00 x' │
1079
+ │ Array_Number │ 2000 │ ' 685 ms' │ ' 7 ms' │ ' 97.86 x' │
1080
+ │ Array_String │ 2000 │ ' 716 ms' │ ' 11 ms' │ ' 65.09 x' │
1081
+ │ Array_Boolean │ 2000 │ ' 732 ms' │ ' 6 ms' │ ' 122.00 x' │
1082
+ │ Array_ObjectA │ 2000 │ ' 3503 ms' │ ' 34 ms' │ ' 103.03 x' │
1083
+ │ Array_ObjectB │ 2000 │ ' 3626 ms' │ ' 38 ms' │ ' 95.42 x' │
1084
+ │ Array_Tuple │ 2000 │ ' 2095 ms' │ ' 21 ms' │ ' 99.76 x' │
1085
+ │ Array_Union │ 2000 │ ' 1577 ms' │ ' 22 ms' │ ' 71.68 x' │
1086
+ │ Array_Vector4 │ 2000 │ ' 2172 ms' │ ' 17 ms' │ ' 127.76 x' │
1087
+ │ Array_Matrix4 │ 2000 │ ' 1468 ms' │ ' 19 ms' │ ' 77.26 x' │
1088
1088
  └──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
1089
1089
  ```
1090
1090
 
@@ -1098,31 +1098,31 @@ This benchmark measures validation performance for varying types. You can review
1098
1098
  ┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
1099
1099
  │ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
1100
1100
  ├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
1101
- │ Number │ 1000000 │ ' 37 ms' │ ' 5 ms' │ ' 5 ms' │ ' 1.00 x' │
1102
- │ String │ 1000000 │ ' 31 ms' │ ' 24 ms' │ ' 13 ms' │ ' 1.85 x' │
1103
- │ Boolean │ 1000000 │ ' 25 ms' │ ' 25 ms' │ ' 11 ms' │ ' 2.27 x' │
1104
- │ Null │ 1000000 │ ' 32 ms' │ ' 26 ms' │ ' 10 ms' │ ' 2.60 x' │
1105
- │ RegEx │ 1000000 │ ' 170 ms' │ ' 48 ms' │ ' 35 ms' │ ' 1.37 x' │
1106
- │ ObjectA │ 1000000 │ ' 564 ms' │ ' 36 ms' │ ' 23 ms' │ ' 1.57 x' │
1107
- │ ObjectB │ 1000000 │ ' 1000 ms' │ ' 56 ms' │ ' 40 ms' │ ' 1.40 x' │
1108
- │ Tuple │ 1000000 │ ' 125 ms' │ ' 27 ms' │ ' 13 ms' │ ' 2.08 x' │
1109
- │ Union │ 1000000 │ ' 316 ms' │ ' 27 ms' │ ' 14 ms' │ ' 1.93 x' │
1110
- │ Recursive │ 1000000 │ ' 3308 ms' │ ' 415 ms' │ ' 183 ms' │ ' 2.27 x' │
1111
- │ Vector4 │ 1000000 │ ' 135 ms' │ ' 27 ms' │ ' 12 ms' │ ' 2.25 x' │
1112
- │ Matrix4 │ 1000000 │ ' 658 ms' │ ' 44 ms' │ ' 30 ms' │ ' 1.47 x' │
1113
- │ Literal_String │ 1000000 │ ' 48 ms' │ ' 26 ms' │ ' 10 ms' │ ' 2.60 x' │
1114
- │ Literal_Number │ 1000000 │ ' 49 ms' │ ' 31 ms' │ ' 9 ms' │ ' 3.44 x' │
1115
- │ Literal_Boolean │ 1000000 │ ' 47 ms' │ ' 28 ms' │ ' 10 ms' │ ' 2.80 x' │
1116
- │ Array_Number │ 1000000 │ ' 418 ms' │ ' 38 ms' │ ' 17 ms' │ ' 2.24 x' │
1117
- │ Array_String │ 1000000 │ ' 466 ms' │ ' 35 ms' │ ' 22 ms' │ ' 1.59 x' │
1118
- │ Array_Boolean │ 1000000 │ ' 403 ms' │ ' 41 ms' │ ' 24 ms' │ ' 1.71 x' │
1119
- │ Array_ObjectA │ 1000000 │ ' 13596 ms' │ ' 2861 ms' │ ' 1759 ms' │ ' 1.63 x' │
1120
- │ Array_ObjectB │ 1000000 │ ' 15767 ms' │ ' 2798 ms' │ ' 1991 ms' │ ' 1.41 x' │
1121
- │ Array_Tuple │ 1000000 │ ' 1666 ms' │ ' 103 ms' │ ' 73 ms' │ ' 1.41 x' │
1122
- │ Array_Union │ 1000000 │ ' 4738 ms' │ ' 229 ms' │ ' 87 ms' │ ' 2.63 x' │
1123
- │ Array_Recursive │ 1000000 │ ' 56371 ms' │ ' 7315 ms' │ ' 2813 ms' │ ' 2.60 x' │
1124
- │ Array_Vector4 │ 1000000 │ ' 2180 ms' │ ' 106 ms' │ ' 53 ms' │ ' 2.00 x' │
1125
- │ Array_Matrix4 │ 1000000 │ ' 11795 ms' │ ' 384 ms' │ ' 313 ms' │ ' 1.23 x' │
1101
+ │ Number │ 1000000 │ ' 24 ms' │ ' 9 ms' │ ' 6 ms' │ ' 1.50 x' │
1102
+ │ String │ 1000000 │ ' 23 ms' │ ' 19 ms' │ ' 12 ms' │ ' 1.58 x' │
1103
+ │ Boolean │ 1000000 │ ' 24 ms' │ ' 19 ms' │ ' 10 ms' │ ' 1.90 x' │
1104
+ │ Null │ 1000000 │ ' 23 ms' │ ' 18 ms' │ ' 9 ms' │ ' 2.00 x' │
1105
+ │ RegEx │ 1000000 │ ' 164 ms' │ ' 46 ms' │ ' 38 ms' │ ' 1.21 x' │
1106
+ │ ObjectA │ 1000000 │ ' 548 ms' │ ' 36 ms' │ ' 22 ms' │ ' 1.64 x' │
1107
+ │ ObjectB │ 1000000 │ ' 1118 ms' │ ' 51 ms' │ ' 38 ms' │ ' 1.34 x' │
1108
+ │ Tuple │ 1000000 │ ' 136 ms' │ ' 25 ms' │ ' 14 ms' │ ' 1.79 x' │
1109
+ │ Union │ 1000000 │ ' 338 ms' │ ' 27 ms' │ ' 16 ms' │ ' 1.69 x' │
1110
+ │ Recursive │ 1000000 │ ' 3251 ms' │ ' 416 ms' │ ' 98 ms' │ ' 4.24 x' │
1111
+ │ Vector4 │ 1000000 │ ' 146 ms' │ ' 23 ms' │ ' 12 ms' │ ' 1.92 x' │
1112
+ │ Matrix4 │ 1000000 │ ' 584 ms' │ ' 40 ms' │ ' 25 ms' │ ' 1.60 x' │
1113
+ │ Literal_String │ 1000000 │ ' 46 ms' │ ' 19 ms' │ ' 10 ms' │ ' 1.90 x' │
1114
+ │ Literal_Number │ 1000000 │ ' 46 ms' │ ' 20 ms' │ ' 10 ms' │ ' 2.00 x' │
1115
+ │ Literal_Boolean │ 1000000 │ ' 47 ms' │ ' 21 ms' │ ' 10 ms' │ ' 2.10 x' │
1116
+ │ Array_Number │ 1000000 │ ' 456 ms' │ ' 31 ms' │ ' 19 ms' │ ' 1.63 x' │
1117
+ │ Array_String │ 1000000 │ ' 489 ms' │ ' 40 ms' │ ' 25 ms' │ ' 1.60 x' │
1118
+ │ Array_Boolean │ 1000000 │ ' 458 ms' │ ' 35 ms' │ ' 27 ms' │ ' 1.30 x' │
1119
+ │ Array_ObjectA │ 1000000 │ ' 13559 ms' │ ' 2568 ms' │ ' 1564 ms' │ ' 1.64 x' │
1120
+ │ Array_ObjectB │ 1000000 │ ' 15863 ms' │ ' 2744 ms' │ ' 2060 ms' │ ' 1.33 x' │
1121
+ │ Array_Tuple │ 1000000 │ ' 1694 ms' │ ' 96 ms' │ ' 63 ms' │ ' 1.52 x' │
1122
+ │ Array_Union │ 1000000 │ ' 4736 ms' │ ' 229 ms' │ ' 86 ms' │ ' 2.66 x' │
1123
+ │ Array_Recursive │ 1000000 │ ' 53804 ms' │ ' 6744 ms' │ ' 1167 ms' │ ' 5.78 x' │
1124
+ │ Array_Vector4 │ 1000000 │ ' 2244 ms' │ ' 99 ms' │ ' 46 ms' │ ' 2.15 x' │
1125
+ │ Array_Matrix4 │ 1000000 │ ' 11966 ms' │ ' 378 ms' │ ' 229 ms' │ ' 1.65 x' │
1126
1126
  └──────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
1127
1127
  ```
1128
1128
 
@@ -1136,11 +1136,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
1136
1136
  ┌──────────────────────┬────────────┬────────────┬─────────────┐
1137
1137
  │ (index) │ Compiled │ Minified │ Compression │
1138
1138
  ├──────────────────────┼────────────┼────────────┼─────────────┤
1139
- │ typebox/compiler │ ' 49 kb' │ ' 24 kb' │ '2.00 x' │
1139
+ │ typebox/compiler │ ' 51 kb' │ ' 25 kb' │ '2.00 x' │
1140
1140
  │ typebox/conditional │ ' 42 kb' │ ' 17 kb' │ '2.46 x' │
1141
1141
  │ typebox/format │ ' 0 kb' │ ' 0 kb' │ '2.66 x' │
1142
- │ typebox/guard │ ' 21 kb' │ ' 10 kb' │ '2.07 x' │
1143
- │ typebox/value │ ' 72 kb' │ ' 33 kb' │ '2.16 x' │
1142
+ │ typebox/guard │ ' 21 kb' │ ' 10 kb' │ '2.08 x' │
1143
+ │ typebox/value │ ' 74 kb' │ ' 34 kb' │ '2.16 x' │
1144
1144
  │ typebox │ ' 11 kb' │ ' 6 kb' │ '1.91 x' │
1145
1145
  └──────────────────────┴────────────┴────────────┴─────────────┘
1146
1146
  ```
package/typebox.d.ts CHANGED
@@ -177,14 +177,16 @@ export interface TProperties {
177
177
  }
178
178
  export declare type ObjectProperties<T> = T extends TObject<infer U> ? U : never;
179
179
  export declare type ObjectPropertyKeys<T> = T extends TObject<infer U> ? keyof U : never;
180
+ export declare type TAdditionalProperties = undefined | TSchema | boolean;
180
181
  export interface ObjectOptions extends SchemaOptions {
181
- additionalProperties?: boolean;
182
+ additionalProperties?: TAdditionalProperties;
182
183
  minProperties?: number;
183
184
  maxProperties?: number;
184
185
  }
185
186
  export interface TObject<T extends TProperties = TProperties> extends TSchema, ObjectOptions {
186
187
  [Kind]: 'Object';
187
188
  static: PropertiesReduce<T, this['params']>;
189
+ additionalProperties?: TAdditionalProperties;
188
190
  type: 'object';
189
191
  properties: T;
190
192
  required?: string[];
package/value/cast.js CHANGED
@@ -230,6 +230,15 @@ var ValueCast;
230
230
  continue;
231
231
  result[key] = Visit(property, references, value[key]);
232
232
  }
233
+ // additional schema properties
234
+ if (typeof schema.additionalProperties === 'object') {
235
+ const propertyKeys = globalThis.Object.keys(schema.properties);
236
+ for (const objectKey of globalThis.Object.keys(value)) {
237
+ if (propertyKeys.includes(objectKey))
238
+ continue;
239
+ result[objectKey] = Visit(schema.additionalProperties, references, value[objectKey]);
240
+ }
241
+ }
233
242
  return result;
234
243
  }
235
244
  function Promise(schema, references, value) {
package/value/check.js CHANGED
@@ -145,6 +145,15 @@ var ValueCheck;
145
145
  }
146
146
  }
147
147
  }
148
+ if (typeof schema.additionalProperties === 'object') {
149
+ for (const objectKey of globalThis.Object.keys(value)) {
150
+ if (propertyKeys.includes(objectKey))
151
+ continue;
152
+ if (!Visit(schema.additionalProperties, references, value[objectKey])) {
153
+ return false;
154
+ }
155
+ }
156
+ }
148
157
  for (const propertyKey of propertyKeys) {
149
158
  const propertySchema = schema.properties[propertyKey];
150
159
  if (schema.required && schema.required.includes(propertyKey)) {