@sinclair/typebox 0.31.29 → 0.31.30
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.d.ts +0 -7
- package/compiler/compiler.js +28 -62
- package/package.json +1 -1
package/compiler/compiler.d.ts
CHANGED
|
@@ -19,13 +19,6 @@ export declare class TypeCheck<T extends Types.TSchema> {
|
|
|
19
19
|
/** Encodes a value or throws if error */
|
|
20
20
|
Encode(value: unknown): Types.StaticEncode<T>;
|
|
21
21
|
}
|
|
22
|
-
export declare namespace StringUtil {
|
|
23
|
-
/**
|
|
24
|
-
* Unquoted string values are embedded into single-quote string. This
|
|
25
|
-
* function ensures that embedded string cannot be escaped.
|
|
26
|
-
*/
|
|
27
|
-
function EscapeSingleQuote(value: string): string;
|
|
28
|
-
}
|
|
29
22
|
export declare class TypeCompilerUnknownTypeError extends Types.TypeBoxError {
|
|
30
23
|
readonly schema: Types.TSchema;
|
|
31
24
|
constructor(schema: Types.TSchema);
|
package/compiler/compiler.js
CHANGED
|
@@ -27,7 +27,7 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.TypeCompiler = exports.Policy = exports.TypeCompilerTypeGuardError = exports.TypeCompilerUnknownTypeError = exports.
|
|
30
|
+
exports.TypeCompiler = exports.Policy = exports.TypeCompilerTypeGuardError = exports.TypeCompilerUnknownTypeError = exports.TypeCheck = void 0;
|
|
31
31
|
const transform_1 = require("../value/transform");
|
|
32
32
|
const guard_1 = require("../value/guard");
|
|
33
33
|
const errors_1 = require("../errors/errors");
|
|
@@ -95,49 +95,6 @@ var Character;
|
|
|
95
95
|
}
|
|
96
96
|
Character.IsNumeric = IsNumeric;
|
|
97
97
|
})(Character || (Character = {}));
|
|
98
|
-
// ------------------------------------------------------------------
|
|
99
|
-
// StringUtil
|
|
100
|
-
// ------------------------------------------------------------------
|
|
101
|
-
var StringUtil;
|
|
102
|
-
(function (StringUtil) {
|
|
103
|
-
/**
|
|
104
|
-
* Unquoted string values are embedded into single-quote string. This
|
|
105
|
-
* function ensures that embedded string cannot be escaped.
|
|
106
|
-
*/
|
|
107
|
-
function EscapeSingleQuote(value) {
|
|
108
|
-
return JSON.stringify(value).slice(1, -1).replace(/'/g, "\\'");
|
|
109
|
-
}
|
|
110
|
-
StringUtil.EscapeSingleQuote = EscapeSingleQuote;
|
|
111
|
-
})(StringUtil || (exports.StringUtil = StringUtil = {}));
|
|
112
|
-
// -------------------------------------------------------------------
|
|
113
|
-
// MemberExpression
|
|
114
|
-
// -------------------------------------------------------------------
|
|
115
|
-
var MemberExpression;
|
|
116
|
-
(function (MemberExpression) {
|
|
117
|
-
function IsFirstCharacterNumeric(value) {
|
|
118
|
-
if (value.length === 0)
|
|
119
|
-
return false;
|
|
120
|
-
return Character.IsNumeric(value.charCodeAt(0));
|
|
121
|
-
}
|
|
122
|
-
function IsAccessor(value) {
|
|
123
|
-
if (IsFirstCharacterNumeric(value))
|
|
124
|
-
return false;
|
|
125
|
-
for (let i = 0; i < value.length; i++) {
|
|
126
|
-
const code = value.charCodeAt(i);
|
|
127
|
-
const check = Character.IsAlpha(code) || Character.IsNumeric(code) || Character.DollarSign(code) || Character.IsUnderscore(code);
|
|
128
|
-
if (!check)
|
|
129
|
-
return false;
|
|
130
|
-
}
|
|
131
|
-
return true;
|
|
132
|
-
}
|
|
133
|
-
function EscapeHyphen(key) {
|
|
134
|
-
return StringUtil.EscapeSingleQuote(key);
|
|
135
|
-
}
|
|
136
|
-
function Encode(object, key) {
|
|
137
|
-
return IsAccessor(key) ? `${object}.${key}` : `${object}['${EscapeHyphen(key)}']`;
|
|
138
|
-
}
|
|
139
|
-
MemberExpression.Encode = Encode;
|
|
140
|
-
})(MemberExpression || (MemberExpression = {}));
|
|
141
98
|
// -------------------------------------------------------------------
|
|
142
99
|
// Identifier
|
|
143
100
|
// -------------------------------------------------------------------
|
|
@@ -158,16 +115,22 @@ var Identifier;
|
|
|
158
115
|
}
|
|
159
116
|
Identifier.Encode = Encode;
|
|
160
117
|
})(Identifier || (Identifier = {}));
|
|
161
|
-
//
|
|
162
|
-
//
|
|
163
|
-
//
|
|
164
|
-
|
|
165
|
-
(
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
118
|
+
// ------------------------------------------------------------------
|
|
119
|
+
// StringConstant
|
|
120
|
+
// ------------------------------------------------------------------
|
|
121
|
+
function StringConstant(value) {
|
|
122
|
+
if (!(0, guard_1.IsString)(value))
|
|
123
|
+
throw Error('ConstantString: Not a String');
|
|
124
|
+
const canonical = JSON.stringify(value).slice(1, -1);
|
|
125
|
+
const escaped = canonical.replace(/'/g, "\\'");
|
|
126
|
+
return `'${escaped}'`;
|
|
127
|
+
}
|
|
128
|
+
// ------------------------------------------------------------------
|
|
129
|
+
// MemberExpression
|
|
130
|
+
// ------------------------------------------------------------------
|
|
131
|
+
function MemberExpression(value, key) {
|
|
132
|
+
return `${value}[${StringConstant(key)}]`;
|
|
133
|
+
}
|
|
171
134
|
// -------------------------------------------------------------------
|
|
172
135
|
// Errors
|
|
173
136
|
// -------------------------------------------------------------------
|
|
@@ -191,7 +154,7 @@ exports.TypeCompilerTypeGuardError = TypeCompilerTypeGuardError;
|
|
|
191
154
|
var Policy;
|
|
192
155
|
(function (Policy) {
|
|
193
156
|
function IsExactOptionalProperty(value, key, expression) {
|
|
194
|
-
return index_1.TypeSystemPolicy.ExactOptionalPropertyTypes ? `(
|
|
157
|
+
return index_1.TypeSystemPolicy.ExactOptionalPropertyTypes ? `(${StringConstant(key)} in ${value} ? ${expression} : true)` : `(${MemberExpression(value, key)} !== undefined ? ${expression} : true)`;
|
|
195
158
|
}
|
|
196
159
|
Policy.IsExactOptionalProperty = IsExactOptionalProperty;
|
|
197
160
|
function IsObjectLike(value) {
|
|
@@ -326,8 +289,11 @@ var TypeCompiler;
|
|
|
326
289
|
if (typeof schema.const === 'number' || typeof schema.const === 'boolean') {
|
|
327
290
|
yield `(${value} === ${schema.const})`;
|
|
328
291
|
}
|
|
292
|
+
else if (typeof schema.const === 'string') {
|
|
293
|
+
yield `(${value} === ${StringConstant(schema.const)})`;
|
|
294
|
+
}
|
|
329
295
|
else {
|
|
330
|
-
|
|
296
|
+
throw Error('Invalid Literal Value');
|
|
331
297
|
}
|
|
332
298
|
}
|
|
333
299
|
function* TNever(schema, references, value) {
|
|
@@ -361,12 +327,12 @@ var TypeCompiler;
|
|
|
361
327
|
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
362
328
|
const knownKeys = Object.getOwnPropertyNames(schema.properties);
|
|
363
329
|
for (const knownKey of knownKeys) {
|
|
364
|
-
const memberExpression = MemberExpression
|
|
330
|
+
const memberExpression = MemberExpression(value, knownKey);
|
|
365
331
|
const property = schema.properties[knownKey];
|
|
366
332
|
if (schema.required && schema.required.includes(knownKey)) {
|
|
367
333
|
yield* Visit(property, references, memberExpression);
|
|
368
334
|
if (Types.ExtendsUndefined.Check(property) || IsAnyOrUnknown(property))
|
|
369
|
-
yield `(
|
|
335
|
+
yield `(${StringConstant(knownKey)} in ${value})`;
|
|
370
336
|
}
|
|
371
337
|
else {
|
|
372
338
|
const expression = CreateExpression(property, references, memberExpression);
|
|
@@ -378,13 +344,13 @@ var TypeCompiler;
|
|
|
378
344
|
yield `Object.getOwnPropertyNames(${value}).length === ${knownKeys.length}`;
|
|
379
345
|
}
|
|
380
346
|
else {
|
|
381
|
-
const keys = `[${knownKeys.map((key) =>
|
|
347
|
+
const keys = `[${knownKeys.map((key) => `${StringConstant(key)}`).join(', ')}]`;
|
|
382
348
|
yield `Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key))`;
|
|
383
349
|
}
|
|
384
350
|
}
|
|
385
351
|
if (typeof schema.additionalProperties === 'object') {
|
|
386
352
|
const expression = CreateExpression(schema.additionalProperties, references, `${value}[key]`);
|
|
387
|
-
const keys = `[${knownKeys.map((key) =>
|
|
353
|
+
const keys = `[${knownKeys.map((key) => `${StringConstant(key)}`).join(', ')}]`;
|
|
388
354
|
yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
|
|
389
355
|
}
|
|
390
356
|
}
|
|
@@ -423,7 +389,7 @@ var TypeCompiler;
|
|
|
423
389
|
yield `${variable}.test(${value})`;
|
|
424
390
|
}
|
|
425
391
|
if (schema.format !== undefined) {
|
|
426
|
-
yield `format(
|
|
392
|
+
yield `format(${StringConstant(schema.format)}, ${value})`;
|
|
427
393
|
}
|
|
428
394
|
}
|
|
429
395
|
function* TSymbol(schema, references, value) {
|
|
@@ -471,7 +437,7 @@ var TypeCompiler;
|
|
|
471
437
|
function* TKind(schema, references, value) {
|
|
472
438
|
const instance = state.instances.size;
|
|
473
439
|
state.instances.set(instance, schema);
|
|
474
|
-
yield `kind(
|
|
440
|
+
yield `kind(${StringConstant(schema[Types.Kind])}, ${instance}, ${value})`;
|
|
475
441
|
}
|
|
476
442
|
function* Visit(schema, references, value, useHoisting = true) {
|
|
477
443
|
const references_ = (0, guard_1.IsString)(schema.$id) ? [...references, schema] : references;
|