next-yup
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

next-yup

Simple validation for Next.js API routes using Yup

Install

npm install next-yp
# or
yarn add next-yup

Usage

Get started by creating a Yup object schema and wrapping your API route function with the withYup wrapper:

import withYup from 'next-yup';
import * as yup from 'yup';

export const catSchema = yup.object().shape({
  catName: yup.string().required(),
  isHungry: yup.boolean().required(),
});

export default withYup()({ query: catSchema }, (req, res, data) => {
  // validated data from query parameters, with attempted type cast to match schema
  const { query } = data;
  return res.json({ query });
});

// GET http://localhost:3000/api/query?catName=bob&isHungry=true
// -> { "query": { "isHungry": true, "catName": "bob" } }

Dependencies (0)

    Dev Dependencies (13)

    Package Sidebar

    Install

    npm i next-yup

    Weekly Downloads

    72

    Version

    1.1.0

    License

    MIT

    Unpacked Size

    216 kB

    Total Files

    15

    Last publish

    Collaborators

    • iffa