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

express-validator-schema-inference

v0.0.2

Published

type helper to infer express-validator's schema type

Readme

express-validator-schema-inference

TypeScript type helper to infer the resulting data type from an express-validator schema.

This utility allows you to statically infer the shape of validated request data, improving type safety and developer experience in Express projects using express-validator.

Features

  • Infers nested object and array types from express-validator schema keys (e.g., user.name.first, user.phones.*.number).
  • Supports common express-validator options (see below).

Supported schema options

The following express-validator schema options are currently supported for type inference:

Validators

  • isBoolean
  • isDate
  • isFloat
  • isInt
  • isString
  • isULID
  • isAlpha
  • isAlphanumeric
  • isAscii
  • isBase32
  • isBase58
  • isBase64
  • isBtcAddress
  • isCreditCard
  • isCurrency
  • isEmail
  • isISO6346
  • isISO4217
  • isISO8601
  • isObject
  • notEmpty
  • isEmpty
  • isNumeric
  • isJSON
  • isURL
  • isUUID
  • isMobilePhone
  • isLength
  • contains
  • equals
  • matches
  • isIP
  • isHexadecimal
  • isMongoId
  • isMD5
  • isDecimal
  • isEthereumAddress
  • isFQDN
  • isHash
  • isHexColor
  • isIBAN
  • isISBN
  • isISSN
  • isISIN
  • isJWT
  • isLatLong
  • isLocale
  • isLowercase
  • isMimeType
  • isPassportNumber
  • isPort
  • isPostalCode
  • isRFC3339
  • isSemVer
  • isSlug
  • isUppercase
  • isDataURI
  • isMagnetURI
  • isMailtoURI
  • isMACAddress
  • isOctal
  • isRgbColor
  • isTime
  • isVAT
  • isWhitelisted
  • isArray
  • isIn

Sanitizers

  • toBoolean
  • toDate
  • toFloat
  • toInt
  • ltrim
  • rtrim
  • trim
  • toLowerCase
  • toUpperCase
  • escape
  • unescape
  • blacklist
  • whitelist
  • normalizeEmail
  • stripLow
  • toArray

Special options

  • optional (marks a field as optional, reflected in the output type)
  • custom (supports custom validators with type guards and assertions)
  • customSanitizer (supports custom sanitizers with return type inference)

Other options may be partially supported if they do not affect the output type. See Limitations below for unsupported or advanced features.

Limitations

  • Recursive fields using **.field (double star for deep/recursive matching) are not supported.
  • Multiple custom validators/sanitizers using arbitrary keys (as described in the express-validator documentation) are not supported. Only single custom and customSanitizer properties are supported per field.
  • Not all express-validator features are supported. Please see the source or open an issue if you need support for additional features.

Install

npm install --save-dev express-validator-schema-inference

Usage example

import type { Request, Response } from 'express';
import type { Schema } from 'express-validator';
import type { Infer } from 'express-validator-schema-inference';

function makeController<T extends Schema>(
  schema: T,
  handler: (req: Request & { data: Infer<T> }, res: Response) => any
) {
  // You should implement makeController so that req.data is set to the return value of matchedData.
}

makeController(
  {
    'user.name.first': {
      isString: true,
      trim: true,
    },
    'user.name.last': {
      isString: true,
      trim: true,
    },
    'user.emails': {
      isArray: true,
      isEmail: true,
    },
    'user.phones': {
      isArray: true,
      optional: true,
    },
    'user.phones.*.number': {
      isNumeric: true,
      trim: true,
    },
    'user.phones.*.code': {
      isNumeric: true,
      trim: true,
    },
    'user.age': {
      isInt: true,
      toInt: true,
    },
    role: {
      isIn: { options: [['owner', 'admin', 'user'] as const] },
      optional: { options: { values: 'null' } },
    },
  },
  (req, res) => {
    req.data;
    // {
    //   user: {
    //     name: {
    //       first: string;
    //       last: string;
    //     }
    //     emails: string[];
    //     phones: {
    //      number: string;
    //      code: string;
    //     }[] | undefined;
    //     age: number;
    //   }
    //   role: "owner" | "admin" | "user" | undefined | null;
    // }
  }
);

License

MIT License