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

@nayi/formulate

v1.0.0

Published

Type-safe input normalization and validation library for any JS/TS app — built for APIs, forms, and frameworks.

Readme

@nayi/formulate

formulate is a framework-agnostic, TypeScript-first input normalization and validation library.
It helps you convert raw user inputs (usually strings from req.query, req.body, or forms) into well-typed, validated, and sanitized JavaScript objects.


✨ Features

  • 🔁 Converts strings into boolean, number, Date, null, etc.
  • 📦 Supports nested objects and arrays
  • 🧩 Custom field transformers and type parsers
  • 🚦 Built-in validation with error collection or strict mode
  • 🛡️ Schema fallback and default value injection
  • 🔍 Whitelist/blacklist support
  • 💡 Dual API: OOP and functional
  • ⚙️ Ready for Express, React, Next.js, Vue, NestJS, and any JS/TS stack

📦 Installation

npm install @nayi/formulate zod yup

🔧 Usage

🔹 Functional API

import { normalize } from '@nayi/formulate';

const { result, errors } = normalize(req.query, {
  convertBooleans: true,
  convertNumbers: true,
  validationMode: 'collect',
  validators: {
    age: (val) => val >= 18,
  },
});

🔹 OOP API

import { InputNormalizer } from '@nayi/formulate';

const normalizer = new InputNormalizer({
  defaultValues: { role: 'user' },
  fieldTransformers: {
    email: (val) => val.trim().toLowerCase(),
  },
});

const { result } = normalizer.normalize({
  email: '  [email protected]  ',
});

🔹 Express Middleware

import express from 'express';
import { createNormalizerMiddleware } from '@nayi/formulate';

const app = express();
app.use(express.json());

app.post(
  '/submit',
  createNormalizerMiddleware({
    source: 'body',
    options: {
      validators: {
        password: (val) => val.length >= 8,
      },
      validationMode: 'collect',
    },
  }),
  (req, res) => {
    if (req.normalized?.errors) {
      return res.status(400).json({ errors: req.normalized.errors });
    }
    res.send(req.normalized.result);
  }
);

🧪 Schema Validation Support

✅ Zod

import { z } from 'zod';

const schema = z.object({
  username: z.string().min(3),
  age: z.number().min(18),
});

const { result, errors } = normalize(input, {
  schema: {
    type: 'zod',
    validator: schema,
  },
  validationMode: 'collect',
});

✅ Yup

import * as yup from 'yup';

const schema = yup.object().shape({
  email: yup.string().email().required(),
});

const { result, errors } = normalize(input, {
  schema: {
    type: 'yup',
    validator: schema,
  },
  validationMode: 'collect',
});

✅ Custom Schema

const customSchema = {
  validate: (input) => {
    const errors = {};
    if (input.role !== 'admin') errors.role = 'Must be admin';
    return { valid: Object.keys(errors).length === 0, errors };
  },
};

const { result, errors } = normalize(input, {
  schema: {
    type: 'custom',
    validator: customSchema,
  },
  validationMode: 'collect',
});

⚙️ Options

| Option | Type | Description | |--------|------|-------------| | convertBooleans | boolean | Convert "true" / "false" | | convertNumbers | boolean | Convert "123" to 123 | | convertNulls | boolean | Convert "null" / "undefined" | | enableDateParsing | boolean | Parse ISO and short dates | | enableJsonParsing | boolean | Convert JSON strings to objects/arrays | | treatEmptyStringAs | "null" \| "undefined" \| "keep" | How to handle empty strings | | removeUndefinedFields | boolean | If true, removes undefined keys | | fieldTransformers | { [key]: (val) => any } | Field-specific mutation | | fieldParsers | { type: (val) => any } | Per-type custom parser | | validators | { [key]: (val) => boolean } | Field validation logic | | defaultValues | { [key]: any } | Fallback values if null/undefined | | schemaFallbacks | { [key]: (val) => any } | Apply fallback if schema fails | | validationMode | "none" \| "strict" \| "collect" | Error behavior | | schema | zod \| yup \| custom | Schema-level validator |


📁 Directory Structure

src/
├── InputNormalizer.ts       # Main class-based engine
├── normalize.ts             # Functional API
├── utils.ts                 # Helpers
├── types.ts                 # Interfaces and types

Documentation


📄 License

MIT © 2025 Patrick NAYITURIKI Pull requests are welcome!


🤝 Contribution

See CONTRIBUTING.md for how to propose changes and collaborate.

📦 Releases & Changelog

See CHANGELOG.md for a full history of updates.

Feel free to submit bug reports, PRs, or feature ideas.