@sinclair/typebox 0.26.8 → 0.26.9

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 +36 -41
  2. package/license +22 -22
  3. package/package.json +50 -47
  4. package/readme.md +1328 -1328
@@ -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 ? `('${key}' in ${value} ? ${expression} : true)` : `(${MemberExpression.Encode(value, key)} !== undefined ? ${expression} : true)`;
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) => `'${key}'`).join(', ');
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) => `'${key}'`).join(', ');
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
- yield `${value} === '${schema.const}'`;
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.Encode(value, knownKey);
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 `('${knownKey}' in ${value})`;
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) => `'${key}'`).join(', ')}]`;
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) => `'${key}'`).join(', ')}]`;
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
  }
@@ -346,7 +339,7 @@ var TypeCompiler;
346
339
  if (IsNumber(schema.maxProperties))
347
340
  yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
348
341
  const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
349
- const local = PushLocal(`new RegExp(/${keyPattern}/)`);
342
+ const local = PushLocal(`${new RegExp(keyPattern)}`);
350
343
  yield `(Object.getOwnPropertyNames(${value}).every(key => ${local}.test(key)))`;
351
344
  const expression = CreateExpression(valueSchema, references, 'value');
352
345
  yield `Object.values(${value}).every(value => ${expression})`;
@@ -374,11 +367,11 @@ var TypeCompiler;
374
367
  if (IsNumber(schema.maxLength))
375
368
  yield `${value}.length <= ${schema.maxLength}`;
376
369
  if (schema.pattern !== undefined) {
377
- const local = PushLocal(`${new RegExp(schema.pattern)};`);
370
+ const local = PushLocal(`${new RegExp(schema.pattern)}`);
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) {
@@ -388,6 +381,8 @@ var TypeCompiler;
388
381
  yield `(Array.isArray(${value}))`;
389
382
  if (schema.items === undefined)
390
383
  return yield `${value}.length === 0`;
384
+ if (!IsNumber(schema.maxItems))
385
+ throw Error('MaxItems: Not a Number');
391
386
  yield `(${value}.length === ${schema.maxItems})`;
392
387
  for (let i = 0; i < schema.items.length; i++) {
393
388
  const expression = CreateExpression(schema.items[i], references, `${value}[${i}]`);
@@ -417,7 +412,7 @@ var TypeCompiler;
417
412
  function* UserDefined(schema, references, value) {
418
413
  const schema_key = `schema_key_${state_remote_custom_types.size}`;
419
414
  state_remote_custom_types.set(schema_key, schema);
420
- yield `custom('${schema[Types.Kind]}', '${schema_key}', ${value})`;
415
+ yield `custom(${StringConstant(schema[Types.Kind])}, '${schema_key}', ${value})`;
421
416
  }
422
417
  function* Visit(schema, references, value) {
423
418
  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.26.8",
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.11.2",
41
- "ajv-formats": "^2.1.1",
42
- "chai": "^4.3.6",
43
- "mocha": "^9.2.2",
44
- "prettier": "^2.7.1",
45
- "typescript": "^5.0.2"
46
- }
47
- }
1
+ {
2
+ "name": "@sinclair/typebox",
3
+ "version": "0.26.9",
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.11.2",
41
+ "ajv-formats": "^2.1.1",
42
+ "chai": "^4.3.6",
43
+ "mocha": "^9.2.2",
44
+ "prettier": "^2.7.1",
45
+ "typescript": "^5.0.2"
46
+ },
47
+ "allowScripts": {
48
+ "esbuild@0.14.53": true
49
+ }
50
+ }