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

@validex/fastify

v1.0.3

Published

Fastify plugin for validex — request validation via decorators and hooks

Downloads

458

Readme

@validex/fastify

npm version npm downloads build TypeScript 5.0+ license MIT

Fastify 5 plugin for validex — request validation via decorators and hooks.



Prerequisites

@validex/core, zod, fastify, and fastify-plugin must be installed.

Install

pnpm add @validex/core @validex/fastify zod fastify fastify-plugin

Plugin Registration

import Fastify from 'fastify'
import { validexPlugin } from '@validex/fastify'

const app = Fastify()

await app.register(validexPlugin, {
  rules: {
    email: { blockDisposable: true },
    password: { length: { min: 10 } },
  },
  preload: {
    disposable: true,
    passwords: 'basic',
  },
})

Plugin Options

interface ValidexFastifyOptions {
  rules?: GlobalConfig['rules']       // Per-rule defaults (same as setup({ rules }))
  preload?: PreloadOptions            // Data files to preload at registration
  errorHandler?: (                    // Custom validation error handler
    result: ValidationResult<unknown>,
    request: FastifyRequest,
    reply: FastifyReply,
  ) => void | Promise<void>
}

Request Validation

In-handler validation

Validate request body, query, or params inside route handlers:

import { z } from 'zod'
import { Email } from '@validex/core'

const schema = z.object({ email: Email() })

app.post('/users', async (request) => {
  const result = await request.validate(schema)
  if (!result.success) {
    return { errors: result.firstErrors }
  }
  return { user: result.data }
})

// Validate query params
app.get('/search', async (request) => {
  const result = await request.validate(querySchema, { source: 'query' })
  // ...
})

// Validate route params
app.get('/users/:id', async (request) => {
  const result = await request.validate(paramsSchema, { source: 'params' })
  // ...
})

Route-level preValidation

Automatically validate request body before the handler runs:

app.post('/users', {
  // Fastify preValidation hook — validates body before handler runs
  config: { validex: { body: userSchema } },
  handler: async (request) => {
    return { created: true }
  },
})

Failed validation returns a 400 response with structured errors by default:

{
  "statusCode": 400,
  "error": "Validation Error",
  "errors": { "email": "Email is not a valid email address" },
  "allErrors": { "email": ["Email is not a valid email address"] }
}

errors contains the first error message per field (for simple display). allErrors contains all messages per field (for detailed feedback).

Decorators

The plugin adds the following to the Fastify instance:

| Decorator | Scope | Signature | |-----------|-------|-----------| | app.validate | Instance | (schema, data) => Promise<ValidationResult> | | request.validate | Request | (schema, opts?) => Promise<ValidationResult> |

app.validate validates arbitrary data. request.validate validates from request body (default), query, or params.

Standalone Functions

The decorator implementations are also exported for use outside the request lifecycle (e.g., in tests, workers, or middleware):

import { validateData, validateRequest } from '@validex/fastify'

const result = await validateData(schema, data)

| Function | Signature | |----------|-----------| | validateData | (schema, data) => Promise<ValidationResult> | | validateRequest | (schema, sources, source?) => Promise<ValidationResult> |

Error Handling

Override the default 400 response with a custom error handler:

await app.register(validexPlugin, {
  errorHandler: (result, request, reply) => {
    reply.status(422).send({
      message: 'Validation failed',
      errors: result.errors,
    })
  },
})

Documentation

License

MIT