@sinclair/typebox 0.26.2 → 0.26.3
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/compiler/compiler.js +17 -14
- package/errors/errors.js +19 -13
- package/package.json +1 -1
- package/readme.md +7 -7
- package/system/system.d.ts +2 -0
- package/system/system.js +8 -0
- package/value/check.js +19 -13
package/compiler/compiler.js
CHANGED
|
@@ -165,10 +165,10 @@ var TypeCompiler;
|
|
|
165
165
|
return typeof value === 'string';
|
|
166
166
|
}
|
|
167
167
|
// -------------------------------------------------------------------
|
|
168
|
-
//
|
|
168
|
+
// Polices
|
|
169
169
|
// -------------------------------------------------------------------
|
|
170
|
-
function
|
|
171
|
-
return
|
|
170
|
+
function IsExactOptionalProperty(value, key, expression) {
|
|
171
|
+
return index_2.TypeSystem.ExactOptionalPropertyTypes ? `('${key}' in ${value} ? ${expression} : true)` : `(${value}.${key} !== undefined ? ${expression} : true)`;
|
|
172
172
|
}
|
|
173
173
|
function IsObjectCheck(value) {
|
|
174
174
|
return !index_2.TypeSystem.AllowArrayObjects ? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))` : `(typeof ${value} === 'object' && ${value} !== null)`;
|
|
@@ -178,6 +178,9 @@ var TypeCompiler;
|
|
|
178
178
|
? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}) && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))`
|
|
179
179
|
: `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))`;
|
|
180
180
|
}
|
|
181
|
+
function IsNumberCheck(value) {
|
|
182
|
+
return !index_2.TypeSystem.AllowNaN ? `(typeof ${value} === 'number' && Number.isFinite(${value}))` : `typeof ${value} === 'number'`;
|
|
183
|
+
}
|
|
181
184
|
function IsVoidCheck(value) {
|
|
182
185
|
return index_2.TypeSystem.AllowVoidNull ? `(${value} === undefined || ${value} === null)` : `${value} === undefined`;
|
|
183
186
|
}
|
|
@@ -302,32 +305,32 @@ var TypeCompiler;
|
|
|
302
305
|
yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
|
|
303
306
|
if (IsNumber(schema.maxProperties))
|
|
304
307
|
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
305
|
-
const
|
|
306
|
-
for (const
|
|
307
|
-
const memberExpression = MemberExpression.Encode(value,
|
|
308
|
-
const property = schema.properties[
|
|
309
|
-
if (schema.required && schema.required.includes(
|
|
308
|
+
const knownKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
|
|
309
|
+
for (const knownKey of knownKeys) {
|
|
310
|
+
const memberExpression = MemberExpression.Encode(value, knownKey);
|
|
311
|
+
const property = schema.properties[knownKey];
|
|
312
|
+
if (schema.required && schema.required.includes(knownKey)) {
|
|
310
313
|
yield* Visit(property, references, memberExpression);
|
|
311
314
|
if (Types.ExtendsUndefined.Check(property))
|
|
312
|
-
yield `('${
|
|
315
|
+
yield `('${knownKey}' in ${value})`;
|
|
313
316
|
}
|
|
314
317
|
else {
|
|
315
318
|
const expression = CreateExpression(property, references, memberExpression);
|
|
316
|
-
yield
|
|
319
|
+
yield IsExactOptionalProperty(value, knownKey, expression);
|
|
317
320
|
}
|
|
318
321
|
}
|
|
319
322
|
if (schema.additionalProperties === false) {
|
|
320
|
-
if (schema.required && schema.required.length ===
|
|
321
|
-
yield `Object.getOwnPropertyNames(${value}).length === ${
|
|
323
|
+
if (schema.required && schema.required.length === knownKeys.length) {
|
|
324
|
+
yield `Object.getOwnPropertyNames(${value}).length === ${knownKeys.length}`;
|
|
322
325
|
}
|
|
323
326
|
else {
|
|
324
|
-
const keys = `[${
|
|
327
|
+
const keys = `[${knownKeys.map((key) => `'${key}'`).join(', ')}]`;
|
|
325
328
|
yield `Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key))`;
|
|
326
329
|
}
|
|
327
330
|
}
|
|
328
331
|
if (typeof schema.additionalProperties === 'object') {
|
|
329
332
|
const expression = CreateExpression(schema.additionalProperties, references, 'value[key]');
|
|
330
|
-
const keys = `[${
|
|
333
|
+
const keys = `[${knownKeys.map((key) => `'${key}'`).join(', ')}]`;
|
|
331
334
|
yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
|
|
332
335
|
}
|
|
333
336
|
}
|
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
|
|
370
|
+
if (IsExactOptionalProperty(value, knownKey)) {
|
|
365
371
|
yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]);
|
|
366
372
|
}
|
|
367
373
|
}
|
package/package.json
CHANGED
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
|
|
328
|
+
│ const T = Type.Composite([ │ type I = { │ const T = { │
|
|
329
329
|
│ Type.Object({ │ x: number │ type: 'object', │
|
|
330
|
-
│ x: Type.Number() │
|
|
331
|
-
│ }), │
|
|
332
|
-
│ Type.Object({ │
|
|
330
|
+
│ x: Type.Number() │ } & { │ required: ['x', 'y'], │
|
|
331
|
+
│ }), │ y: number │ properties: { │
|
|
332
|
+
│ Type.Object({ │ } │ x: { │
|
|
333
333
|
│ y: Type.Number() │ │ type: 'number' │
|
|
334
|
-
│ }) │
|
|
335
|
-
│ ]) │
|
|
336
|
-
│ │
|
|
334
|
+
│ }) │ type T = { │ }, │
|
|
335
|
+
│ ]) │ [K in keyof I]: I[K] │ y: { │
|
|
336
|
+
│ │ } │ type: 'number' │
|
|
337
337
|
│ │ │ } │
|
|
338
338
|
│ │ │ } │
|
|
339
339
|
│ │ │ } │
|
package/system/system.d.ts
CHANGED
|
@@ -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
|
|
264
|
+
if (IsExactOptionalProperty(value, knownKey) && !Visit(property, references, value[knownKey])) {
|
|
259
265
|
return false;
|
|
260
266
|
}
|
|
261
267
|
}
|