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

smart-auth-validator

v1.4.8

Published

Zero-regex, type-safe authentication and form validation for Fastify and Express

Readme

smart-auth-validator


npm version npm downloads License: MIT TypeScript Node.js


Why smart-auth-validator?

Most backend validation libraries are either:

  • Too heavy
  • Schema-verbose
  • Regex-dependent
  • Hard to maintain
  • Poorly typed

smart-auth-validator is designed for modern Node.js backends that need:

✅ Type-safe validation ✅ Express middleware support ✅ Fastify preHandler support ✅ Clean error responses ✅ Zero boilerplate ✅ Zero manual regex handling ✅ Built-in authentication rules ✅ Direct validation utilities ✅ Lightweight runtime performance


Install

npm install smart-auth-validator

Features

  • TypeScript-first architecture
  • Built-in auth and form validation
  • Express middleware support
  • Fastify preHandler support
  • Fully typed validated payloads
  • Automatic structured error responses
  • Custom rules support
  • Custom error message overrides
  • Query/body/params validation
  • No schema DSL complexity
  • Minimal setup
  • ESM + CommonJS support

Quick Example

Instead of manually writing repetitive validation logic:

if (!req.body.email || !req.body.email.includes("@")) {
  return res.status(400).json({
    error: "Invalid email",
  });
}

Use:

import { expressAuthValidator } from "smart-auth-validator";

app.post(
  "/contact",
  expressAuthValidator({
    name: true,
    email: true,
    phone: true,
  }),
  handler
);

That's it.


Express Example

import express from "express";
import { expressAuthValidator } from "smart-auth-validator";

const app = express();

app.use(express.json());

app.post(
  "/register",
  expressAuthValidator({
    name: true,
    email: true,
    password: true,
    phone: true,
  }),
  (req, res) => {
    res.json({
      success: true,
      data: req.validated,
    });
  }
);

app.listen(3000);

Fastify Example

import Fastify from "fastify";
import { fastifyAuthValidator } from "smart-auth-validator";

const app = Fastify();

app.post(
  "/register",
  {
    preHandler: fastifyAuthValidator({
      name: true,
      email: true,
      password: true,
    }),
  },
  async (req, reply) => {
    reply.send({
      success: true,
      data: req.validated,
    });
  }
);

app.listen({ port: 3000 });

Direct Validation (Without Middleware)

You can also validate data directly inside:

  • Services
  • Workers
  • CRON jobs
  • Background tasks
  • CLI scripts
import { validate } from "smart-auth-validator";

const result = validate(
  {
    email: true,
    password: true,
  },
  {
    email: "[email protected]",
    password: "Secure@123",
  }
);

if (!result.success) {
  console.log(result.errors);
}

Custom Rules

Use rule objects for advanced validation control.

app.post(
  "/profile",
  expressAuthValidator({
    username: {
      min: 3,
      max: 20,
    },

    bio: {
      max: 160,
    },

    website: {
      regex: /^https?:\/\/.+/,
    },

    displayName: {
      min: 2,
      max: 50,
      transform: (value) => String(value).trim(),
    },
  }),
  handler
);

Custom Error Messages

Override validation errors per field.

expressAuthValidator({
  email: {
    messages: {
      REQUIRED: "Please enter your email",
      PATTERN: "Invalid email format",
    },
  },

  password: {
    min: 8,
    messages: {
      MIN_LENGTH: "Password must contain at least 8 characters",
    },
  },
});

Validate Query Params

expressAuthValidator(
  {
    q: true,
    page: true,
  },
  "query"
);

Example:

/search?q=nodejs&page=2

Validate Route Params

expressAuthValidator(
  {
    id: true,
  },
  "params"
);

Example:

/user/:id

Error Response Format

Validation errors are automatically standardized.

{
  "success": false,
  "errors": [
    {
      "field": "email",
      "code": "PATTERN",
      "message": "email format is invalid"
    },
    {
      "field": "password",
      "code": "MIN_LENGTH",
      "message": "password must be at least 8 characters"
    }
  ]
}

Access Typed Validated Data

Validated payloads become available on:

req.validated

Example:

app.post(
  "/register",
  expressAuthValidator({
    email: true,
  }),
  (req, res) => {
    const { email } = req.validated;

    res.json({
      success: true,
    });
  }
);

Supported Fields

Supported Fields

Identity

| Field | Description | |-------|-------------| | name | Full name validation (min/max length, no special characters) | | username | Username validation (alphanumeric, underscores allowed) | | email | Email format validation (RFC-compliant) | | password | Strong password (min 8 chars, uppercase, number, special char) |

Contact

| Field | Description | |-------|-------------| | phone | International phone number validation (E.164 format) | | url | URL format validation (http/https required) |

Address

| Field | Description | |-------|-------------| | street | Street address validation | | city | City name validation | | state | State or province validation | | postalCode | Postal/ZIP code validation |

Finance

| Field | Description | |-------|-------------| | creditCard | Credit card number validation (Luhn algorithm) | | cvv | CVV/CVC validation (3–4 digits) |

System

| Field | Description | |-------|-------------| | date | Date format validation (YYYY-MM-DD) | | time | Time format validation (HH:MM or HH:MM:SS) | | active | Boolean active/inactive status validation |

Media

| Field | Description | |-------|-------------| | avatar | Image validation (URL format, MIME type, size limits) |


Validation Error Codes

| Code | Description | | ------------------------ | -------------------------- | | REQUIRED | Field is missing | | INVALID_TYPE | Invalid data type | | MIN_LENGTH | Value shorter than minimum | | MAX_LENGTH | Value exceeds maximum | | PATTERN | Pattern validation failed | | INVALID_IMAGE | Invalid image object | | IMAGE_TOO_LARGE | Image exceeds allowed size | | UNSUPPORTED_IMAGE_TYPE | Unsupported MIME type |


Package Information

| Property | Value | | -------------- | ---------------- | | Runtime | Node.js >=18 | | Language | TypeScript | | Module Support | ESM + CommonJS | | Frameworks | Express, Fastify | | License | MIT |


SEO Keywords

Node.js validation library, Express validator middleware, Fastify validator, TypeScript validation library, authentication validation middleware, form validation package, schema validation for Node.js, backend validation package, TypeScript Express middleware, Fastify authentication validator, zero regex validation, auth validation package.


Contributing

Contributions, issues, and feature requests are welcome.


License

MIT © Burhan Chughtai


Links

  • npm: https://www.npmjs.com/package/smart-auth-validator
  • GitHub: https://github.com/smart-auth-validator/smart-auth-validator