{"id":13454889,"url":"https://github.com/jellydn/next-validations","last_synced_at":"2025-04-05T02:07:51.399Z","repository":{"id":37072522,"uuid":"364000393","full_name":"jellydn/next-validations","owner":"jellydn","description":"NextJS API Validations,  support Zod, Yup, Fastest-Validator, Joi, and more","archived":false,"fork":false,"pushed_at":"2025-04-02T17:22:39.000Z","size":8490,"stargazers_count":52,"open_issues_count":4,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T18:29:26.918Z","etag":null,"topics":["api","fastest-validator","hacktoberfest","joi-validation","nextjs","validations","yup-validation","zod"],"latest_commit_sha":null,"homepage":"https://next-validations.productsway.com/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jellydn.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["jellydn"],"ko_fi":"dunghd","buy_me_a_coffee":"dunghd","polar":"jellydn"}},"created_at":"2021-05-03T16:57:47.000Z","updated_at":"2025-04-02T10:30:02.000Z","dependencies_parsed_at":"2023-10-27T20:45:16.913Z","dependency_job_id":"2753999f-af1b-47c1-ae56-115d5e110129","html_url":"https://github.com/jellydn/next-validations","commit_stats":{"total_commits":859,"total_committers":8,"mean_commits":107.375,"dds":0.5715948777648429,"last_synced_commit":"1b19e15e2b3a9da96d1042cc68b5c39b8987d17c"},"previous_names":[],"tags_count":41,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jellydn%2Fnext-validations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jellydn%2Fnext-validations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jellydn%2Fnext-validations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jellydn%2Fnext-validations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jellydn","download_url":"https://codeload.github.com/jellydn/next-validations/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247276163,"owners_count":20912288,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","fastest-validator","hacktoberfest","joi-validation","nextjs","validations","yup-validation","zod"],"created_at":"2024-07-31T08:00:58.997Z","updated_at":"2025-04-05T02:07:51.382Z","avatar_url":"https://github.com/jellydn.png","language":"TypeScript","funding_links":["https://github.com/sponsors/jellydn","https://ko-fi.com/dunghd","https://buymeacoffee.com/dunghd","https://polar.sh/jellydn","https://paypal.me/dunghd","https://www.buymeacoffee.com/dunghd"],"categories":["TypeScript"],"sub_categories":[],"readme":"# Welcome to next-validations 👋\n\n\u003c!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --\u003e\n\n[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors-)\n\n\u003c!-- ALL-CONTRIBUTORS-BADGE:END --\u003e\n\n[![Version](https://img.shields.io/npm/v/next-validations.svg)](https://npmjs.org/package/next-validations)\n[![Downloads/week](https://img.shields.io/npm/dw/next-validations.svg)](https://npmjs.org/package/next-validations)\n![Prerequisite](https://img.shields.io/badge/node-%3E%3D10-blue.svg)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](#)\n[![Twitter: jellydn](https://img.shields.io/twitter/follow/jellydn.svg?style=social)](https://twitter.com/jellydn)\n\n\u003e NextJS API Validations\n\n## 🏠 [Homepage](https://github.com/jellydn/next-validations)\n\n### ✨ [Demo](https://next-validations-demo.productsway.com/)\n\n![https://gyazo.com/bf4582f7b7aa0f0ae67c4cc337c4e974.gif](https://gyazo.com/bf4582f7b7aa0f0ae67c4cc337c4e974.gif)\n\n## Prerequisites\n\n- node \u003e=18\n- nextjs \u003e= 9\n\n## Install\n\n```sh\nyarn add next-validations\n```\n\n## Features\n\n- **Support for Multiple Validation Libraries**: This package is designed to work seamlessly with a variety of popular validation libraries. These include [Yup](https://github.com/jquense/yup), [Fastest-Validator](https://github.com/icebob/fastest-validator), [Joi](https://github.com/sideway/joi), [Zod](https://github.com/colinhacks/zod), and [Valibot](https://github.com/fabian-hiller/valibot). This means you can choose the library that best suits your project's needs.\n\n- **Integration with TypeSchema**: `next-validations` integrates with [TypeSchema - Universal adapter for TypeScript schema validation](https://typeschema.com/). This allows for even more flexibility and compatibility with additional validation libraries.\n\n## Usage\n\n### Validation of multiple modes\n\n```sh\nyarn add yup joi next-validations @typeschema/yup @typeschema/yoi\n```\n\n```typescript\nimport Joi from 'joi';\nimport { NextApiRequest, NextApiResponse } from 'next';\nimport { createRouter } from 'next-connect';\nimport { withValidations } from 'next-validations';\nimport * as yup from 'yup';\n\nconst querySchema = yup.object().shape({\n  type: yup.string().oneOf(['email', 'sms']).required(),\n});\n\nconst validateQuery = {\n  schema: querySchema,\n  mode: 'query',\n} as const;\n\nconst bodySchema = Joi.object({\n  phone: Joi.string().required(),\n  email: Joi.string().email().required(),\n  name: Joi.string().required(),\n});\n\nconst validateBody = {\n  schema: bodySchema,\n  mode: 'body',\n} as const;\n\nconst validate = withValidations([validateQuery, validateBody]);\n\nconst handler = (req: NextApiRequest, res: NextApiResponse) =\u003e {\n  res.status(200).json({ ...req.body, ...req.query });\n};\n\nexport default connect().post(validate(), handler);\n```\n\n### Validate custom API endpoint with Yup\n\n```sh\nyarn add yup next-validations @typeschema/yup\n```\n\n```typescript\nimport { NextApiRequest, NextApiResponse } from 'next';\nimport { withValidation } from 'next-validations';\nimport * as yup from 'yup';\n\nconst schema = yup.object().shape({\n  name: yup.string().required(),\n});\n\nconst validate = withValidation({\n  schema,\n  mode: 'query',\n});\n\nconst handler = (req: NextApiRequest, res: NextApiResponse) =\u003e {\n  res.status(200).json(req.query);\n};\n\nconst router = createRouter();\n\nrouter.post(validate(), handler);\n\nexport default router.handler({\n  onError: (err, _req, _event) =\u003e {\n    return new NextResponse('Something broke!', {\n      status: (err as any)?.statusCode ?? 500,\n    });\n  },\n});\n```\n\n### Validate custom API endpoint with Zod\n\n```sh\nyarn add zod next-validations @typeschema/zod\n```\n\n```typescript\nimport { NextApiRequest, NextApiResponse } from 'next';\nimport { withValidation } from 'next-validations';\nimport { z } from 'zod';\n\nconst schema = z.object({\n  username: z.string().min(6),\n});\n\nconst validate = withValidation({\n  schema,\n  mode: 'body',\n});\n\nconst handler = (req: NextApiRequest, res: NextApiResponse) =\u003e {\n  res.status(200).json(req.body);\n};\n\nexport default validate(handler);\n```\n\n### Validate custom API endpoint with Valibot\n\n```sh\nyarn add valibot next-validations @typeschema/valibot\n```\n\n```typescript\nimport { NextApiRequest, NextApiResponse } from 'next';\nimport { withValidation } from 'next-validations';\nimport * as valibot from 'valibot';\n\nconst schema = valibot.object({\n  name: valibot.string([valibot.minLength(4)]),\n});\n\nconst validate = withValidation({\n  schema,\n  mode: 'query',\n});\n\nconst handler = (req: NextApiRequest, res: NextApiResponse) =\u003e {\n  res.status(200).json(req.query);\n};\n\nexport default validate(handler);\n```\n\n### Validate custom API endpoint with fastest-validator\n\n```sh\nyarn add fastest-validator next-validations @typeschema/fastest-validator\n```\n\n```typescript\nimport { NextApiRequest, NextApiResponse } from 'next';\nimport { withValidation } from 'next-validations';\n\nconst schema = {\n  name: { type: 'string', min: 3, max: 255 },\n  email: { type: 'email' },\n  age: 'number',\n};\n\nconst validate = withValidation({\n  // This is fastest-validator schema, the type is not working nicely with TypeScript\n  schema: schema as any,\n  mode: 'body',\n});\n\nconst handler = (req: NextApiRequest, res: NextApiResponse) =\u003e {\n  res.status(200).json(req.body);\n};\n\nexport default validate(handler);\n```\n\n### Validate custom API endpoint with joi\n\n```sh\nyarn add joi next-connect next-validations @typeschema/joi\n```\n\n```typescript\nimport Joi from 'joi';\nimport { NextApiRequest, NextApiResponse } from 'next';\nimport { createRouter } from 'next-connect';\nimport { withValidation } from 'next-validations';\n\nconst schema = Joi.object({\n  dob: Joi.date().iso(),\n  email: Joi.string().email().required(),\n  name: Joi.string().required(),\n});\n\nconst validate = withValidation({\n  schema,\n  mode: 'body',\n});\n\nconst handler = (req: NextApiRequest, res: NextApiResponse) =\u003e {\n  res.status(200).json(req.body);\n};\n\nconst router = createRouter();\n\nrouter.post(validate(), handler);\n\nexport default router.handler({\n  onError: (err, _req, _event) =\u003e {\n    return new NextResponse('Something broke!', {\n      status: (err as any)?.statusCode ?? 500,\n    });\n  },\n});\n```\n\n## Run tests\n\n```sh\nyarn test\n```\n\n## Author\n\n👤 **Huynh Duc Dung**\n\n- Website: https://productsway.com/\n- Twitter: [@jellydn](https://twitter.com/jellydn)\n- Github: [@jellydn](https://github.com/jellydn)\n\n## Show your support\n\nGive a ⭐️ if this project helped you!\n\n[![kofi](https://img.shields.io/badge/Ko--fi-F16061?style=for-the-badge\u0026logo=ko-fi\u0026logoColor=white)](https://ko-fi.com/dunghd)\n[![paypal](https://img.shields.io/badge/PayPal-00457C?style=for-the-badge\u0026logo=paypal\u0026logoColor=white)](https://paypal.me/dunghd)\n[![buymeacoffee](https://img.shields.io/badge/Buy_Me_A_Coffee-FFDD00?style=for-the-badge\u0026logo=buy-me-a-coffee\u0026logoColor=black)](https://www.buymeacoffee.com/dunghd)\n\n## Star History\n\n[![Star History Chart](https://api.star-history.com/svg?repos=jellydn/next-validations\u0026type=Date)](https://star-history.com/#jellydn/next-validations\u0026Date)\n\n## Contributors ✨\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://productsway.com/\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/870029?v=4?s=100\" width=\"100px;\" alt=\"Dung Duc Huynh (Kaka)\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eDung Duc Huynh (Kaka)\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/jellydn/next-validations/commits?author=jellydn\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"https://github.com/jellydn/next-validations/commits?author=jellydn\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"http://sferadev.com\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/2181866?v=4?s=100\" width=\"100px;\" alt=\"Alexis Rico\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eAlexis Rico\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/jellydn/next-validations/commits?author=SferaDev\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/decs\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/601381?v=4?s=100\" width=\"100px;\" alt=\"André Costa\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eAndré Costa\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/jellydn/next-validations/commits?author=decs\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-restore --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjellydn%2Fnext-validations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjellydn%2Fnext-validations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjellydn%2Fnext-validations/lists"}