{"id":19606733,"url":"https://github.com/learnwithfair/express-validator","last_synced_at":"2025-07-17T02:36:17.540Z","repository":{"id":238070717,"uuid":"795808452","full_name":"learnwithfair/express-validator","owner":"learnwithfair","description":"express-validator with  [learnwithfair, Learn with fair, Rahatul Rabbi, Md Rahatul Rabbi ,rahatulrabbi]","archived":false,"fork":false,"pushed_at":"2024-05-04T05:54:08.000Z","size":28,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-09T09:24:22.999Z","etag":null,"topics":["express-middleware","express-validation","express-validator","learn-with-fair","learnwithfair","mern","rahatul-rabbi","rahatulrabbi","web-development"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/learnwithfair.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2024-05-04T05:46:51.000Z","updated_at":"2024-05-04T05:54:11.000Z","dependencies_parsed_at":"2024-05-04T06:31:27.871Z","dependency_job_id":"0b177796-431a-4aeb-b2bd-e4a4a0b77262","html_url":"https://github.com/learnwithfair/express-validator","commit_stats":null,"previous_names":["learnwithfair/express-validator"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/learnwithfair%2Fexpress-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/learnwithfair%2Fexpress-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/learnwithfair%2Fexpress-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/learnwithfair%2Fexpress-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/learnwithfair","download_url":"https://codeload.github.com/learnwithfair/express-validator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240894491,"owners_count":19874822,"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":["express-middleware","express-validation","express-validator","learn-with-fair","learnwithfair","mern","rahatul-rabbi","rahatulrabbi","web-development"],"created_at":"2024-11-11T10:07:03.398Z","updated_at":"2025-02-26T16:43:45.869Z","avatar_url":"https://github.com/learnwithfair.png","language":"JavaScript","readme":"# VALIDATION-WITH-EXPRESS-VALIDATOR\n\n[![Youtube][youtube-shield]][youtube-url]\n[![Facebook][facebook-shield]][facebook-url]\n[![Instagram][instagram-shield]][instagram-url]\n[![LinkedIn][linkedin-shield]][linkedin-url]\n\nThanks for visiting my GitHub account!\n\n\u003cimg src =\"screenshot/icon.png\" height = \"200px\" width = \"200px\"/\u003e **Express-validator** is a set of express.js middlewares that wraps the extensive collection of validators and sanitizers offered by validator.js.\n\nIt allows you to combine them in many ways so that, you can validate and sanitize your express requests and offers tools to determine if the request is valid or not, which data was matched according to your validators, and so on. [see-more](https://express-validator.github.io/docs/)\n\n### [Code-Example](https://github.com/learnwithfair/mern-ecommerce-electro-master)\n\n## Source Code \n[download](https://mega.nz/file/xKNl3bib#9NOgw1YEr6uL8z_4Ez56cagl4cnPeoY6bGyGaj4hdwc)\n\n## How to use this project\n\n- step 1: `npm install express-validator`\n- step 2: create the sanitation rules that you want to use for validating the data\n\n### Example of Sanitation Rules\n\n```js\n// validation/auth.js\nconst { check } = require(\"express-validator\");\n\nexports.userRegistrationValidator = [\n  check(\"name\")\n    .trim()\n    .notEmpty()\n    .withMessage(\"Name is missing\")\n    .isLength({ min: 5, max: 31 })\n    .withMessage(\"name must have at least 5 characters\")\n    .isLength({ max: 31 })\n    .withMessage(\"name can have maximum 31characters\"),\n  check(\"email\")\n    .trim()\n    .notEmpty()\n    .withMessage(\"Email is missing\")\n    .isEmail()\n    .withMessage(\"Not a valid email\"),\n  check(\"password\")\n    .trim()\n    .notEmpty()\n    .withMessage(\"Password is missing\")\n    .isLength({ min: 5 })\n    .withMessage(\"password must have at least 5 characters\"),\n  check(\"dob\")\n    .trim()\n    .notEmpty()\n    .withMessage(\"dob is missing\")\n    .isISO8601()\n    .toDate()\n    .withMessage(\"Not a valid dob\"),\n  check(\"image\")\n    .optional()\n    .isString()\n    .withMessage(\"User image must be a string\"),\n];\n\nexports.userLoginValidator = [\n  check(\"email\")\n    .trim()\n    .notEmpty()\n    .withMessage(\"Email is missing\")\n    .isEmail()\n    .withMessage(\"Not a valid email\"),\n  check(\"password\")\n    .trim()\n    .notEmpty()\n    .withMessage(\"Password is missing\")\n    .isLength({ min: 5 })\n    .withMessage(\"password must have at least 5 characters\"),\n];\n\nconst validateUserPasswordUpdate = [\n  check(\"email\")\n    .trim()\n    .notEmpty()\n    .withMessage(\"Email is required\")\n    .isEmail()\n    .withMessage(\"Invalid email address\"),\n  check(\"oldPassword\")\n    .trim()\n    .notEmpty()\n    .withMessage(\"Old Password is required\")\n    .isLength({ min: 6 })\n    .withMessage(\"Password should be at least 6 characters long\"),\n  check(\"newPassword\")\n    .trim()\n    .notEmpty()\n    .withMessage(\"New Password is required\")\n    .isLength({ min: 6 })\n    .withMessage(\"Password should be at least 6 characters long\"),\n  check(\"confirmedPassword\").custom((value, { req }) =\u003e {\n    if (value !== req.body.newPassword) {\n      throw new Error(\"Passwords do not match\");\n    }\n    return true;\n  }),\n];\n\nconst { check } = require(\"express-validator\");\n\nconst validateProduct = [\n  check(\"name\")\n    .trim()\n    .notEmpty()\n    .withMessage(\"Product Name is required\")\n    .isLength({ min: 3, max: 200 })\n    .withMessage(\"Product Name should be at least 3-200 characters long\"),\n  check(\"description\")\n    .trim()\n    .notEmpty()\n    .withMessage(\"Description is required\")\n    .isLength({ min: 3 })\n    .withMessage(\"Description should be at least 3 characters long\"),\n  check(\"price\")\n    .trim()\n    .notEmpty()\n    .withMessage(\"Price is required\")\n    .isFloat({ min: 0 })\n    .withMessage(\"Price must be a positive number\"),\n  check(\"category\").trim().notEmpty().withMessage(\"Category is required\"),\n  check(\"quantity\")\n    .trim()\n    .notEmpty()\n    .withMessage(\"Quantity is required\")\n    .isInt({ min: 1 })\n    .withMessage(\"Quantity must be a positive integer\"),\n];\n\nmodule.exports = { validateProduct };\n```\n\n- step 3: run the validation\n\n```js\n// validation/index.js\nconst { check, validationResult } = require(\"express-validator\");\n\nexports.runValidation = (req, res, next) =\u003e {\n  const errors = validationResult(req);\n  if (!errors.isEmpty()) {\n    let errorsList = errors.array().map((error) =\u003e error.msg);\n    // return res.status(400).json({ errors: errorsList });\n    return res.status(422).send({\n      success: false,\n      message: errors.array()[0].msg,\n    });\n  }\n\n  next();\n\n  // method2\n  // const errors = validationResult(req);\n  // console.log(errors);\n  // if (!errors.isEmpty()) {\n  //   const validationErrors = {};\n  //   const allErrors = errors.array();\n  //   allErrors.forEach((error) =\u003e {\n  //     validationErrors[error.param] = error.msg;\n  //   });\n\n  //   return res.status(400).json({\n  //     validationErrors,\n  //   });\n  // }\n  // return next();\n\n  // method 3\n  // const errors = validationResult(req);\n  // if (!errors.isEmpty()) {\n  //   return res.status(422).json({\n  //     error: errors.array()[0].msg,\n  //   });\n  // }\n  // return next();\n};\n```\n\n## Follow Me\n\n[\u003cimg src='https://cdn.jsdelivr.net/npm/simple-icons@3.0.1/icons/github.svg' alt='github' height='40'\u003e](https://github.com/learnwithfair) [\u003cimg src='https://cdn.jsdelivr.net/npm/simple-icons@3.0.1/icons/facebook.svg' alt='facebook' height='40'\u003e](https://www.facebook.com/learnwithfair/) [\u003cimg src='https://cdn.jsdelivr.net/npm/simple-icons@3.0.1/icons/instagram.svg' alt='instagram' height='40'\u003e](https://www.instagram.com/learnwithfair/) [\u003cimg src='https://cdn.jsdelivr.net/npm/simple-icons@3.0.1/icons/twitter.svg' alt='twitter' height='40'\u003e](https://www.twiter.com/learnwithfair/) [\u003cimg src='https://cdn.jsdelivr.net/npm/simple-icons@3.0.1/icons/youtube.svg' alt='YouTube' height='40'\u003e](https://www.youtube.com/@learnwithfair)\n\n\u003c!-- MARKDOWN LINKS \u0026 IMAGES --\u003e\n\n[youtube-shield]: https://img.shields.io/badge/-Youtube-black.svg?style=flat-square\u0026logo=youtube\u0026color=555\u0026logoColor=white\n[youtube-url]: https://youtube.com/@learnwithfair\n[facebook-shield]: https://img.shields.io/badge/-Facebook-black.svg?style=flat-square\u0026logo=facebook\u0026color=555\u0026logoColor=white\n[facebook-url]: https://facebook.com/learnwithfair\n[instagram-shield]: https://img.shields.io/badge/-Instagram-black.svg?style=flat-square\u0026logo=instagram\u0026color=555\u0026logoColor=white\n[instagram-url]: https://instagram.com/learnwithfair\n[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=flat-square\u0026logo=linkedin\u0026colorB=555\n[linkedin-url]: https://linkedin.com/company/learnwithfair\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flearnwithfair%2Fexpress-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flearnwithfair%2Fexpress-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flearnwithfair%2Fexpress-validator/lists"}