npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

validation-gen

v1.0.1

Published

CLI tool to generate validation functions file with user-selected validation types

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-gen

Use with npx (Recommended)

npx validation-gen

Usage

Run the CLI tool:

npx validation-gen

The tool will:

  1. Prompt you to select TypeScript or JavaScript
  2. Let you choose which validation functions you need
  3. Auto-detect your project structure
  4. 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.ts

Available 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: generates src/validations.{ts|js}
  • If no src/ folder: generates validations.{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.md

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.