@sinclair/typebox
Advanced tools
+36
-41
@@ -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}))`; | ||
@@ -359,3 +352,3 @@ } | ||
| const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0]; | ||
| const local = PushLocal(`new RegExp(/${keyPattern}/)`); | ||
| const local = PushLocal(`${new RegExp(keyPattern)}`); | ||
| yield `(Object.getOwnPropertyNames(${value}).every(key => ${local}.test(key)))`; | ||
@@ -388,7 +381,7 @@ const expression = CreateExpression(valueSchema, references, 'value'); | ||
| if (schema.pattern !== undefined) { | ||
| const local = PushLocal(`${new RegExp(schema.pattern)};`); | ||
| const local = PushLocal(`${new RegExp(schema.pattern)}`); | ||
| yield `${local}.test(${value})`; | ||
| } | ||
| if (schema.format !== undefined) { | ||
| yield `format('${schema.format}', ${value})`; | ||
| yield `format(${StringConstant(schema.format)}, ${value})`; | ||
| } | ||
@@ -403,2 +396,4 @@ } | ||
| return yield `${value}.length === 0`; | ||
| if (!IsNumber(schema.maxItems)) | ||
| throw Error('MaxItems: Not a Number'); | ||
| yield `(${value}.length === ${schema.maxItems})`; | ||
@@ -433,3 +428,3 @@ for (let i = 0; i < schema.items.length; i++) { | ||
| 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})`; | ||
| } | ||
@@ -436,0 +431,0 @@ function* Visit(schema, references, value) { |
+5
-2
| { | ||
| "name": "@sinclair/typebox", | ||
| "version": "0.26.8", | ||
| "version": "0.26.9", | ||
| "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", | ||
@@ -24,3 +24,3 @@ "keywords": [ | ||
| "type": "git", | ||
| "url": "https://github.com/sinclairzx81/typebox" | ||
| "url": "https://github.com/sinclairzx81/sinclair-typebox" | ||
| }, | ||
@@ -47,3 +47,6 @@ "scripts": { | ||
| "typescript": "^5.0.2" | ||
| }, | ||
| "allowScripts": { | ||
| "esbuild@0.14.53": true | ||
| } | ||
| } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
404929
-0.35%6709
-0.07%