@sinclair/typebox 0.26.2 → 0.26.4

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.
@@ -14,9 +14,6 @@ export declare class TypeCheck<T extends Types.TSchema> {
14
14
  /** Returns true if the value matches the compiled type. */
15
15
  Check(value: unknown): value is Types.Static<T>;
16
16
  }
17
- export declare namespace MemberExpression {
18
- function Encode(object: string, key: string): string;
19
- }
20
17
  export declare class TypeCompilerUnknownTypeError extends Error {
21
18
  readonly schema: Types.TSchema;
22
19
  constructor(schema: Types.TSchema);
@@ -27,7 +27,7 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.TypeCompiler = exports.TypeCompilerTypeGuardError = exports.TypeCompilerDereferenceError = exports.TypeCompilerUnknownTypeError = exports.MemberExpression = exports.TypeCheck = void 0;
30
+ exports.TypeCompiler = exports.TypeCompilerTypeGuardError = exports.TypeCompilerDereferenceError = exports.TypeCompilerUnknownTypeError = exports.TypeCheck = void 0;
31
31
  const Types = require("../typebox");
32
32
  const index_1 = require("../errors/index");
33
33
  const index_2 = require("../system/index");
@@ -65,20 +65,49 @@ var Character;
65
65
  return code === 36;
66
66
  }
67
67
  Character.DollarSign = DollarSign;
68
- function Underscore(code) {
68
+ function IsUnderscore(code) {
69
69
  return code === 95;
70
70
  }
71
- Character.Underscore = Underscore;
72
- function Numeric(code) {
73
- return code >= 48 && code <= 57;
71
+ Character.IsUnderscore = IsUnderscore;
72
+ function IsAlpha(code) {
73
+ return (code >= 64 && code <= 90) || (code >= 97 && code <= 122);
74
74
  }
75
- Character.Numeric = Numeric;
76
- function Alpha(code) {
77
- return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
75
+ Character.IsAlpha = IsAlpha;
76
+ function IsNumeric(code) {
77
+ return code >= 48 && code <= 57;
78
78
  }
79
- Character.Alpha = Alpha;
79
+ Character.IsNumeric = IsNumeric;
80
80
  })(Character || (Character = {}));
81
81
  // -------------------------------------------------------------------
82
+ // MemberExpression
83
+ // -------------------------------------------------------------------
84
+ var MemberExpression;
85
+ (function (MemberExpression) {
86
+ function IsFirstCharacterNumeric(value) {
87
+ if (value.length === 0)
88
+ return false;
89
+ return Character.IsNumeric(value.charCodeAt(0));
90
+ }
91
+ function IsAccessor(value) {
92
+ if (IsFirstCharacterNumeric(value))
93
+ return false;
94
+ for (let i = 0; i < value.length; i++) {
95
+ const code = value.charCodeAt(i);
96
+ const check = Character.IsAlpha(code) || Character.IsNumeric(code) || Character.DollarSign(code) || Character.IsUnderscore(code);
97
+ if (!check)
98
+ return false;
99
+ }
100
+ return true;
101
+ }
102
+ function EscapeHyphen(key) {
103
+ return key.replace(/'/g, "\\'");
104
+ }
105
+ function Encode(object, key) {
106
+ return IsAccessor(key) ? `${object}.${key}` : `${object}['${EscapeHyphen(key)}']`;
107
+ }
108
+ MemberExpression.Encode = Encode;
109
+ })(MemberExpression || (MemberExpression = {}));
110
+ // -------------------------------------------------------------------
82
111
  // Identifier
83
112
  // -------------------------------------------------------------------
84
113
  var Identifier;
@@ -87,7 +116,7 @@ var Identifier;
87
116
  const buffer = [];
88
117
  for (let i = 0; i < $id.length; i++) {
89
118
  const code = $id.charCodeAt(i);
90
- if (Character.Numeric(code) || Character.Alpha(code)) {
119
+ if (Character.IsNumeric(code) || Character.IsAlpha(code)) {
91
120
  buffer.push($id.charAt(i));
92
121
  }
93
122
  else {
@@ -99,33 +128,6 @@ var Identifier;
99
128
  Identifier.Encode = Encode;
100
129
  })(Identifier || (Identifier = {}));
101
130
  // -------------------------------------------------------------------
102
- // MemberExpression
103
- // -------------------------------------------------------------------
104
- var MemberExpression;
105
- (function (MemberExpression) {
106
- function Check(propertyName) {
107
- if (propertyName.length === 0)
108
- return false;
109
- {
110
- const code = propertyName.charCodeAt(0);
111
- if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code))) {
112
- return false;
113
- }
114
- }
115
- for (let i = 1; i < propertyName.length; i++) {
116
- const code = propertyName.charCodeAt(i);
117
- if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code) || Character.Numeric(code))) {
118
- return false;
119
- }
120
- }
121
- return true;
122
- }
123
- function Encode(object, key) {
124
- return !Check(key) ? `${object}['${key}']` : `${object}.${key}`;
125
- }
126
- MemberExpression.Encode = Encode;
127
- })(MemberExpression = exports.MemberExpression || (exports.MemberExpression = {}));
128
- // -------------------------------------------------------------------
129
131
  // TypeCompiler
130
132
  // -------------------------------------------------------------------
131
133
  class TypeCompilerUnknownTypeError extends Error {
@@ -165,10 +167,10 @@ var TypeCompiler;
165
167
  return typeof value === 'string';
166
168
  }
167
169
  // -------------------------------------------------------------------
168
- // Overrides
170
+ // Polices
169
171
  // -------------------------------------------------------------------
170
- function IsNumberCheck(value) {
171
- return !index_2.TypeSystem.AllowNaN ? `(typeof ${value} === 'number' && Number.isFinite(${value}))` : `typeof ${value} === 'number'`;
172
+ function IsExactOptionalProperty(value, key, expression) {
173
+ return index_2.TypeSystem.ExactOptionalPropertyTypes ? `('${key}' in ${value} ? ${expression} : true)` : `(${value}.${key} !== undefined ? ${expression} : true)`;
172
174
  }
173
175
  function IsObjectCheck(value) {
174
176
  return !index_2.TypeSystem.AllowArrayObjects ? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))` : `(typeof ${value} === 'object' && ${value} !== null)`;
@@ -178,6 +180,9 @@ var TypeCompiler;
178
180
  ? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}) && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))`
179
181
  : `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))`;
180
182
  }
183
+ function IsNumberCheck(value) {
184
+ return !index_2.TypeSystem.AllowNaN ? `(typeof ${value} === 'number' && Number.isFinite(${value}))` : `typeof ${value} === 'number'`;
185
+ }
181
186
  function IsVoidCheck(value) {
182
187
  return index_2.TypeSystem.AllowVoidNull ? `(${value} === undefined || ${value} === null)` : `${value} === undefined`;
183
188
  }
@@ -302,32 +307,32 @@ var TypeCompiler;
302
307
  yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
303
308
  if (IsNumber(schema.maxProperties))
304
309
  yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
305
- const schemaKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
306
- for (const schemaKey of schemaKeys) {
307
- const memberExpression = MemberExpression.Encode(value, schemaKey);
308
- const property = schema.properties[schemaKey];
309
- if (schema.required && schema.required.includes(schemaKey)) {
310
+ const knownKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
311
+ for (const knownKey of knownKeys) {
312
+ const memberExpression = MemberExpression.Encode(value, knownKey);
313
+ const property = schema.properties[knownKey];
314
+ if (schema.required && schema.required.includes(knownKey)) {
310
315
  yield* Visit(property, references, memberExpression);
311
316
  if (Types.ExtendsUndefined.Check(property))
312
- yield `('${schemaKey}' in ${value})`;
317
+ yield `('${knownKey}' in ${value})`;
313
318
  }
314
319
  else {
315
320
  const expression = CreateExpression(property, references, memberExpression);
316
- yield `('${schemaKey}' in ${value} ? ${expression} : true)`;
321
+ yield IsExactOptionalProperty(value, knownKey, expression);
317
322
  }
318
323
  }
319
324
  if (schema.additionalProperties === false) {
320
- if (schema.required && schema.required.length === schemaKeys.length) {
321
- yield `Object.getOwnPropertyNames(${value}).length === ${schemaKeys.length}`;
325
+ if (schema.required && schema.required.length === knownKeys.length) {
326
+ yield `Object.getOwnPropertyNames(${value}).length === ${knownKeys.length}`;
322
327
  }
323
328
  else {
324
- const keys = `[${schemaKeys.map((key) => `'${key}'`).join(', ')}]`;
329
+ const keys = `[${knownKeys.map((key) => `'${key}'`).join(', ')}]`;
325
330
  yield `Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key))`;
326
331
  }
327
332
  }
328
333
  if (typeof schema.additionalProperties === 'object') {
329
334
  const expression = CreateExpression(schema.additionalProperties, references, 'value[key]');
330
- const keys = `[${schemaKeys.map((key) => `'${key}'`).join(', ')}]`;
335
+ const keys = `[${knownKeys.map((key) => `'${key}'`).join(', ')}]`;
331
336
  yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
332
337
  }
333
338
  }
package/errors/errors.js CHANGED
@@ -136,6 +136,24 @@ var ValueErrors;
136
136
  // ----------------------------------------------------------------------
137
137
  // Guards
138
138
  // ----------------------------------------------------------------------
139
+ function IsBigInt(value) {
140
+ return typeof value === 'bigint';
141
+ }
142
+ function IsInteger(value) {
143
+ return globalThis.Number.isInteger(value);
144
+ }
145
+ function IsString(value) {
146
+ return typeof value === 'string';
147
+ }
148
+ function IsDefined(value) {
149
+ return value !== undefined;
150
+ }
151
+ // ----------------------------------------------------------------------
152
+ // Policies
153
+ // ----------------------------------------------------------------------
154
+ function IsExactOptionalProperty(value, key) {
155
+ return index_1.TypeSystem.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined;
156
+ }
139
157
  function IsObject(value) {
140
158
  const result = typeof value === 'object' && value !== null;
141
159
  return index_1.TypeSystem.AllowArrayObjects ? result : result && !globalThis.Array.isArray(value);
@@ -143,26 +161,14 @@ var ValueErrors;
143
161
  function IsRecordObject(value) {
144
162
  return IsObject(value) && !(value instanceof globalThis.Date) && !(value instanceof globalThis.Uint8Array);
145
163
  }
146
- function IsBigInt(value) {
147
- return typeof value === 'bigint';
148
- }
149
164
  function IsNumber(value) {
150
165
  const result = typeof value === 'number';
151
166
  return index_1.TypeSystem.AllowNaN ? result : result && globalThis.Number.isFinite(value);
152
167
  }
153
- function IsInteger(value) {
154
- return globalThis.Number.isInteger(value);
155
- }
156
- function IsString(value) {
157
- return typeof value === 'string';
158
- }
159
168
  function IsVoid(value) {
160
169
  const result = value === undefined;
161
170
  return index_1.TypeSystem.AllowVoidNull ? result || value === null : result;
162
171
  }
163
- function IsDefined(value) {
164
- return value !== undefined;
165
- }
166
172
  // ----------------------------------------------------------------------
167
173
  // Types
168
174
  // ----------------------------------------------------------------------
@@ -361,7 +367,7 @@ var ValueErrors;
361
367
  }
362
368
  }
363
369
  else {
364
- if (knownKey in value) {
370
+ if (IsExactOptionalProperty(value, knownKey)) {
365
371
  yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]);
366
372
  }
367
373
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.26.2",
3
+ "version": "0.26.4",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -325,15 +325,15 @@ The following table lists the Standard TypeBox types. These types are fully comp
325
325
  │ │ │ } │
326
326
  │ │ │ │
327
327
  ├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
328
- │ const T = Type.Composite([ │ type T = { │ const T = { │
328
+ │ const T = Type.Composite([ │ type I = { │ const T = { │
329
329
  │ Type.Object({ │ x: number │ type: 'object', │
330
- │ x: Type.Number() │ y: number │ required: ['x', 'y'], │
331
- │ }), │ } │ properties: { │
332
- │ Type.Object({ │ │ x: { │
330
+ │ x: Type.Number() │ } & { │ required: ['x', 'y'], │
331
+ │ }), │ y: number │ properties: { │
332
+ │ Type.Object({ │ } │ x: { │
333
333
  │ y: Type.Number() │ │ type: 'number' │
334
- │ }) │ │ }, │
335
- │ ]) │ │ y: { │
336
- │ │ │ type: 'number' │
334
+ │ }) │ type T = { │ }, │
335
+ │ ]) │ [K in keyof I]: I[K] │ y: { │
336
+ │ │ } │ type: 'number' │
337
337
  │ │ │ } │
338
338
  │ │ │ } │
339
339
  │ │ │ } │
@@ -7,6 +7,8 @@ export declare class TypeSystemDuplicateFormat extends Error {
7
7
  }
8
8
  /** Creates user defined types and formats and provides overrides for value checking behaviours */
9
9
  export declare namespace TypeSystem {
10
+ /** Sets whether TypeBox should assert optional properties using the TypeScript `exactOptionalPropertyTypes` assertion policy. The default is `false` */
11
+ let ExactOptionalPropertyTypes: boolean;
10
12
  /** Sets whether arrays should be treated as a kind of objects. The default is `false` */
11
13
  let AllowArrayObjects: boolean;
12
14
  /** Sets whether `NaN` or `Infinity` should be treated as valid numeric values. The default is `false` */
package/system/system.js CHANGED
@@ -44,12 +44,20 @@ exports.TypeSystemDuplicateFormat = TypeSystemDuplicateFormat;
44
44
  /** Creates user defined types and formats and provides overrides for value checking behaviours */
45
45
  var TypeSystem;
46
46
  (function (TypeSystem) {
47
+ // ------------------------------------------------------------------------
48
+ // Assertion Policies
49
+ // ------------------------------------------------------------------------
50
+ /** Sets whether TypeBox should assert optional properties using the TypeScript `exactOptionalPropertyTypes` assertion policy. The default is `false` */
51
+ TypeSystem.ExactOptionalPropertyTypes = false;
47
52
  /** Sets whether arrays should be treated as a kind of objects. The default is `false` */
48
53
  TypeSystem.AllowArrayObjects = false;
49
54
  /** Sets whether `NaN` or `Infinity` should be treated as valid numeric values. The default is `false` */
50
55
  TypeSystem.AllowNaN = false;
51
56
  /** Sets whether `null` should validate for void types. The default is `false` */
52
57
  TypeSystem.AllowVoidNull = false;
58
+ // ------------------------------------------------------------------------
59
+ // String Formats and Types
60
+ // ------------------------------------------------------------------------
53
61
  /** Creates a new type */
54
62
  function Type(kind, check) {
55
63
  if (Types.TypeRegistry.Has(kind))
package/value/check.js CHANGED
@@ -53,6 +53,24 @@ var ValueCheck;
53
53
  // ----------------------------------------------------------------------
54
54
  // Guards
55
55
  // ----------------------------------------------------------------------
56
+ function IsBigInt(value) {
57
+ return typeof value === 'bigint';
58
+ }
59
+ function IsInteger(value) {
60
+ return globalThis.Number.isInteger(value);
61
+ }
62
+ function IsString(value) {
63
+ return typeof value === 'string';
64
+ }
65
+ function IsDefined(value) {
66
+ return value !== undefined;
67
+ }
68
+ // ----------------------------------------------------------------------
69
+ // Policies
70
+ // ----------------------------------------------------------------------
71
+ function IsExactOptionalProperty(value, key) {
72
+ return index_1.TypeSystem.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined;
73
+ }
56
74
  function IsObject(value) {
57
75
  const result = typeof value === 'object' && value !== null;
58
76
  return index_1.TypeSystem.AllowArrayObjects ? result : result && !globalThis.Array.isArray(value);
@@ -60,26 +78,14 @@ var ValueCheck;
60
78
  function IsRecordObject(value) {
61
79
  return IsObject(value) && !(value instanceof globalThis.Date) && !(value instanceof globalThis.Uint8Array);
62
80
  }
63
- function IsBigInt(value) {
64
- return typeof value === 'bigint';
65
- }
66
81
  function IsNumber(value) {
67
82
  const result = typeof value === 'number';
68
83
  return index_1.TypeSystem.AllowNaN ? result : result && globalThis.Number.isFinite(value);
69
84
  }
70
- function IsInteger(value) {
71
- return globalThis.Number.isInteger(value);
72
- }
73
- function IsString(value) {
74
- return typeof value === 'string';
75
- }
76
85
  function IsVoid(value) {
77
86
  const result = value === undefined;
78
87
  return index_1.TypeSystem.AllowVoidNull ? result || value === null : result;
79
88
  }
80
- function IsDefined(value) {
81
- return value !== undefined;
82
- }
83
89
  // ----------------------------------------------------------------------
84
90
  // Types
85
91
  // ----------------------------------------------------------------------
@@ -255,7 +261,7 @@ var ValueCheck;
255
261
  }
256
262
  }
257
263
  else {
258
- if (knownKey in value && !Visit(property, references, value[knownKey])) {
264
+ if (IsExactOptionalProperty(value, knownKey) && !Visit(property, references, value[knownKey])) {
259
265
  return false;
260
266
  }
261
267
  }