UNPKG

spanwright

Version:

CLI tool to generate Cloud Spanner E2E testing framework projects with Go database tools and Playwright browser automation

71 lines 3.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateProjectName = validateProjectName; exports.validateDatabaseCount = validateDatabaseCount; exports.isFlag = isFlag; exports.sanitizeInput = sanitizeInput; exports.validateDatabaseName = validateDatabaseName; exports.validateTemplateVariable = validateTemplateVariable; exports.validateSchemaPath = validateSchemaPath; exports.validateAllTemplateInputs = validateAllTemplateInputs; const errors_1 = require("./errors"); const constants_1 = require("./constants"); // Input validation utilities function validateProjectName(name) { if (!name || name.trim().length === 0) { throw new errors_1.ValidationError('Project name cannot be empty', 'projectName'); } // Basic pattern matching if (!/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(name)) { throw new errors_1.ValidationError('Project name can only contain letters, numbers, hyphens, and underscores. Must start with a letter.', 'projectName'); } } function validateDatabaseCount(count) { if (!constants_1.VALIDATION.DB_COUNTS.includes(count)) { throw new errors_1.ValidationError('Database count must be 1 or 2', 'dbCount'); } } function isFlag(arg) { return arg.startsWith(constants_1.VALIDATION.FLAG_PREFIX); } function sanitizeInput(input) { return input.trim(); } // Enhanced security validation functions function validateDatabaseName(name, fieldName) { if (!name || name.trim().length === 0) { throw new errors_1.ValidationError(`${fieldName} cannot be empty`, fieldName); } // Basic pattern matching if (!/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(name)) { throw new errors_1.ValidationError(`${fieldName} can only contain letters, numbers, hyphens, and underscores. Must start with a letter.`, fieldName); } // Basic length limit if (name.length > 30) { throw new errors_1.ValidationError(`${fieldName} must be 30 characters or less`, fieldName); } } function validateTemplateVariable(value, variableName) { if (!value || value.trim().length === 0) { throw new errors_1.ValidationError(`Template variable ${variableName} cannot be empty`, variableName); } // Basic pattern matching if (!/^[a-zA-Z0-9_-]+$/.test(value)) { throw new errors_1.ValidationError(`Template variable ${variableName} can only contain letters, numbers, hyphens, and underscores`, variableName); } } function validateSchemaPath(path, fieldName) { if (!path || path.trim().length === 0) { throw new errors_1.ValidationError(`${fieldName} cannot be empty`, fieldName); } // Basic path validation if (!/^[a-zA-Z0-9_./\\-]+$/.test(path)) { throw new errors_1.ValidationError(`${fieldName} contains invalid characters. Only letters, numbers, dots, slashes, backslashes, hyphens, and underscores are allowed.`, fieldName); } } function validateAllTemplateInputs(inputs) { for (const [key, value] of Object.entries(inputs)) { validateTemplateVariable(value, key); } } //# sourceMappingURL=validation.js.map