@sinclair/typebox 0.31.28 → 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.js +27 -47
- package/license +22 -22
- package/package.json +67 -64
- package/readme.md +1734 -1734
package/compiler/compiler.js
CHANGED
|
@@ -96,35 +96,6 @@ var Character;
|
|
|
96
96
|
Character.IsNumeric = IsNumeric;
|
|
97
97
|
})(Character || (Character = {}));
|
|
98
98
|
// -------------------------------------------------------------------
|
|
99
|
-
// MemberExpression
|
|
100
|
-
// -------------------------------------------------------------------
|
|
101
|
-
var MemberExpression;
|
|
102
|
-
(function (MemberExpression) {
|
|
103
|
-
function IsFirstCharacterNumeric(value) {
|
|
104
|
-
if (value.length === 0)
|
|
105
|
-
return false;
|
|
106
|
-
return Character.IsNumeric(value.charCodeAt(0));
|
|
107
|
-
}
|
|
108
|
-
function IsAccessor(value) {
|
|
109
|
-
if (IsFirstCharacterNumeric(value))
|
|
110
|
-
return false;
|
|
111
|
-
for (let i = 0; i < value.length; i++) {
|
|
112
|
-
const code = value.charCodeAt(i);
|
|
113
|
-
const check = Character.IsAlpha(code) || Character.IsNumeric(code) || Character.DollarSign(code) || Character.IsUnderscore(code);
|
|
114
|
-
if (!check)
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
return true;
|
|
118
|
-
}
|
|
119
|
-
function EscapeHyphen(key) {
|
|
120
|
-
return key.replace(/'/g, "\\'");
|
|
121
|
-
}
|
|
122
|
-
function Encode(object, key) {
|
|
123
|
-
return IsAccessor(key) ? `${object}.${key}` : `${object}['${EscapeHyphen(key)}']`;
|
|
124
|
-
}
|
|
125
|
-
MemberExpression.Encode = Encode;
|
|
126
|
-
})(MemberExpression || (MemberExpression = {}));
|
|
127
|
-
// -------------------------------------------------------------------
|
|
128
99
|
// Identifier
|
|
129
100
|
// -------------------------------------------------------------------
|
|
130
101
|
var Identifier;
|
|
@@ -144,16 +115,22 @@ var Identifier;
|
|
|
144
115
|
}
|
|
145
116
|
Identifier.Encode = Encode;
|
|
146
117
|
})(Identifier || (Identifier = {}));
|
|
147
|
-
//
|
|
148
|
-
//
|
|
149
|
-
//
|
|
150
|
-
|
|
151
|
-
(
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
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
|
+
}
|
|
157
134
|
// -------------------------------------------------------------------
|
|
158
135
|
// Errors
|
|
159
136
|
// -------------------------------------------------------------------
|
|
@@ -177,7 +154,7 @@ exports.TypeCompilerTypeGuardError = TypeCompilerTypeGuardError;
|
|
|
177
154
|
var Policy;
|
|
178
155
|
(function (Policy) {
|
|
179
156
|
function IsExactOptionalProperty(value, key, expression) {
|
|
180
|
-
return index_1.TypeSystemPolicy.ExactOptionalPropertyTypes ? `(
|
|
157
|
+
return index_1.TypeSystemPolicy.ExactOptionalPropertyTypes ? `(${StringConstant(key)} in ${value} ? ${expression} : true)` : `(${MemberExpression(value, key)} !== undefined ? ${expression} : true)`;
|
|
181
158
|
}
|
|
182
159
|
Policy.IsExactOptionalProperty = IsExactOptionalProperty;
|
|
183
160
|
function IsObjectLike(value) {
|
|
@@ -312,8 +289,11 @@ var TypeCompiler;
|
|
|
312
289
|
if (typeof schema.const === 'number' || typeof schema.const === 'boolean') {
|
|
313
290
|
yield `(${value} === ${schema.const})`;
|
|
314
291
|
}
|
|
292
|
+
else if (typeof schema.const === 'string') {
|
|
293
|
+
yield `(${value} === ${StringConstant(schema.const)})`;
|
|
294
|
+
}
|
|
315
295
|
else {
|
|
316
|
-
|
|
296
|
+
throw Error('Invalid Literal Value');
|
|
317
297
|
}
|
|
318
298
|
}
|
|
319
299
|
function* TNever(schema, references, value) {
|
|
@@ -347,12 +327,12 @@ var TypeCompiler;
|
|
|
347
327
|
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
348
328
|
const knownKeys = Object.getOwnPropertyNames(schema.properties);
|
|
349
329
|
for (const knownKey of knownKeys) {
|
|
350
|
-
const memberExpression = MemberExpression
|
|
330
|
+
const memberExpression = MemberExpression(value, knownKey);
|
|
351
331
|
const property = schema.properties[knownKey];
|
|
352
332
|
if (schema.required && schema.required.includes(knownKey)) {
|
|
353
333
|
yield* Visit(property, references, memberExpression);
|
|
354
334
|
if (Types.ExtendsUndefined.Check(property) || IsAnyOrUnknown(property))
|
|
355
|
-
yield `(
|
|
335
|
+
yield `(${StringConstant(knownKey)} in ${value})`;
|
|
356
336
|
}
|
|
357
337
|
else {
|
|
358
338
|
const expression = CreateExpression(property, references, memberExpression);
|
|
@@ -364,13 +344,13 @@ var TypeCompiler;
|
|
|
364
344
|
yield `Object.getOwnPropertyNames(${value}).length === ${knownKeys.length}`;
|
|
365
345
|
}
|
|
366
346
|
else {
|
|
367
|
-
const keys = `[${knownKeys.map((key) =>
|
|
347
|
+
const keys = `[${knownKeys.map((key) => `${StringConstant(key)}`).join(', ')}]`;
|
|
368
348
|
yield `Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key))`;
|
|
369
349
|
}
|
|
370
350
|
}
|
|
371
351
|
if (typeof schema.additionalProperties === 'object') {
|
|
372
352
|
const expression = CreateExpression(schema.additionalProperties, references, `${value}[key]`);
|
|
373
|
-
const keys = `[${knownKeys.map((key) =>
|
|
353
|
+
const keys = `[${knownKeys.map((key) => `${StringConstant(key)}`).join(', ')}]`;
|
|
374
354
|
yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
|
|
375
355
|
}
|
|
376
356
|
}
|
|
@@ -409,7 +389,7 @@ var TypeCompiler;
|
|
|
409
389
|
yield `${variable}.test(${value})`;
|
|
410
390
|
}
|
|
411
391
|
if (schema.format !== undefined) {
|
|
412
|
-
yield `format(
|
|
392
|
+
yield `format(${StringConstant(schema.format)}, ${value})`;
|
|
413
393
|
}
|
|
414
394
|
}
|
|
415
395
|
function* TSymbol(schema, references, value) {
|
|
@@ -457,7 +437,7 @@ var TypeCompiler;
|
|
|
457
437
|
function* TKind(schema, references, value) {
|
|
458
438
|
const instance = state.instances.size;
|
|
459
439
|
state.instances.set(instance, schema);
|
|
460
|
-
yield `kind(
|
|
440
|
+
yield `kind(${StringConstant(schema[Types.Kind])}, ${instance}, ${value})`;
|
|
461
441
|
}
|
|
462
442
|
function* Visit(schema, references, value, useHoisting = true) {
|
|
463
443
|
const references_ = (0, guard_1.IsString)(schema.$id) ? [...references, schema] : references;
|
package/license
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
TypeBox: JSON Schema Type Builder with Static Type Resolution for TypeScript
|
|
2
|
-
|
|
3
|
-
The MIT License (MIT)
|
|
4
|
-
|
|
5
|
-
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
6
|
-
|
|
7
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
-
in the Software without restriction, including without limitation the rights
|
|
10
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
-
furnished to do so, subject to the following conditions:
|
|
13
|
-
|
|
14
|
-
The above copyright notice and this permission notice shall be included in
|
|
15
|
-
all copies or substantial portions of the Software.
|
|
16
|
-
|
|
17
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
1
|
+
TypeBox: JSON Schema Type Builder with Static Type Resolution for TypeScript
|
|
2
|
+
|
|
3
|
+
The MIT License (MIT)
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in
|
|
15
|
+
all copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
23
23
|
THE SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,64 +1,67 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@sinclair/typebox",
|
|
3
|
-
"version": "0.31.
|
|
4
|
-
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"typescript",
|
|
7
|
-
"json-schema",
|
|
8
|
-
"validate",
|
|
9
|
-
"typecheck"
|
|
10
|
-
],
|
|
11
|
-
"author": "sinclairzx81",
|
|
12
|
-
"license": "MIT",
|
|
13
|
-
"main": "./typebox.js",
|
|
14
|
-
"types": "./typebox.d.ts",
|
|
15
|
-
"exports": {
|
|
16
|
-
"./compiler": "./compiler/index.js",
|
|
17
|
-
"./errors": "./errors/index.js",
|
|
18
|
-
"./system": "./system/index.js",
|
|
19
|
-
"./value/cast": "./value/cast.js",
|
|
20
|
-
"./value/check": "./value/check.js",
|
|
21
|
-
"./value/clone": "./value/clone.js",
|
|
22
|
-
"./value/convert": "./value/convert.js",
|
|
23
|
-
"./value/create": "./value/create.js",
|
|
24
|
-
"./value/delta": "./value/delta.js",
|
|
25
|
-
"./value/deref": "./value/deref.js",
|
|
26
|
-
"./value/equal": "./value/equal.js",
|
|
27
|
-
"./value/guard": "./value/guard.js",
|
|
28
|
-
"./value/hash": "./value/hash.js",
|
|
29
|
-
"./value/mutate": "./value/mutate.js",
|
|
30
|
-
"./value/pointer": "./value/pointer.js",
|
|
31
|
-
"./value/transform": "./value/transform.js",
|
|
32
|
-
"./value": "./value/index.js",
|
|
33
|
-
".": "./typebox.js"
|
|
34
|
-
},
|
|
35
|
-
"repository": {
|
|
36
|
-
"type": "git",
|
|
37
|
-
"url": "https://github.com/sinclairzx81/typebox"
|
|
38
|
-
},
|
|
39
|
-
"scripts": {
|
|
40
|
-
"test:typescript": "hammer task test_typescript",
|
|
41
|
-
"test:static": "hammer task test_static",
|
|
42
|
-
"test:runtime": "hammer task test_runtime",
|
|
43
|
-
"test": "hammer task test",
|
|
44
|
-
"clean": "hammer task clean",
|
|
45
|
-
"format": "hammer task format",
|
|
46
|
-
"start": "hammer task start",
|
|
47
|
-
"benchmark:compression": "hammer task benchmark_compression",
|
|
48
|
-
"benchmark:measurement": "hammer task benchmark_measurement",
|
|
49
|
-
"benchmark": "hammer task benchmark",
|
|
50
|
-
"build": "hammer task build",
|
|
51
|
-
"publish": "hammer task publish",
|
|
52
|
-
"publish:dev": "hammer task publish_dev"
|
|
53
|
-
},
|
|
54
|
-
"devDependencies": {
|
|
55
|
-
"@sinclair/hammer": "^0.18.0",
|
|
56
|
-
"@types/mocha": "^9.1.1",
|
|
57
|
-
"@types/node": "^18.11.9",
|
|
58
|
-
"ajv": "^8.12.0",
|
|
59
|
-
"ajv-formats": "^2.1.1",
|
|
60
|
-
"mocha": "^9.2.2",
|
|
61
|
-
"prettier": "^2.7.1",
|
|
62
|
-
"typescript": "^5.2.2"
|
|
63
|
-
}
|
|
64
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@sinclair/typebox",
|
|
3
|
+
"version": "0.31.30",
|
|
4
|
+
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"json-schema",
|
|
8
|
+
"validate",
|
|
9
|
+
"typecheck"
|
|
10
|
+
],
|
|
11
|
+
"author": "sinclairzx81",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"main": "./typebox.js",
|
|
14
|
+
"types": "./typebox.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
"./compiler": "./compiler/index.js",
|
|
17
|
+
"./errors": "./errors/index.js",
|
|
18
|
+
"./system": "./system/index.js",
|
|
19
|
+
"./value/cast": "./value/cast.js",
|
|
20
|
+
"./value/check": "./value/check.js",
|
|
21
|
+
"./value/clone": "./value/clone.js",
|
|
22
|
+
"./value/convert": "./value/convert.js",
|
|
23
|
+
"./value/create": "./value/create.js",
|
|
24
|
+
"./value/delta": "./value/delta.js",
|
|
25
|
+
"./value/deref": "./value/deref.js",
|
|
26
|
+
"./value/equal": "./value/equal.js",
|
|
27
|
+
"./value/guard": "./value/guard.js",
|
|
28
|
+
"./value/hash": "./value/hash.js",
|
|
29
|
+
"./value/mutate": "./value/mutate.js",
|
|
30
|
+
"./value/pointer": "./value/pointer.js",
|
|
31
|
+
"./value/transform": "./value/transform.js",
|
|
32
|
+
"./value": "./value/index.js",
|
|
33
|
+
".": "./typebox.js"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/sinclairzx81/sinclair-typebox"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"test:typescript": "hammer task test_typescript",
|
|
41
|
+
"test:static": "hammer task test_static",
|
|
42
|
+
"test:runtime": "hammer task test_runtime",
|
|
43
|
+
"test": "hammer task test",
|
|
44
|
+
"clean": "hammer task clean",
|
|
45
|
+
"format": "hammer task format",
|
|
46
|
+
"start": "hammer task start",
|
|
47
|
+
"benchmark:compression": "hammer task benchmark_compression",
|
|
48
|
+
"benchmark:measurement": "hammer task benchmark_measurement",
|
|
49
|
+
"benchmark": "hammer task benchmark",
|
|
50
|
+
"build": "hammer task build",
|
|
51
|
+
"publish": "hammer task publish",
|
|
52
|
+
"publish:dev": "hammer task publish_dev"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@sinclair/hammer": "^0.18.0",
|
|
56
|
+
"@types/mocha": "^9.1.1",
|
|
57
|
+
"@types/node": "^18.11.9",
|
|
58
|
+
"ajv": "^8.12.0",
|
|
59
|
+
"ajv-formats": "^2.1.1",
|
|
60
|
+
"mocha": "^9.2.2",
|
|
61
|
+
"prettier": "^2.7.1",
|
|
62
|
+
"typescript": "^5.2.2"
|
|
63
|
+
},
|
|
64
|
+
"allowScripts": {
|
|
65
|
+
"esbuild@0.15.7": true
|
|
66
|
+
}
|
|
67
|
+
}
|