-
I am using this with import nc, { ncOptions } from 'lib-server/nc';
import { requireAuth } from 'lib-server/middleware/auth';
import { postCreateSchema } from 'lib-server/validation';
const handler = nc(ncOptions);
const validatePostCreate = withValidation({
schema: postCreateSchema,
type: 'Zod',
mode: 'body',
});
handler.post(
requireAuth,
validatePostCreate, // pass it here
async (req: NextApiRequest, res: NextApiResponse) => {
...
// this handler never runs?
res.status(201).json({ post: 'test' });
}
); |
Beta Was this translation helpful? Give feedback.
Answered by
jellydn
Nov 27, 2021
Replies: 2 comments 1 reply
-
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(), handler); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
nemanjam
-
That works, thanks. handler.post(
requireAuth,
validatePostCreate(),
async (req: NextApiRequest, res: NextApiResponse) => {
... |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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