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

@emorio/zod-validator

v0.0.4

Published

Zod request validator for AdonisJS

Downloads

2,178

Readme

Zod Validator for AdonisJS

A lightweight, type-safe request validation package for AdonisJS that leverages the power of Zod schemas instead of VineJS.

This package extends AdonisJS's existing validation pattern by adding support for Zod schemas while maintaining the familiar request.validateUsing() API. Instead of learning VineJS syntax, you can use Zod's powerful type-safe validation directly in your AdonisJS controllers.

// Instead of VineJS
const data = await request.validateUsing(vine.compile(vineSchema))

// Use Zod directly
const data = await request.validateUsing(zodSchema)

Installation

Install the package using npm, yarn, or pnpm:

npm install @emorio/zod-validator zod
yarn add @emorio/zod-validator zod
pnpm add @emorio/zod-validator zod

Configuration

1. Register the Provider

Add the provider to your adonisrc.ts file:

import { defineConfig } from '@adonisjs/core/app'

export default defineConfig({
  // ... other config
  providers: [
    // ... other providers
    () => import('@emorio/zod-validator/zod_provider'),
  ],
})

Usage

Basic Validation

import { HttpContext } from '@adonisjs/core/http'
import { z } from 'zod'

// Define your Zod schema
const createUserSchema = z.object({
  username: z.string().min(3).max(50),
  email: z.email(),
  age: z.number().int().min(18),
})

export default class UsersController {
  async store({ request, response }: HttpContext) {
    try {
      // Validate request data
      const validatedData = await request.validateUsing(createUserSchema)

      console.log(validatedData.username) // TypeScript knows this is a string

      // Create user with validated data
      // ... your logic here

      return response.json({ message: 'User created successfully' })
    } catch (error) {
      return response.status(422).json({ errors: error.errors })
    }
  }
}

Advanced Validation with Headers and Params

import { HttpContext } from '@adonisjs/core/http'
import { z } from 'zod'

export default class PostsController {
  async update({ request, response }: HttpContext) {
    const updatePostSchema = z.object({
      // Request body validation
      title: z.string().min(1).max(255),
      content: z.string().min(10),
      published: z.boolean().optional(),

      // Route params validation
      params: z.object({
        id: z.string().uuid(),
      }),

      // Headers validation
      headers: z.object({
        'content-type': z.string(),
        'authorization': z.string().startsWith('Bearer '),
      }),

      // Cookies validation (optional)
      cookies: z
        .object({
          session_id: z.string().optional(),
        })
        .optional(),
    })

    const validatedData = await request.validateUsing(updatePostSchema)

    // Access validated data with full type safety
    const postId = validatedData.params.id
    const authToken = validatedData.headers.authorization
    const title = validatedData.title

    // ... your logic here
  }
}

API Reference

request.validateUsing<Schema>(schema: Schema): Promise<z.infer<Schema>>

Validates the incoming request data using the provided Zod schema.

Parameters:

  • schema: A Zod schema to validate against

Returns:

  • Promise<z.output<Schema>>: The validated and typed data

Validation Data Sources: The method automatically validates data from:

  • Request body (request.all())
  • Route parameters (request.params())
  • Request headers (request.headers())
  • Cookies (request.cookiesList())

Error Handling: Throws a Zod validation error if validation fails. Handle this in your exception handler or with try/catch blocks.

Error Handling

Global Exception Handler

Create a custom exception handler to format Zod validation errors:

import { errors } from '@adonisjs/core'
import { HttpContext } from '@adonisjs/core/http'
import { ZodError } from 'zod'

export default class HttpExceptionHandler extends errors.HttpExceptionHandler {
  async handle(error: unknown, ctx: HttpContext) {
    if (error instanceof ZodError) {
      // z.treeifyError(error);
      // handle the error
    }

    return super.handle(error, ctx)
  }
}

Roadmap & Enhancements

Planned Features

  • [ ] ace add command: Add support for registering by using the node ace add command
  • [ ] Tuyau Integration: Generate Tuyau api and types with this zod schema
  • [ ] Custom File Validators: Enhanced file validation similar to VineJS file validators

Testing

Run the test suite:

npm test

Requirements

  • Node.js >= 20.6.0
  • AdonisJS >= 6.2.0
  • Zod >= 4.0.0

License

This package is open-sourced software licensed under the MIT license.

Credits


Need help? Open an issue on GitHub or start a discussion.