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

@tezx/validate

v1.0.1

Published

Schema validation middleware for TezX framework with support for Zod, AJV, Joi and custom adapters.

Readme

@tezx/validate

Schema validation middleware for TezX framework. Supports Zod, AJV, Joi, or any custom schema adapter. Automatically parses request data (body, query, params) and sets ctx.validated for downstream handlers.


Features

  • Works with TezX middleware.
  • Supports async validation.
  • Compatible with Zod, AJV, Joi, or any custom adapter.
  • Optional error handler for validation failures.
  • Optional data transformation after validation.
  • Supports parsing of json, text, or formData request bodies.

Installation

npm install @tezx/validate
# or
yarn add @tezx/validate

Usage

Import

import { validate, SchemaAdapter } from "@tezx/validate";
import { z } from "zod"; // or import AJV/Joi

Basic Example with Zod

const userSchema: SchemaAdapter = {
  validate: async (data) =>
    z.object({
      username: z.string().min(3),
      password: z.string().min(6)
    }).parseAsync(data)
};

app.use(
  "/login",
  validate({ adapter: userSchema }),
  (ctx) => {
    // ctx.validated contains validated data
    ctx.json({ success: true, user: ctx.validated });
  }
);

Using AJV

import Ajv from "ajv";

const ajv = new Ajv();
const schema = {
  type: "object",
  properties: {
    username: { type: "string", minLength: 3 },
    password: { type: "string", minLength: 6 }
  },
  required: ["username", "password"],
  additionalProperties: false
};

const ajvAdapter: SchemaAdapter = {
  validate: async (data) => {
    const valid = ajv.validate(schema, data);
    if (!valid) throw new Error(JSON.stringify(ajv.errors));
    return data;
  }
};

app.use("/login", validate({ adapter: ajvAdapter }));

Using Joi

import Joi from "joi";

const joiSchema = Joi.object({
  username: Joi.string().min(3).required(),
  password: Joi.string().min(6).required()
});

const joiAdapter: SchemaAdapter = {
  validate: async (data) => {
    const { error, value } = joiSchema.validate(data);
    if (error) throw error;
    return value;
  }
};

app.use("/login", validate({ adapter: joiAdapter }));

Options

| Option | Type | Default | Description | | | | ----------- | -------------------------------------------- | ------- | --------------------------------------------- | -------- | ------------------------------ | | adapter | SchemaAdapter | — | Schema validator implementation | | | | onError | (err: Error, ctx: Context) => Response | — | Custom error handler | | | | source | "body" | "query" | "params" | "body" | Source of data to validate | | transform | (data: any) => any | — | Optional function to transform validated data | | | | parseBody | "json" | "text" | "formData" | "json" | Parser method for request body |


Error Handling

By default, validate will throw a Error with status 400 on validation failure. You can provide onError to customize the response:

validate({
  adapter: userSchema,
  onError: (err, ctx) => ctx.json({ success: false, message: err.message }, 422)
});

TypeScript Support

ctx.validated is automatically typed as any, but you can narrow it by providing generic types:

validate<{ username: string; password: string }>({
  adapter: userSchema
});

Custom Adapters

You can implement any schema library by creating a simple adapter:

const customAdapter: SchemaAdapter = {
  validate: async (data) => {
    if (!data.username) throw new Error("Missing username");
    return data;
  }
};

app.use(validate({ adapter: customAdapter }));