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

yupinator

v1.0.0

Published

This is a package that will help you to validate your data using Yup

Downloads

5

Readme

Yupinator

Yupinator is a powerful and flexible utility for validating query parameters using Yup schemas. Designed for use in frameworks like Next.js, Express, and other Node.js environments, it ensures clean and reliable parameter validation with minimal effort.


Features

  • Schema-based Validation: Use Yup schemas to define validation rules.
  • Error Handling: Provides detailed error messages for invalid inputs.
  • Simple Integration: Works seamlessly with URLSearchParams from new URL(req.url).

Installation

Install Yupinator via NPM:

npm install yupinator


Usage

Validate Query Parameters from new URL(req.url)

The validateQueryParams function extracts query parameters from URLSearchParams and validates them against a Yup schema.

Example:

import { validateQueryParams } from 'yupinator';
import * as Yup from 'yup';

const validationSchema = Yup.object({
  first_name: Yup.string().required('First name is required'),
  age: Yup.number().positive('Age must be positive').required('Age is required'),
});

async function handleRequest(req: Request) {
  const { searchParams } = new URL(req.url);

  const { isValid, data, errors } = await validateQueryParams(searchParams, validationSchema);

  if (isValid) {
    console.log('Validated data:', data);
    return new Response('Validation passed!', { status: 200 });
  } else {
    console.error('Validation errors:', errors);
    return new Response(JSON.stringify({ errors }), { status: 400, headers: { 'Content-Type': 'application/json' } });
  }
}

Parameters

validateQueryParams(searchParams: URLSearchParams, schema: Yup.ObjectSchema)

  • Parameter: searchParams - Type: URLSearchParams - Description: Extracted from new URL(req.url).
  • Parameter: schema - Type: Yup.ObjectSchema - Description: A Yup schema to validate the inputs.

Returns:

  • isValid: boolean - Indicates if the validation passed (true) or failed (false).
  • data: Record<string, any> - Validated query parameters (only available if isValid is true).
  • errors: Record<string, string> - Object containing error messages for each invalid parameter (if invalid).

Examples

Basic Example

import { validateQueryParams } from 'yupinator';
import * as Yup from 'yup';

const schema = Yup.object({
  first_name: Yup.string().required('First name is required'),
  last_name: Yup.string().required('Last name is required'),
});

const { searchParams } = new URL('http://example.com?first_name=John&last_name=Doe');

validateQueryParams(searchParams, schema).then(({ isValid, data, errors }) => {
  if (isValid) {
    console.log('Validated data:', data);
  } else {
    console.error('Validation errors:', errors);
  }
});

Real-World Example (Next.js API Route)

import { NextRequest } from 'next/server';
import { validateQueryParams } from 'yupinator';
import * as Yup from 'yup';

export async function GET(req: NextRequest) {
  const schema = Yup.object({
    first_name: Yup.string().required('First name is required'),
    age: Yup.number().positive('Age must be positive').required('Age is required'),
  });

  const { searchParams } = new URL(req.url);

  const { isValid, data, errors } = await validateQueryParams(searchParams, schema);

  if (!isValid) {
    return new Response(JSON.stringify({ errors }), { status: 400, headers: { 'Content-Type': 'application/json' } });
  }

  return new Response(JSON.stringify({ message: 'Success', data }), {
    status: 200,
    headers: { 'Content-Type': 'application/json' },
  });
}

Error Handling Example

import { validateQueryParams } from 'yupinator';
import * as Yup from 'yup';

const schema = Yup.object({
  email: Yup.string().email('Invalid email address').required('Email is required'),
  age: Yup.number().min(18, 'You must be at least 18 years old').required('Age is required'),
});

const { searchParams } = new URL('http://example.com?email=invalid-email');

validateQueryParams(searchParams, schema).then(({ isValid, data, errors }) => {
  if (!isValid) {
    console.log('Errors:', errors);
  }
});

FAQ

Why Use URLSearchParams?

URLSearchParams is a native browser and Node.js API that makes parsing query strings clean and consistent. It integrates seamlessly with new URL(req.url) in server-side frameworks like Next.js, Express, and more.

What Happens When Validation Fails?

When validation fails:

  • The isValid field will be false.
  • The errors field will contain an object mapping invalid parameters to their respective error messages.

Can I Use This for Other Input Validation?

While Yupinator is optimized for query parameters (URLSearchParams), the core logic can be adapted for any input that maps to a Record<string, any>.


License

MIT License