@sinclair/typebox
Advanced tools
@@ -17,5 +17,2 @@ import { ValueError } from '../errors/index'; | ||
| } | ||
| export declare namespace MemberExpression { | ||
| function Encode(object: string, key: string): string; | ||
| } | ||
| export declare class TypeCompilerUnknownTypeError extends Error { | ||
@@ -22,0 +19,0 @@ readonly schema: Types.TSchema; |
+33
-36
@@ -30,3 +30,3 @@ "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.TypeCompiler = exports.TypeCompilerUnknownTypeError = exports.MemberExpression = exports.TypeCheck = void 0; | ||
| exports.TypeCompiler = exports.TypeCompilerUnknownTypeError = exports.TypeCheck = void 0; | ||
| const index_1 = require("../errors/index"); | ||
@@ -105,30 +105,22 @@ const index_2 = require("../system/index"); | ||
| })(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)}]`; | ||
| } | ||
| // ------------------------------------------------------------------- | ||
| var MemberExpression; | ||
| (function (MemberExpression) { | ||
| function Check(propertyName) { | ||
| if (propertyName.length === 0) | ||
| return false; | ||
| { | ||
| const code = propertyName.charCodeAt(0); | ||
| if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code))) { | ||
| return false; | ||
| } | ||
| } | ||
| for (let i = 1; i < propertyName.length; i++) { | ||
| const code = propertyName.charCodeAt(i); | ||
| if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code) || Character.Numeric(code))) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| function Encode(object, key) { | ||
| return !Check(key) ? `${object}['${key}']` : `${object}.${key}`; | ||
| } | ||
| MemberExpression.Encode = Encode; | ||
| })(MemberExpression = exports.MemberExpression || (exports.MemberExpression = {})); | ||
| // ------------------------------------------------------------------- | ||
| // TypeCompiler | ||
@@ -205,4 +197,7 @@ // ------------------------------------------------------------------- | ||
| } | ||
| else if (typeof schema.const === 'string') { | ||
| yield `(${value} === ${StringConstant(schema.const)})`; | ||
| } | ||
| else { | ||
| yield `(${value} === '${schema.const}')`; | ||
| throw Error('Invalid Literal Value'); | ||
| } | ||
@@ -255,3 +250,3 @@ } | ||
| else { | ||
| const keys = `[${propertyKeys.map((key) => `'${key}'`).join(', ')}]`; | ||
| const keys = `[${propertyKeys.map((key) => `${StringConstant(key)}`).join(', ')}]`; | ||
| yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key)))`; | ||
@@ -262,7 +257,7 @@ } | ||
| const expression = CreateExpression(schema.additionalProperties, 'value[key]'); | ||
| const keys = `[${propertyKeys.map((key) => `'${key}'`).join(', ')}]`; | ||
| const keys = `[${propertyKeys.map((key) => `${StringConstant(key)}`).join(', ')}]`; | ||
| yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`; | ||
| } | ||
| for (const propertyKey of propertyKeys) { | ||
| const memberExpression = MemberExpression.Encode(value, propertyKey); | ||
| const memberExpression = MemberExpression(value, propertyKey); | ||
| const propertySchema = schema.properties[propertyKey]; | ||
@@ -272,3 +267,3 @@ if (schema.required && schema.required.includes(propertyKey)) { | ||
| if (index_3.TypeExtends.Undefined(propertySchema)) { | ||
| yield `('${propertyKey}' in ${value})`; | ||
| yield `(${StringConstant(propertyKey)} in ${value})`; | ||
| } | ||
@@ -293,3 +288,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)))`; | ||
@@ -321,7 +316,7 @@ const expression = CreateExpression(valueSchema, '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}))`; | ||
| } | ||
@@ -333,2 +328,4 @@ } | ||
| return yield `(${value}.length === 0)`; | ||
| if (!IsNumber(schema.maxItems)) | ||
| throw Error('MaxItems: Not a Number'); | ||
| yield `(${value}.length === ${schema.maxItems})`; | ||
@@ -363,3 +360,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}))`; | ||
| } | ||
@@ -366,0 +363,0 @@ function* Visit(schema, value) { |
+5
-2
| { | ||
| "name": "@sinclair/typebox", | ||
| "version": "0.25.24", | ||
| "version": "0.25.25", | ||
| "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", | ||
@@ -29,3 +29,3 @@ "keywords": [ | ||
| "type": "git", | ||
| "url": "https://github.com/sinclairzx81/typebox" | ||
| "url": "https://github.com/sinclairzx81/sinclair-typebox" | ||
| }, | ||
@@ -52,3 +52,6 @@ "scripts": { | ||
| "typescript": "^4.9.3" | ||
| }, | ||
| "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
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
354056
-2.09%5866
-0.1%1
Infinity%