{"id":15826339,"url":"https://github.com/danybeltran/next-api-validation","last_synced_at":"2025-10-09T18:04:35.463Z","repository":{"id":110079198,"uuid":"341746429","full_name":"danybeltran/next-api-validation","owner":"danybeltran","description":"Request validator implemented in Next.js:)","archived":false,"fork":false,"pushed_at":"2022-12-13T21:34:47.000Z","size":14,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-06T09:44:12.405Z","etag":null,"topics":["api-validator","api-wrapper","next","nextapirequest","nextapiresponse","nextjs"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/next-api-validation","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/danybeltran.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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}},"created_at":"2021-02-24T01:50:29.000Z","updated_at":"2023-05-17T15:44:04.000Z","dependencies_parsed_at":"2023-04-28T15:00:55.736Z","dependency_job_id":null,"html_url":"https://github.com/danybeltran/next-api-validation","commit_stats":{"total_commits":9,"total_committers":1,"mean_commits":9.0,"dds":0.0,"last_synced_commit":"6a4d46181ccfaf617b3f7511e791e049b56192e3"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danybeltran%2Fnext-api-validation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danybeltran%2Fnext-api-validation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danybeltran%2Fnext-api-validation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danybeltran%2Fnext-api-validation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danybeltran","download_url":"https://codeload.github.com/danybeltran/next-api-validation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232548601,"owners_count":18540145,"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-validator","api-wrapper","next","nextapirequest","nextapiresponse","nextjs"],"created_at":"2024-10-05T09:44:09.959Z","updated_at":"2025-10-09T18:04:30.431Z","avatar_url":"https://github.com/danybeltran.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"###  Next API Validation\r\n\r\nThis is basically the same as [serverless-request-validator](https://www.npmjs.com/package/serverless-request-validator) ready be used in Next.js\r\n\r\n### How to use it:\r\n\r\nIt is nice to be able to programmatically define what you want your API endpoint(s) to do deppending on the request method used.\r\n\r\nIn express it's something like:\r\n\r\n```js\r\napp.get((req,res)=\u003e{\r\n    res.send(\"Something\");\r\n})\r\n```\r\n\u003e That will work only when using a `GET` request\r\n\r\nIn Next.js (and Vercel) apps, your API are files in a specific order in the project directory, each file with a default export being the actual handler that will handle that request.\r\n\r\nFirst, install the module:\r\n\r\n```sh\r\nnpm install next-api-validation\r\n```\r\nOr\r\n```sh\r\nyarn add next-api-validation\r\n```\r\n\r\nUsing it in any of your API routes in Next.js:\r\n\r\n```js\r\nimport validation from \"next-api-validation\";\r\n\r\nexport default validation.get((req,res)=\u003e{\r\n    res.send(\"This only accepts GET request\")\r\n})\r\n```\r\n\u003e As you can see, using the `get` method in the `validation` object prevents the handler from being executed if a different request method is used.\r\n\r\nAnd so with other methods:\r\n\r\n\r\n```js\r\n// api/index.js or api/index.ts\r\nimport validation from \"next-api-validation\";\r\n\r\nexport default validation.post((req,res)=\u003e{\r\n    res.send(\"You just sent a POST request\")\r\n})\r\n```\r\n\u003e This handler a POST request\r\n\r\n**What if an endpoint should handle requests using more than one or two methods?**\r\n\r\nCreating a default export of the function should solve that:\r\n\r\n```js\r\n// api/index.js or api/index.ts\r\nimport validate from \"next-api-validation\"\r\n\r\nexport default validate({\r\n    get(req,res){\r\n        res.send(\"A get request\")\r\n    },\r\n    post(req,res){\r\n        res.send(\"I only handle post requests\"))\r\n    },\r\n    put(req,res){\r\n        res.send(\"Did you put something?\")\r\n    }\r\n})\r\n```\r\n\r\nThe previous code handles requests that use three different methods, and calls only the necessary handler. An example of how it can be used:\r\n\r\n```js\r\n// CRUD of a MongoDB Document model\r\n\r\nimport { Post } from \"src/Models\";\r\nimport { connectToDatabase } from \"src/utils\";\r\nimport validate from \"next-api-validation\";\r\n\r\nconnectToDatabase();\r\n\r\nexport default validate({\r\n  get: async (req, res) =\u003e {\r\n    const posts = await Post.find();\r\n    res.send(posts);\r\n  },\r\n  post: async (req, res) =\u003e {\r\n    const newPost = new Post(req.body);\r\n    const saved = await newPost.save();\r\n    res.send(saved);\r\n  },\r\n  put: async (req, res) =\u003e {\r\n    const editedPost = await Post.findByIdAndUpdate(req.body._id, req.body);\r\n    res.send(editedPost);\r\n  },\r\n  delete: async (req, res) =\u003e {\r\n    const deletedPost = await Post.findByIdAndDelete(req.body._id);\r\n    res.send(deletedPost);\r\n  },\r\n});\r\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanybeltran%2Fnext-api-validation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanybeltran%2Fnext-api-validation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanybeltran%2Fnext-api-validation/lists"}