@sinclair/typebox 0.27.10 → 0.27.11
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 +32 -39
- package/package.json +5 -2
package/compiler/compiler.js
CHANGED
|
@@ -79,35 +79,6 @@ var Character;
|
|
|
79
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
|
-
// -------------------------------------------------------------------
|
|
111
82
|
// Identifier
|
|
112
83
|
// -------------------------------------------------------------------
|
|
113
84
|
var Identifier;
|
|
@@ -127,6 +98,25 @@ var Identifier;
|
|
|
127
98
|
}
|
|
128
99
|
Identifier.Encode = Encode;
|
|
129
100
|
})(Identifier || (Identifier = {}));
|
|
101
|
+
// ------------------------------------------------------------------
|
|
102
|
+
// StringConstant
|
|
103
|
+
// ------------------------------------------------------------------
|
|
104
|
+
function IsString(value) {
|
|
105
|
+
return typeof value === 'string';
|
|
106
|
+
}
|
|
107
|
+
function StringConstant(value) {
|
|
108
|
+
if (!IsString(value))
|
|
109
|
+
throw Error('ConstantString: Not a String');
|
|
110
|
+
const canonical = JSON.stringify(value).slice(1, -1);
|
|
111
|
+
const escaped = canonical.replace(/'/g, "\\'");
|
|
112
|
+
return `'${escaped}'`;
|
|
113
|
+
}
|
|
114
|
+
// ------------------------------------------------------------------
|
|
115
|
+
// MemberExpression
|
|
116
|
+
// ------------------------------------------------------------------
|
|
117
|
+
function MemberExpression(value, key) {
|
|
118
|
+
return `${value}[${StringConstant(key)}]`;
|
|
119
|
+
}
|
|
130
120
|
// -------------------------------------------------------------------
|
|
131
121
|
// TypeCompiler
|
|
132
122
|
// -------------------------------------------------------------------
|
|
@@ -170,7 +160,7 @@ var TypeCompiler;
|
|
|
170
160
|
// Polices
|
|
171
161
|
// -------------------------------------------------------------------
|
|
172
162
|
function IsExactOptionalProperty(value, key, expression) {
|
|
173
|
-
return index_2.TypeSystem.ExactOptionalPropertyTypes ? `(
|
|
163
|
+
return index_2.TypeSystem.ExactOptionalPropertyTypes ? `(${StringConstant(key)} in ${value} ? ${expression} : true)` : `(${MemberExpression(value, key)} !== undefined ? ${expression} : true)`;
|
|
174
164
|
}
|
|
175
165
|
function IsObjectCheck(value) {
|
|
176
166
|
return !index_2.TypeSystem.AllowArrayObjects ? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))` : `(typeof ${value} === 'object' && ${value} !== null)`;
|
|
@@ -255,14 +245,14 @@ var TypeCompiler;
|
|
|
255
245
|
}
|
|
256
246
|
else if (schema.unevaluatedProperties === false) {
|
|
257
247
|
// prettier-ignore
|
|
258
|
-
const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) =>
|
|
248
|
+
const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) => `${StringConstant(key)}`).join(', ');
|
|
259
249
|
const expressions = schema.allOf.map((schema) => CreateExpression(schema, references, value));
|
|
260
250
|
const expression1 = `Object.getOwnPropertyNames(${value}).every(key => [${schemaKeys}].includes(key))`;
|
|
261
251
|
yield `${expressions.join(' && ')} && ${expression1}`;
|
|
262
252
|
}
|
|
263
253
|
else if (typeof schema.unevaluatedProperties === 'object') {
|
|
264
254
|
// prettier-ignore
|
|
265
|
-
const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) =>
|
|
255
|
+
const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) => `${StringConstant(key)}`).join(', ');
|
|
266
256
|
const expressions = schema.allOf.map((schema) => CreateExpression(schema, references, value));
|
|
267
257
|
const expression1 = CreateExpression(schema.unevaluatedProperties, references, 'value[key]');
|
|
268
258
|
const expression2 = `Object.getOwnPropertyNames(${value}).every(key => [${schemaKeys}].includes(key) || ${expression1})`;
|
|
@@ -273,8 +263,11 @@ var TypeCompiler;
|
|
|
273
263
|
if (typeof schema.const === 'number' || typeof schema.const === 'boolean') {
|
|
274
264
|
yield `${value} === ${schema.const}`;
|
|
275
265
|
}
|
|
266
|
+
else if (typeof schema.const === 'string') {
|
|
267
|
+
yield `(${value} === ${StringConstant(schema.const)})`;
|
|
268
|
+
}
|
|
276
269
|
else {
|
|
277
|
-
|
|
270
|
+
throw Error('Invalid Literal Value');
|
|
278
271
|
}
|
|
279
272
|
}
|
|
280
273
|
function* Never(schema, references, value) {
|
|
@@ -309,12 +302,12 @@ var TypeCompiler;
|
|
|
309
302
|
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
310
303
|
const knownKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
|
|
311
304
|
for (const knownKey of knownKeys) {
|
|
312
|
-
const memberExpression = MemberExpression
|
|
305
|
+
const memberExpression = MemberExpression(value, knownKey);
|
|
313
306
|
const property = schema.properties[knownKey];
|
|
314
307
|
if (schema.required && schema.required.includes(knownKey)) {
|
|
315
308
|
yield* Visit(property, references, memberExpression);
|
|
316
309
|
if (Types.ExtendsUndefined.Check(property))
|
|
317
|
-
yield `(
|
|
310
|
+
yield `(${StringConstant(knownKey)} in ${value})`;
|
|
318
311
|
}
|
|
319
312
|
else {
|
|
320
313
|
const expression = CreateExpression(property, references, memberExpression);
|
|
@@ -326,13 +319,13 @@ var TypeCompiler;
|
|
|
326
319
|
yield `Object.getOwnPropertyNames(${value}).length === ${knownKeys.length}`;
|
|
327
320
|
}
|
|
328
321
|
else {
|
|
329
|
-
const keys = `[${knownKeys.map((key) =>
|
|
322
|
+
const keys = `[${knownKeys.map((key) => `${StringConstant(key)}`).join(', ')}]`;
|
|
330
323
|
yield `Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key))`;
|
|
331
324
|
}
|
|
332
325
|
}
|
|
333
326
|
if (typeof schema.additionalProperties === 'object') {
|
|
334
327
|
const expression = CreateExpression(schema.additionalProperties, references, 'value[key]');
|
|
335
|
-
const keys = `[${knownKeys.map((key) =>
|
|
328
|
+
const keys = `[${knownKeys.map((key) => `${StringConstant(key)}`).join(', ')}]`;
|
|
336
329
|
yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
|
|
337
330
|
}
|
|
338
331
|
}
|
|
@@ -374,7 +367,7 @@ var TypeCompiler;
|
|
|
374
367
|
yield `${local}.test(${value})`;
|
|
375
368
|
}
|
|
376
369
|
if (schema.format !== undefined) {
|
|
377
|
-
yield `format(
|
|
370
|
+
yield `format(${StringConstant(schema.format)}, ${value})`;
|
|
378
371
|
}
|
|
379
372
|
}
|
|
380
373
|
function* Symbol(schema, references, value) {
|
|
@@ -422,7 +415,7 @@ var TypeCompiler;
|
|
|
422
415
|
function* UserDefined(schema, references, value) {
|
|
423
416
|
const schema_key = `schema_key_${state_remote_custom_types.size}`;
|
|
424
417
|
state_remote_custom_types.set(schema_key, schema);
|
|
425
|
-
yield `custom(
|
|
418
|
+
yield `custom(${StringConstant(schema[Types.Kind])}, '${schema_key}', ${value})`;
|
|
426
419
|
}
|
|
427
420
|
function* Visit(schema, references, value) {
|
|
428
421
|
const references_ = IsString(schema.$id) ? [...references, schema] : references;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sinclair/typebox",
|
|
3
|
-
"version": "0.27.
|
|
3
|
+
"version": "0.27.11",
|
|
4
4
|
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
},
|
|
22
22
|
"repository": {
|
|
23
23
|
"type": "git",
|
|
24
|
-
"url": "https://github.com/sinclairzx81/typebox
|
|
24
|
+
"url": "https://github.com/sinclairzx81/sinclair-typebox"
|
|
25
25
|
},
|
|
26
26
|
"scripts": {
|
|
27
27
|
"clean": "hammer task clean",
|
|
@@ -45,5 +45,8 @@
|
|
|
45
45
|
"mocha": "^9.2.2",
|
|
46
46
|
"prettier": "^2.7.1",
|
|
47
47
|
"typescript": "5.0.2"
|
|
48
|
+
},
|
|
49
|
+
"allowScripts": {
|
|
50
|
+
"esbuild@0.15.18": true
|
|
48
51
|
}
|
|
49
52
|
}
|