@sinclair/typebox 0.28.20 → 0.28.21

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.
Files changed (4) hide show
  1. package/compiler/compiler.js +30 -37
  2. package/license +22 -22
  3. package/package.json +50 -47
  4. package/readme.md +1563 -1563
@@ -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
  // -------------------------------------------------------------------
@@ -176,7 +166,7 @@ var TypeCompiler;
176
166
  // Polices
177
167
  // -------------------------------------------------------------------
178
168
  function IsExactOptionalProperty(value, key, expression) {
179
- return index_2.TypeSystem.ExactOptionalPropertyTypes ? `('${key}' in ${value} ? ${expression} : true)` : `(${MemberExpression.Encode(value, key)} !== undefined ? ${expression} : true)`;
169
+ return index_2.TypeSystem.ExactOptionalPropertyTypes ? `(${StringConstant(key)} in ${value} ? ${expression} : true)` : `(${MemberExpression(value, key)} !== undefined ? ${expression} : true)`;
180
170
  }
181
171
  function IsObjectCheck(value) {
182
172
  return !index_2.TypeSystem.AllowArrayObjects ? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))` : `(typeof ${value} === 'object' && ${value} !== null)`;
@@ -276,8 +266,11 @@ var TypeCompiler;
276
266
  if (typeof schema.const === 'number' || typeof schema.const === 'boolean') {
277
267
  yield `(${value} === ${schema.const})`;
278
268
  }
269
+ else if (typeof schema.const === 'string') {
270
+ yield `(${value} === ${StringConstant(schema.const)})`;
271
+ }
279
272
  else {
280
- yield `(${value} === '${schema.const}')`;
273
+ throw Error('Invalid Literal Value');
281
274
  }
282
275
  }
283
276
  function* Never(schema, references, value) {
@@ -312,12 +305,12 @@ var TypeCompiler;
312
305
  yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
313
306
  const knownKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
314
307
  for (const knownKey of knownKeys) {
315
- const memberExpression = MemberExpression.Encode(value, knownKey);
308
+ const memberExpression = MemberExpression(value, knownKey);
316
309
  const property = schema.properties[knownKey];
317
310
  if (schema.required && schema.required.includes(knownKey)) {
318
311
  yield* Visit(property, references, memberExpression);
319
312
  if (Types.ExtendsUndefined.Check(property) || IsAnyOrUnknown(property))
320
- yield `('${knownKey}' in ${value})`;
313
+ yield `(${StringConstant(knownKey)} in ${value})`;
321
314
  }
322
315
  else {
323
316
  const expression = CreateExpression(property, references, memberExpression);
@@ -329,13 +322,13 @@ var TypeCompiler;
329
322
  yield `Object.getOwnPropertyNames(${value}).length === ${knownKeys.length}`;
330
323
  }
331
324
  else {
332
- const keys = `[${knownKeys.map((key) => `'${key}'`).join(', ')}]`;
325
+ const keys = `[${knownKeys.map((key) => `${StringConstant(key)}`).join(', ')}]`;
333
326
  yield `Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key))`;
334
327
  }
335
328
  }
336
329
  if (typeof schema.additionalProperties === 'object') {
337
330
  const expression = CreateExpression(schema.additionalProperties, references, `${value}[key]`);
338
- const keys = `[${knownKeys.map((key) => `'${key}'`).join(', ')}]`;
331
+ const keys = `[${knownKeys.map((key) => `${StringConstant(key)}`).join(', ')}]`;
339
332
  yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
340
333
  }
341
334
  }
@@ -378,7 +371,7 @@ var TypeCompiler;
378
371
  yield `${local}.test(${value})`;
379
372
  }
380
373
  if (schema.format !== undefined) {
381
- yield `format('${schema.format}', ${value})`;
374
+ yield `format(${StringConstant(schema.format)}, ${value})`;
382
375
  }
383
376
  }
384
377
  function* Symbol(schema, references, value) {
@@ -426,7 +419,7 @@ var TypeCompiler;
426
419
  function* UserDefined(schema, references, value) {
427
420
  const schema_key = `schema_key_${state.customs.size}`;
428
421
  state.customs.set(schema_key, schema);
429
- yield `custom('${schema[Types.Kind]}', '${schema_key}', ${value})`;
422
+ yield `custom(${StringConstant(schema[Types.Kind])}, '${schema_key}', ${value})`;
430
423
  }
431
424
  function* Visit(schema, references, value, root = false) {
432
425
  const references_ = 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,47 +1,50 @@
1
- {
2
- "name": "@sinclair/typebox",
3
- "version": "0.28.20",
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": "./value/index.js",
20
- ".": "./typebox.js"
21
- },
22
- "repository": {
23
- "type": "git",
24
- "url": "https://github.com/sinclairzx81/typebox"
25
- },
26
- "scripts": {
27
- "clean": "hammer task clean",
28
- "format": "hammer task format",
29
- "start": "hammer task start",
30
- "test": "hammer task test",
31
- "benchmark": "hammer task benchmark",
32
- "build": "hammer task build",
33
- "publish": "hammer task publish"
34
- },
35
- "devDependencies": {
36
- "@sinclair/hammer": "^0.17.1",
37
- "@types/chai": "^4.3.3",
38
- "@types/mocha": "^9.1.1",
39
- "@types/node": "^18.11.9",
40
- "ajv": "^8.12.0",
41
- "ajv-formats": "^2.1.1",
42
- "chai": "^4.3.6",
43
- "mocha": "^9.2.2",
44
- "prettier": "^2.7.1",
45
- "typescript": "^5.1.3"
46
- }
47
- }
1
+ {
2
+ "name": "@sinclair/typebox",
3
+ "version": "0.28.21",
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": "./value/index.js",
20
+ ".": "./typebox.js"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/sinclairzx81/sinclair-typebox"
25
+ },
26
+ "scripts": {
27
+ "clean": "hammer task clean",
28
+ "format": "hammer task format",
29
+ "start": "hammer task start",
30
+ "test": "hammer task test",
31
+ "benchmark": "hammer task benchmark",
32
+ "build": "hammer task build",
33
+ "publish": "hammer task publish"
34
+ },
35
+ "devDependencies": {
36
+ "@sinclair/hammer": "^0.17.1",
37
+ "@types/chai": "^4.3.3",
38
+ "@types/mocha": "^9.1.1",
39
+ "@types/node": "^18.11.9",
40
+ "ajv": "^8.12.0",
41
+ "ajv-formats": "^2.1.1",
42
+ "chai": "^4.3.6",
43
+ "mocha": "^9.2.2",
44
+ "prettier": "^2.7.1",
45
+ "typescript": "^5.1.3"
46
+ },
47
+ "allowScripts": {
48
+ "esbuild@0.14.53": true
49
+ }
50
+ }