@sinclair/typebox
Advanced tools
+32
-39
@@ -82,31 +82,2 @@ "use strict"; | ||
| // ------------------------------------------------------------------- | ||
| // MemberExpression | ||
| // ------------------------------------------------------------------- | ||
| var MemberExpression; | ||
| (function (MemberExpression) { | ||
| function IsFirstCharacterNumeric(value) { | ||
| if (value.length === 0) | ||
| return false; | ||
| return Character.IsNumeric(value.charCodeAt(0)); | ||
| } | ||
| function IsAccessor(value) { | ||
| if (IsFirstCharacterNumeric(value)) | ||
| return false; | ||
| for (let i = 0; i < value.length; i++) { | ||
| const code = value.charCodeAt(i); | ||
| const check = Character.IsAlpha(code) || Character.IsNumeric(code) || Character.DollarSign(code) || Character.IsUnderscore(code); | ||
| if (!check) | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| function EscapeHyphen(key) { | ||
| return key.replace(/'/g, "\\'"); | ||
| } | ||
| function Encode(object, key) { | ||
| return IsAccessor(key) ? `${object}.${key}` : `${object}['${EscapeHyphen(key)}']`; | ||
| } | ||
| MemberExpression.Encode = Encode; | ||
| })(MemberExpression || (MemberExpression = {})); | ||
| // ------------------------------------------------------------------- | ||
| // Identifier | ||
@@ -131,2 +102,21 @@ // ------------------------------------------------------------------- | ||
| })(Identifier || (Identifier = {})); | ||
| // ------------------------------------------------------------------ | ||
| // StringConstant | ||
| // ------------------------------------------------------------------ | ||
| function IsString(value) { | ||
| return typeof value === 'string'; | ||
| } | ||
| function StringConstant(value) { | ||
| if (!IsString(value)) | ||
| throw Error('ConstantString: Not a String'); | ||
| const canonical = JSON.stringify(value).slice(1, -1); | ||
| const escaped = canonical.replace(/'/g, "\\'"); | ||
| return `'${escaped}'`; | ||
| } | ||
| // ------------------------------------------------------------------ | ||
| // MemberExpression | ||
| // ------------------------------------------------------------------ | ||
| function MemberExpression(value, key) { | ||
| return `${value}[${StringConstant(key)}]`; | ||
| } | ||
| // ------------------------------------------------------------------- | ||
@@ -175,3 +165,3 @@ // TypeCompiler | ||
| function IsExactOptionalProperty(value, key, expression) { | ||
| return index_2.TypeSystem.ExactOptionalPropertyTypes ? `('${key}' in ${value} ? ${expression} : true)` : `(${MemberExpression.Encode(value, key)} !== undefined ? ${expression} : true)`; | ||
| return index_2.TypeSystem.ExactOptionalPropertyTypes ? `(${StringConstant(key)} in ${value} ? ${expression} : true)` : `(${MemberExpression(value, key)} !== undefined ? ${expression} : true)`; | ||
| } | ||
@@ -261,3 +251,3 @@ function IsObjectCheck(value) { | ||
| // prettier-ignore | ||
| const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) => `'${key}'`).join(', '); | ||
| const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) => `${StringConstant(key)}`).join(', '); | ||
| const expressions = schema.allOf.map((schema) => CreateExpression(schema, references, value)); | ||
@@ -269,3 +259,3 @@ const expression1 = `Object.getOwnPropertyNames(${value}).every(key => [${schemaKeys}].includes(key))`; | ||
| // prettier-ignore | ||
| const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) => `'${key}'`).join(', '); | ||
| const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) => `${StringConstant(key)}`).join(', '); | ||
| const expressions = schema.allOf.map((schema) => CreateExpression(schema, references, value)); | ||
@@ -281,4 +271,7 @@ const expression1 = CreateExpression(schema.unevaluatedProperties, references, 'value[key]'); | ||
| } | ||
| else if (typeof schema.const === 'string') { | ||
| yield `(${value} === ${StringConstant(schema.const)})`; | ||
| } | ||
| else { | ||
| yield `${value} === '${schema.const}'`; | ||
| throw Error('Invalid Literal Value'); | ||
| } | ||
@@ -318,3 +311,3 @@ } | ||
| for (const knownKey of knownKeys) { | ||
| const memberExpression = MemberExpression.Encode(value, knownKey); | ||
| const memberExpression = MemberExpression(value, knownKey); | ||
| const property = schema.properties[knownKey]; | ||
@@ -324,3 +317,3 @@ if (schema.required && schema.required.includes(knownKey)) { | ||
| if (Types.ExtendsUndefined.Check(property)) | ||
| yield `('${knownKey}' in ${value})`; | ||
| yield `(${StringConstant(knownKey)} in ${value})`; | ||
| } | ||
@@ -337,3 +330,3 @@ else { | ||
| else { | ||
| const keys = `[${knownKeys.map((key) => `'${key}'`).join(', ')}]`; | ||
| const keys = `[${knownKeys.map((key) => `${StringConstant(key)}`).join(', ')}]`; | ||
| yield `Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key))`; | ||
@@ -344,3 +337,3 @@ } | ||
| const expression = CreateExpression(schema.additionalProperties, references, 'value[key]'); | ||
| const keys = `[${knownKeys.map((key) => `'${key}'`).join(', ')}]`; | ||
| const keys = `[${knownKeys.map((key) => `${StringConstant(key)}`).join(', ')}]`; | ||
| yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`; | ||
@@ -387,3 +380,3 @@ } | ||
| if (schema.format !== undefined) { | ||
| yield `format('${schema.format}', ${value})`; | ||
| yield `format(${StringConstant(schema.format)}, ${value})`; | ||
| } | ||
@@ -436,3 +429,3 @@ } | ||
| state_remote_custom_types.set(schema_key, schema); | ||
| yield `custom('${schema[Types.Kind]}', '${schema_key}', ${value})`; | ||
| yield `custom(${StringConstant(schema[Types.Kind])}, '${schema_key}', ${value})`; | ||
| } | ||
@@ -439,0 +432,0 @@ function* Visit(schema, references, value) { |
+5
-2
| { | ||
| "name": "@sinclair/typebox", | ||
| "version": "0.27.10", | ||
| "version": "0.27.11", | ||
| "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", | ||
@@ -24,3 +24,3 @@ "keywords": [ | ||
| "type": "git", | ||
| "url": "https://github.com/sinclairzx81/typebox-legacy" | ||
| "url": "https://github.com/sinclairzx81/sinclair-typebox" | ||
| }, | ||
@@ -49,3 +49,6 @@ "scripts": { | ||
| "typescript": "5.0.2" | ||
| }, | ||
| "allowScripts": { | ||
| "esbuild@0.15.18": true | ||
| } | ||
| } |
440260
-0.02%7339
-0.1%