All Projects → codecoolture → next-joi

codecoolture / next-joi

Licence: MIT license
Validate NEXT.js API Routes with joi

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to next-joi

toi
A TypeScript validation library capable of inferring types
Stars: ✭ 25 (-77.48%)
Mutual labels:  joi, validation-schema
simple-developer-portfolio-website
✈️ A simple portfolio for developers to showcase their skills and projects. Blog and tutorial at freeCodeCamp.
Stars: ✭ 265 (+138.74%)
Mutual labels:  vercel
express-mongoose-es8-rest-api
A Boilerplate for developing Rest api's in Node.js using express with support for ES6,ES7,ES8 ,Mongoose,JWT for authentication,Standardjs for linting
Stars: ✭ 20 (-81.98%)
Mutual labels:  joi
api-with-express
🌈 API with Express
Stars: ✭ 25 (-77.48%)
Mutual labels:  joi
joi-to-typescript
Convert Joi Schemas to TypeScript interfaces
Stars: ✭ 73 (-34.23%)
Mutual labels:  joi
next-banner
🖼️ Generate Open Graph images for Next.js on build
Stars: ✭ 45 (-59.46%)
Mutual labels:  vercel
express-ts-api-boilerplate
Express TypeScript API Boilerplate
Stars: ✭ 15 (-86.49%)
Mutual labels:  joi
Portfolio
My portfolio (+ template) built using Next.js
Stars: ✭ 88 (-20.72%)
Mutual labels:  vercel
calculator-bot
Simple Serverless Telegram Bot on vercel.com
Stars: ✭ 44 (-60.36%)
Mutual labels:  vercel
datasette-publish-vercel
Datasette plugin for publishing data using Vercel
Stars: ✭ 30 (-72.97%)
Mutual labels:  vercel
website
My personal website.
Stars: ✭ 29 (-73.87%)
Mutual labels:  vercel
bifrost
🌉 The rainbow bridge. URL shortener for Vercel.
Stars: ✭ 28 (-74.77%)
Mutual labels:  vercel
amazin
A MERN-stack app for eCommerce platform, Webshop, Web Store. Storybook: https://www.amazin.one/ Alternative: https://ntrix.github.io/amazin-story
Stars: ✭ 27 (-75.68%)
Mutual labels:  vercel
task-manager
Task Manager App
Stars: ✭ 19 (-82.88%)
Mutual labels:  joi
vercel-bref
▲ Vercel bref runtime • brefphp • vercel-bref
Stars: ✭ 25 (-77.48%)
Mutual labels:  vercel
wily
Build Node.js APIs from the command line (Dead Project 😵)
Stars: ✭ 14 (-87.39%)
Mutual labels:  joi
blog
My personal blog.
Stars: ✭ 13 (-88.29%)
Mutual labels:  vercel
Aya
your globally distributed waifu storage
Stars: ✭ 32 (-71.17%)
Mutual labels:  vercel
vercel-toast
💬 Framework-agnostic vercel design's toast component (≈1KB Gzipped)
Stars: ✭ 67 (-39.64%)
Mutual labels:  vercel
reach-schema
Functional schema-driven JavaScript object validation library.
Stars: ✭ 34 (-69.37%)
Mutual labels:  validation-schema

next-joi

Validate NEXT.js API Routes with joi 😄

github action badge

Install

yarn add next-joi

This package does not bundle with next.js or joi, so you will need to install them separately.

Getting started

How does it work?

The validation function will check the incoming request against the defined validation schemas. If the request does not comply with the schemas, it will be aborted immediately, and (by default) a 400 BAD REQUEST response will be returned. It is possible to customize this error handling by passing a custom onValidationError function to the primary factory function.

lib/middlewares/validation.ts

import withJoi from "next-joi";

export default withJoi({
  onValidationError: (_, res) => {
    res.status(400).end();
  },
});

Working with NEXT.js API Routes

If you are using standard NEXT.js API Routes, you may use the validation function to wrap your route definition and pass along the validation schema:

import Joi from "joi";

import validate from "/lib/middlewares/validation";

const schema = Joi.object({
  birthdate: Joi.date().iso(),
  email: Joi.string().email().required(),
  name: Joi.string().required(),
});

export default validate({ body: schema }, (req, res) => {
  // This function will be only executed if the incoming request complies
  // with the validation schema defined above.
});

NEXT.js & connect-like middlewares

If your routes are powered by using a package such as next-connect, you can still use next-joi! The middleware function is ready to work with connect just out-of-the-box:

import Joi from "joi";
import connect from "next-connect";

import validate from "/lib/middlewares/validation";

const schema = Joi.object({
  birthdate: Joi.date().iso(),
  email: Joi.string().email().required(),
  name: Joi.string().required(),
});

export default connect().post(validate({ body: schema }), (req, res) => {
  // This function will be only executed if the incoming request complies
  // with the validation schema defined above.
}))

API

withJoi(config?) => validate

This factory function may optionally receive a configuration object. It will return the actual validation function (validate) that can be used as API route middleware.

config

Optional

If omitted, next-joi will use a default configuration.

config.onValidationError

Required

Custom error function to handle validation errors. It will receive the API request, response, and validation error.

import withJoi from "next-joi";

export default withJoi({
  onValidationError: (req, res, error) => {
    res.status(400).end();
  },
});

validate(schemas, handler)

The validate function has support to check the following request fields: body, headers and query. The first argument for this function should always be an object with the desired validation schemas.

schemas

Required

Even if empty, this argument is required.

schemas.body

Optional

A valid joi schema.

schemas.headers

Optional

Note: since most of the time, you may receive more headers than expected, it is a good practice to make this schema always support unknown keys. Otherwise, the validation will fail.

A valid joi schema.

schemas.query

Optional

A valid joi schema.

handler

Optional

A valid next API Route handler. If you are using the validate function without a connect-like middleware engine, this argument becomes mandatory.

Example:

const handler = function (req: NextApiRequest, res: NextApiResponse) {
  // implementation
};

export default validate({}, handler);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].