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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@yazouv/form-validator

v0.1.4

Published

A simple TypeScript form validation library

Readme

@yazouv/form-validator

A lightweight, TypeScript-based form validation library with a simple and intuitive API.

NPM Version License: MIT

Features

  • 🚀 Lightweight - Minimal dependencies, small bundle size
  • 📝 TypeScript - Full TypeScript support with type definitions
  • 🔧 Simple API - Easy to use pipe-based rule syntax
  • Fast - Efficient validation with early returns
  • 🎯 Flexible - Combine multiple validation rules easily
  • 🔌 Extensible - Built-in rules cover most common use cases

Installation

npm install @yazouv/form-validator

Quick Start

import { validate } from "@yazouv/form-validator";

// Define your data
const formData = {
  name: "John Doe",
  email: "[email protected]",
  age: "25",
};

// Define validation rules
const rules = {
  name: "required",
  email: "required|email",
  age: "required|number|min:18",
};

// Validate
const result = validate(formData, rules);

if (result.valid) {
  console.log("✅ Form is valid!");
} else {
  console.log("❌ Validation errors:", result.errors);
}

Available Validation Rules

Basic Rules

| Rule | Description | Example | | ---------- | ----------------------- | ------------ | | required | Field must not be empty | "required" | | email | Valid email format | "email" | | number | Must be a valid number | "number" | | link | Valid URL format | "link" |

Numeric Rules

| Rule | Description | Example | | -------------- | ------------- | ------------------ | | min:X | Minimum value | "min:18" | | max:X | Maximum value | "max:65" | | min:X\|max:Y | Value range | "min:18\|max:65" |

String Rules

| Rule | Description | Example | | --------------- | ------------------------------------------------- | --------------------- | | slug | URL-friendly format (lowercase, numbers, hyphens) | "slug" | | regex:pattern | Custom regex pattern | "regex:^[A-Z]{2,}$" |

Combining Rules

Use the pipe (|) character to combine multiple rules:

const rules = {
  username: "required|slug",
  email: "required|email",
  age: "required|number|min:18|max:99",
  website: "link", // Optional field
  password: "required|regex:^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{8,}$",
};

Examples

Basic Form Validation

import { validate } from "@yazouv/form-validator";

const userData = {
  firstName: "John",
  lastName: "Doe",
  email: "[email protected]",
  age: "28",
};

const rules = {
  firstName: "required",
  lastName: "required",
  email: "required|email",
  age: "required|number|min:18",
};

const result = validate(userData, rules);
// { valid: true, errors: {} }

Handling Validation Errors

const invalidData = {
  firstName: "",
  email: "invalid-email",
  age: "15",
};

const result = validate(invalidData, rules);

if (!result.valid) {
  // Handle each field's errors
  Object.entries(result.errors).forEach(([field, errors]) => {
    console.log(`${field}: ${errors.join(", ")}`);
  });
}

// Output:
// firstName: This field is required
// email: Invalid email format
// age: Must be at least 18

Advanced Examples

User Registration Form

const registrationData = {
  username: "john-doe",
  email: "[email protected]",
  password: "MySecurePass123",
  age: "25",
  website: "https://johndoe.com",
};

const registrationRules = {
  username: "required|slug",
  email: "required|email",
  password: "required|regex:^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{8,}$",
  age: "required|number|min:18",
  website: "link", // Optional
};

const result = validate(registrationData, registrationRules);

E-commerce Product Form

const productData = {
  name: "Gaming Laptop",
  price: "999.99",
  category: "electronics",
  description: "High-performance gaming laptop",
};

const productRules = {
  name: "required",
  price: "required|number|min:0",
  category: "required|slug",
  description: "required|max:500",
};

Contact Form with Phone Validation

const contactData = {
  name: "Jane Smith",
  email: "[email protected]",
  phone: "+1234567890",
  message: "Hello, I'm interested in your services.",
};

const contactRules = {
  name: "required",
  email: "required|email",
  phone: "regex:^\\+?[1-9]\\d{1,14}$",
  message: "required|min:10|max:1000",
};

API Reference

validate(data, rules)

Validates an object against a set of rules.

Parameters:

  • data (object): The data object to validate
  • rules (object): Object defining validation rules for each field

Returns:

  • { valid: boolean, errors: object } - Validation result

Example:

const result = validate(
  { email: "[email protected]" },
  { email: "required|email" }
);
// { valid: true, errors: {} }

Error Messages

The library provides clear, user-friendly error messages:

  • required: "This field is required"
  • email: "Invalid email format"
  • number: "Must be a number"
  • link: "This field must be a valid URL"
  • min:X: "Must be at least X"
  • max:X: "Must be at most X"
  • slug: "Invalid format"
  • regex: "Invalid format"

TypeScript Support

Full TypeScript support with proper type definitions:

interface ValidationResult {
  valid: boolean;
  errors: Record<string, string[]>;
}

function validate(
  data: Record<string, any>,
  rules: Record<string, string>
): ValidationResult;

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you find this package useful, please consider giving it a ⭐ on GitHub!

For issues and feature requests, please use the GitHub Issues page.