{"id":21353517,"url":"https://github.com/johnkimdev/sails-hook-req-validate-promise","last_synced_at":"2025-03-16T05:13:33.454Z","repository":{"id":140160612,"uuid":"119214471","full_name":"JohnKimDev/sails-hook-req-validate-promise","owner":"JohnKimDev","description":"Promise wrapper of https://github.com/JohnKimDev/sails-hook-req-validate","archived":false,"fork":false,"pushed_at":"2019-01-15T19:15:44.000Z","size":43,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-26T00:42:30.933Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/JohnKimDev.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":"2018-01-28T00:27:51.000Z","updated_at":"2020-01-23T05:36:00.000Z","dependencies_parsed_at":"2023-05-07T11:26:18.153Z","dependency_job_id":null,"html_url":"https://github.com/JohnKimDev/sails-hook-req-validate-promise","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnKimDev%2Fsails-hook-req-validate-promise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnKimDev%2Fsails-hook-req-validate-promise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnKimDev%2Fsails-hook-req-validate-promise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnKimDev%2Fsails-hook-req-validate-promise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JohnKimDev","download_url":"https://codeload.github.com/JohnKimDev/sails-hook-req-validate-promise/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826792,"owners_count":20354221,"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":[],"created_at":"2024-11-22T03:18:12.598Z","updated_at":"2025-03-16T05:13:33.432Z","avatar_url":"https://github.com/JohnKimDev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [DEPRECATION NOTICE] \n\n## sails-hook-req-validate-promise\n\n## Please use [sails-hook-req-validate](https://www.npmjs.com/package/sails-hook-req-validate) for the updated version! There is a brand new version that supports both PROMISE and Non-PROMISE versions with many new features.\n\nI will no longer support this package but I will continue to update the [sails-hook-req-validate](https://www.npmjs.com/package/sails-hook-req-validate) package. \n\n---\n---\n\n\nSails hook for overwrite req.validate request with Promise.\n\nNon-Promise version: https://www.npmjs.com/package/sails-hook-req-validate\n\n```javascript\n  npm install sails-hook-req-validate-promise --save \n```\n\n### req.validate();\n\n\u003e ##### Requirements:\nSails v1.x.x and lodash enabled as global (lodash is enabled by default). \n\n---\n\n### Default Value (when a parameter is not set)\n\n```javascript\n  const params = await req.validate(['fruit', ['string', {default: 'apple'}]); // if 'fruit' doesn't exists, it will be set as 'apple'\n  console.log(params);\n```\n\n```javascript\n  const params = await req.validate([\n    {'fruit', {enum: ['apple', 'organe', 'bannana'], default: 'apple'}},   // also can be used with enum\n    {'username?', 'string' }\n  ]);\n  console.log(params);\n```\n\n---\n\n### Enumeration check\n\n```javascript\n  const params = await req.validate('fruit', {enum: ['apple', 'organe', 'bannana']});\n  console.log(params);\n```\n\n```javascript\n  const params = await req.validate([\n    {'fruit', {enum: ['apple', 'organe', 'bannana']}},\n    {'username?', 'string' },\n    {'nickname?', 'any' }   // any type\n  ]);\n  console.log(params);\n```\n\n```javascript\n  const params = await req.validate([\n    {'fruit', ['string', {enum: ['apple', 'organe', 'bannana']}]},\n    {'username?', 'string' }\n  ]);\n  console.log(params);\n```\n\n---\n\n### Simple Single \u0026 Multple Parameter(s)\nValidates `req.params` for expecting parameter keys and returns `req.badRequest` (400 status code) if any parameter key is missing.\n\n\n```javascript\n  try {\n    const params = await req.validate('id');\n    console.log(params);   // {id: '1234'} \n    // if the validation fails, \"req.badRequest\" will be called and returns Promise.reject\n  } catch (err) {\n    // wil catch Promise.reject here.\n  }\n```\n\nIf you prefer non async-await promise method\n\n```javascript\n  req.validate('id');\n    .then(params =\u003e {  \n      console.log(params);   // {id: '1234'} \n    })\n    .catch(err =\u003e {\n      console.error(err);\n    });\n```\n\n\u003cbr\u003e\n\nDisable `req.badRequest` on error and enable `Promise.reject`\n```javascript\n  const params = await req.validate('id', false);  // \u003c--- if you set the second value as FALSE, req.badRequest will NOT be call when error but it will just return Promise.reject\n```\nNOTE: To disable the default error response, set `false` as the second passing variable.\n\n\u003cbr\u003e\n\n```javascript\n  const params = await req.validate(['id', 'firstname', 'lastname']);\n  console.log(params);   // {id: '1234', firstname: \"John\", lastname: \"Doe\"}\n  // if the validation fails, \"req.badRequest\" will be called and returns Promise.reject\n```\n\n\u003cbr\u003e\n\n### Optional Parameter\nValidates `req.params` for expecting parameter keys and returns `req.badRequest` (400 status code) if any parameter key is missing except optional parameters.\n\n```javascript\n  const params = await req.validate(['id', 'firstname', 'lastname?']);  // lastname is an OPTIONAL field \n  console.log(params);   // {id: '1234', firstname: \"John\", lastname: \"Doe\"}\n  // if the validation fails, \"req.badRequest\" will be called and returns Promise.reject\n```\n\nNOTE: For an optional parameter, just add `?` at the end of the passing parameter key.\n\n\u003cbr\u003e\n\n### Multple Parameters with TYPE filters\nValidates `req.params` for expecting parameter keys and returns `req.badRequest` (400 status code) if any missing parameter key.\n\n```javascript\n  const params = await req.validate([\n    {'id' : 'numeric'},\n    {'firstname' : 'string'}, \n    {'lastname' : 'string'}\n\t\t]);\n  console.log(params);   // {id: '1234', firstname: \"John\", lastname: \"Doe\"}\n```\nSee [Validation Filters](#validation_filters) for more information.\n\n\u003cbr\u003e\n\n### OR Operation\nOR `|` operarion is a new addition to 0.2.x version. It can be applied to either *required* or *optional* parameter.\n```javascript\n  const params = await rreq.validate(\n      {'id': 'string | numeric'},   // 'numeric | string', 'numeric|string' or 'numeric| string' are OK. Space will be ignored\n      {'usernameOrEmail': 'string | numeric | email'}\n    );```\n\u003cbr\u003e\n\n\n### Multple Parameters with TYPE filters \u0026 CONVERTION filters\nValidates `req.params` for expecting parameter keys and returns `req.badRequest` (400 status code) if any missing parameter key.\n\n```javascript\n  const params = await rreq.validate([\n\t\t{'id' : 'numeric'},\n\t\t{'firstname' : ['string', 'toUppercase']}, \n\t\t{'lastname' : ['string', 'toLowercase']}\n\t\t]);\n  console.log(params);   // {id: '1234', firstname: \"John\", lastname: \"Doe\"}\n```\nNOTE: All CONVERTION filters start with `to`, for example: toUppercase, toBoolean.\n\nSee [Validation Filters](#validation_filters) and [Conversion Filters](#conversion_filters) for more information.\n\n\u003cbr\u003e\n\n### - Additional Example (Combining All Above Examples in One) \nValidates `req.params` for expecting parameter keys and returns `req.badRequest` (400 status code) if any missing parameter key.\n\n```javascript\n  const params = await req.validate([\n\t\t{'id' : 'numeric'},                             // (required) 'id' param as NUMERIC type\n\t\t'phone?',                                       // (optional) 'phone' as ANY type\n\t\t{'website?': 'url'},                            // (optional) 'website' as URL type\n\t\t{'firstname' : ['string', 'toUppercase']},      // (required) 'firstname' as STRING type and convert to UPPERCASE\n\t\t{'department' : ['string', 'lowercase']}        // (required) 'department' as STRING type and must be LOWERCASE input\n\t\t]);\n  console.log(params);   // {id: '1234', firstname: \"John\", lastname: \"Doe\"}\n  // if the validation fails, \"req.badRequest\" will be called and will NOT returns Promise.reject\n```\nSee [Validation Filters](#validation_filters) and [Conversion Filters](#conversion_filters) for more information.\n\n\u003cbr\u003e\n\n### Disable Default Error Response  \nWhen the validation fails, `res.badRequest` will not be sent instead 'false' will be returned.\n\n```javascript\n  try {\n    const params = await req.validate(\n      ['id', 'firstname', 'lastname?'],   // lastname is an OPTIONAL field \n      false  // \u003c--- if you set the second value as FALSE, req.badRequest will NOT be call \"res.badRequest\" response when error but it will return Promise.reject                         \n    );\n    console.log(params);   // {id: '1234', firstname: \"John\", lastname: \"Doe\"}\n    // ..process\n  } catch (err) {\n    console.error(err);\n    return res.badRequest();    // Make sure to handle badRequest response\n  }\n```\n\nNOTE: To disable the default error response, set `false` as the second passing variable.\n\n\u003cbr\u003e\n\n### \u003ca name=\"validation_filters\"\u003e\u003c/a\u003eValidation Filters\n\n```javascript  \n  any\n  email\n  url\n  ip\n  alpha\n  numeric\n  base64\n  hex\n  hexColor\n  lowercase\n  uppercase\n  string\n  boolean\n  int\n  float\n  date\n  json\n  array\n  object\n  ascii\n  mongoId\n  alphanumeric\n  creditCard\n```\n\n\u003cbr\u003e\n\n### \u003ca name=\"conversion_filters\"\u003e\u003c/a\u003eConversion Filters\n\n```javascript  \n  toLowercase\n  toUppercase\n  toEmail         // Normalize email string \n  toBoolean\n  toDate\n  toInt\n  toFloat\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnkimdev%2Fsails-hook-req-validate-promise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohnkimdev%2Fsails-hook-req-validate-promise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnkimdev%2Fsails-hook-req-validate-promise/lists"}