validation-gen
v1.0.1
Published
CLI tool to generate validation functions file with user-selected validation types
Maintainers
Readme
validation-gen
A CLI tool to generate validation functions file with user-selected validation types for TypeScript and JavaScript projects.
Features
- 🚀 Interactive CLI to select validation functions
- 📝 Supports both TypeScript and JavaScript
- 🎯 Auto-detects project structure (creates file in
src/if exists, otherwise in root) - ✅ Production-ready validation functions
- 📦 Zero runtime dependencies in generated files
Installation
Global Installation
npm install -g validation-genUse with npx (Recommended)
npx validation-genUsage
Run the CLI tool:
npx validation-genThe tool will:
- Prompt you to select TypeScript or JavaScript
- Let you choose which validation functions you need
- Auto-detect your project structure
- Generate the validation file in the appropriate location
Example Output
🚀 Validation Generator
==================================================
? Select the language for your validation file: TypeScript (.ts)
? Select the validation functions you need:
◯ Input Field Validation - Generic input validation (required, min/max length, pattern matching)
◯ Email Validation - Email format validation
◯ Password Validation - Password strength validation with configurable requirements
◯ Name Validation - Name format validation (first name, last name, full name)
◯ Phone Validation - Phone number validation (US, international, or any format)
◯ URL Validation - URL format validation with protocol restrictions
◯ Number Validation - Numeric value validation (min/max, integer only)
📋 Generation Summary:
──────────────────────────────────────────────────
Language: TypeScript
Selected Validations: 3
✓ Email Validation
✓ Password Validation
✓ Name Validation
Output Path: src/validations.ts
──────────────────────────────────────────────────
? Generate the validation file? Yes
✅ Success!
📄 Validation file generated at: src/validations.tsAvailable Validation Functions
1. Input Field Validation (validateInput)
Generic input validation with configurable options.
TypeScript:
import { validateInput } from './validations';
const result = validateInput('hello', {
required: true,
minLength: 3,
maxLength: 10,
pattern: '^[a-zA-Z]+$'
});
if (!result.isValid) {
console.log(result.error);
}JavaScript:
import { validateInput } from './validations';
const result = validateInput('hello', {
required: true,
minLength: 3,
maxLength: 10,
pattern: '^[a-zA-Z]+$'
});2. Email Validation (validateEmail)
Validates email address format.
Usage:
import { validateEmail } from './validations';
const result = validateEmail('[email protected]', {
required: true
});3. Password Validation (validatePassword)
Validates password strength with configurable requirements.
Usage:
import { validatePassword } from './validations';
const result = validatePassword('MyP@ssw0rd', {
required: true,
minLength: 8,
requireUppercase: true,
requireLowercase: true,
requireNumber: true,
requireSpecialChar: true
});4. Name Validation (validateName)
Validates name format (first name, last name, or full name).
Usage:
import { validateName } from './validations';
// First name
const firstNameResult = validateName('John', {
required: true,
type: 'first'
});
// Last name
const lastNameResult = validateName('Doe', {
required: true,
type: 'last'
});
// Full name
const fullNameResult = validateName('John Doe', {
required: true,
type: 'full'
});5. Phone Validation (validatePhone)
Validates phone number format (US, international, or any).
Usage:
import { validatePhone } from './validations';
// US format
const usResult = validatePhone('1234567890', {
required: true,
format: 'us'
});
// International format
const intlResult = validatePhone('+1234567890', {
required: true,
format: 'international'
});
// Any format
const anyResult = validatePhone('1234567890', {
required: true,
format: 'any'
});6. URL Validation (validateURL)
Validates URL format with protocol restrictions.
Usage:
import { validateURL } from './validations';
const result = validateURL('https://example.com', {
required: true,
allowedProtocols: ['http', 'https']
});7. Number Validation (validateNumber)
Validates numeric values with min/max constraints.
Usage:
import { validateNumber } from './validations';
const result = validateNumber(42, {
required: true,
min: 0,
max: 100,
integerOnly: true
});Validation Result Structure
All validation functions return a ValidationResult object:
TypeScript:
interface ValidationResult {
isValid: boolean;
error?: string;
}JavaScript:
/**
* @typedef {Object} ValidationResult
* @property {boolean} isValid - Whether the validation passed
* @property {string} [error] - Error message if validation failed
*/Project Structure Detection
The tool automatically detects your project structure:
- If a
src/folder exists: generatessrc/validations.{ts|js} - If no
src/folder: generatesvalidations.{ts|js}in the root directory
File Overwrite Protection
If a validation file already exists at the target location, the tool will display an error and ask you to delete the existing file first. This prevents accidental overwrites.
Development
Project Structure
validation-gen/
├── bin/
│ └── validation-gen.js # CLI entry point
├── src/
│ ├── cli.js # Interactive CLI logic
│ ├── generator.js # File generation logic
│ ├── utils.js # Utility functions
│ └── validation-functions.js # Validation function templates
├── templates/
│ ├── validations.ts.template
│ └── validations.js.template
├── package.json
└── README.mdLicense
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
