Skip to content

How to call validate middleware with next-connect? #170

Answered by jellydn
nemanjam asked this question in Q&A
Discussion options

You must be logged in to vote

You could check out the example app on this repository. Here is the example. https://github.com/jellydn/next-validations/blob/main/example/pages/api/contact.ts

import { NextApiRequest, NextApiResponse } from 'next';

import Joi from 'joi';
import connect from 'next-connect';
import { withValidation } from 'next-validations';

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

const validate = withValidation({
  schema,
  type: 'Joi',
  mode: 'body',
});

const handler = (req: NextApiRequest, res: NextApiResponse) => {
  res.status(200).json(req.body);
};

export default connect().post(validate(), h…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@jellydn
Comment options

Answer selected by nemanjam
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #168 on November 27, 2021 20:35.