{"_id":"@alamat/joi-errors-for-forms","_rev":"1-d9034f063b33f4a3bfc2de993a57e300","name":"@alamat/joi-errors-for-forms","dist-tags":{"latest":"0.2.7"},"versions":{"0.2.7":{"name":"@alamat/joi-errors-for-forms","version":"0.2.7","description":"Convert Joi errors to form UI or Mongoose schemas, optionally changing the message text.","main":"index.js","scripts":{"test":"node test/test1.js"},"repository":{"type":"git","url":"git+https://github.com/alamat/joi-errors-for-forms.git"},"keywords":["joi","error","errors","form","forms","i18n"],"author":{"name":"John Szwaronek","email":"johnsz9999@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/alamat/joi-errors-for-forms/issues"},"homepage":"https://github.com/alamat/joi-errors-for-forms#readme","engines":{"node":">4.2.4"},"gitHead":"526c240feabc3784dc001bd334e3c51e74ee96b9","_id":"@alamat/joi-errors-for-forms@0.2.7","_npmVersion":"6.4.1","_nodeVersion":"8.12.0","_npmUser":{"name":"dehamzah","email":"dehamzah@gmail.com"},"dist":{"integrity":"sha512-IJrfFDeGC1Ma6GLh1nQ1Muq6zhfkc6p3T8oZfbauzw+Hdf+qCnP29weVgxYjMUBKu4JEP3nWYrBhCDd4oUY0uA==","shasum":"7c5674dce0f9456c179a273cd21d301971b65d05","tarball":"https://registry.npmjs.org/@alamat/joi-errors-for-forms/-/joi-errors-for-forms-0.2.7.tgz","fileCount":6,"unpackedSize":16285,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcb5+YCRA9TVsSAnZWagAACdoQAJsW/vdjPLP07VTUX6y9\nNjV95vLD+WXmOCpN6DPAyhc2IU5MJqMvEZI3aSN34M1SVpB76Jpkle6i3/7Q\n4OM1EEcg1nR7wnVgkP1IcRssoojn1bxXd9O36Kn5rbfwOs8XO28A/64fxyAL\nvqc1aTvT3Q/bZ7t2mAAJ4eSMxopAnZCgfU1LWfDp5WvlStKu/jLRPSsR4rEI\noEzSI8rJlmWWAvikz9vdeXSKypNPQp+JlJZWh/SEicyQsAWXhR48JrI4Uszn\nqgswAlilgG0u0385uhH+KYng6uC2h6to5CN0PuCquj5CHTHwSB/NCBxECFjg\nhlNvSYTXTp6w+hucn8ERrxHiI/oNQydGheTh85cMOpr572ydR5gmaGF8h9bk\nhPVFP27Re/mbJZP+4jWTgWmsa6Pfi021clp6PtJUYshnh1wgubWt+fSDpiXX\nQD+WeHAN1ozUam9lZpqzZf+dZzLNy8om3GJbEMDKlvxnjcowep10mU1ETpCa\n7IokR2/G2Fpd6UICMdC2Ctj8tG0vo+ORId1iQgsQFqErq2MZ9xIb3X12IxW3\nBFueAcb4xrvZQNHBSt8SMvZUeKV1bScLG2wNMQi6CzwEXRwtMvS3inoryl+W\ncLFTBAQ/k2xkdoD0enMZnUohwnA2AsZuytV3OlBdoLvMxQaDNovLgLhraJjY\nOIMU\r\n=m6vA\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDU2XN1uQrEoA+qE6L56dhXNrGpL3zFCXs/B5WqWt8B/gIhAKJoq1T9iGkczqGznwjegW50Acked5ZCL4CqREQ7t/Sh"}]},"maintainers":[{"name":"dehamzah","email":"dehamzah@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/joi-errors-for-forms_0.2.7_1550819223627_0.12387409532736182"},"_hasShrinkwrap":false}},"time":{"created":"2019-02-22T07:07:03.460Z","0.2.7":"2019-02-22T07:07:03.752Z","modified":"2022-04-04T12:32:52.150Z"},"maintainers":[{"name":"dehamzah","email":"dehamzah@gmail.com"}],"description":"Convert Joi errors to form UI or Mongoose schemas, optionally changing the message text.","homepage":"https://github.com/alamat/joi-errors-for-forms#readme","keywords":["joi","error","errors","form","forms","i18n"],"repository":{"type":"git","url":"git+https://github.com/alamat/joi-errors-for-forms.git"},"author":{"name":"John Szwaronek","email":"johnsz9999@gmail.com"},"bugs":{"url":"https://github.com/alamat/joi-errors-for-forms/issues"},"license":"MIT","readme":"## joi-errors-for-forms\n\nIn order to keep a consistent validation API in your apps,\nconvert the error objects returned by [Joi](https://github.com/hapijs/joi) to either\n\n- the `{ name1: text, name2: text }` schema commonly used with form UIs, or\n- the Mongoose schema\n`{ name1: { message: ..., name: 'ValidatorError', path: ..., type: ... } }`.\n\nThe Joi error messages may be replaced either for internationalization or clarity.\n\nThe package has no dependencies.\n\n[![Build Status](https://travis-ci.org/eddyystop/joi-errors-for-forms.svg?branch=master)](https://travis-ci.org/eddyystop/joi-errors-for-forms)\n\n## Code Examples\n\nFor the following Joi schema:\n\n```javascript\nconst Joi = require('joi');\nconst name = Joi.string().trim().regex(/^[\\sa-zA-Z0-9]{5,30}$/).required();\nconst password = Joi.string().trim().min(2).max(30).required();\nconst schema = Joi.object().keys({\n  name,\n  password,\n  confirmPassword: password.label('Confirm password'),\n});\nconst joiOptions = { convert: true, abortEarly: false };\n\nconst values = { name: 'j', password: 'z', confirmPassword: 'z' };\n```\n\n(1) Convert the Joi messages to the form UI schema, retaining the original message text.\n\n```javascript\nconst joiToForms = require('joi-errors-for-forms').form;\nconst convertToForms = joiToForms();\n\nJoi.validate(values, schema, joiOptions, (errs, convertedValues) => {\n  console.log(convertToForms(errs));\n  // { name: '\"name\" with value \"j\" fails to match the required pattern: /^[\\\\sa-zA-Z0-9]{5,30}$/',\n  //   password: '\"password\" length must be at least 2 characters long',\n  //   confirmPassword: '\"Confirm password\" length must be at least 2 characters long'\n  // }\n  // or null if no errors.\n});\n```\n\n\n(2) Convert to the form UI schema. Replace Joi messages using Joi error types. (Recommended.)\n\n```javascript\nconst joiToForms = require('joi-errors-for-forms').form;\nconst convertToForms = joiToForms({\n  'string.min': () => i18n('\"${key}\" must be ${limit} or more chars.'),\n  'string.regex.base': (context) => {\n    switch (context.pattern.toString()) {\n      case /^[\\sa-zA-Z0-9]{5,30}$/.toString():\n        return i18n('\"${key}\" must consist of letters, digits or spaces.');\n    }\n  }\n});\n\nJoi.validate(values, schema, joiOptions, (errs, convertedValues) => {\n  console.log(convertToForms(errs));\n  // { name: '\"name\" must consist of letters, digits or spaces.',\n  //   password: '\"password\" must be 2 or more chars.',\n  //   confirmPassword: '\"Confirm password\" must be 2 or more chars.'\n  // }\n});\n\nfunction i18n(str) { return str; } // internationalization\n\n```\n\nor convert to the Mongoose schema.\n\n```javascript\nconst joiToMongoose = require('joi-errors-for-forms').mongoose;\nconst convertToMongoose = joiToMongoose({\n  ... same as above ...\n});\n\nJoi.validate(values, schema, joiOptions, (errs, convertedValues) => {\n  console.log(convertToMongoose(errs));\n  // { name: \n  //     { message: '\"name\" must consist of letters, digits or spaces.',\n  //       name: 'ValidatorError',\n  //       path: 'name',\n  //       type: 'string.regex.base' },\n  //   password: \n  //     { message: '\"password\" must be 2 or more chars.',\n  //       name: 'ValidatorError',\n  //       path: 'password',\n  //       type: 'string.min' },\n  //   confirmPassword: \n  //     { message: '\"Confirm password\" must be 2 or more chars.',\n  //       name: 'ValidatorError',\n  //       path: 'confirmPassword',\n  //       type: 'string.min' }\n  // }\n});\n\n```\n\nList of substitution tokens. Refer to Joi documentation for more information.\n\n- `${key}` prop name, or label if `.label('...')` was used.\n- `${value}` prop value. Its rudimentally converted to a string.\n- `${pattern}` regex value if `.regex(...)` was involved in the error. Its converted to a string.\n- `${limit}` allowed length of string.\n- `${encoding}` string encoding. Could be `undefined`. Its converted to a string.\n\nNote that `type` retains the Joi value in the Mongoose schema.\nIt is not converted to what Mongoose would return.\n\n\n(3) Replace Joi messages with a generic error message.\n\n```javascript\nconst convertToForms = joiToForms('\"${key}\" is badly formed.');\n\nJoi.validate(values, schema, joiOptions, (errs, convertedValues) => {\n  console.log(convertToForms(errs));\n  // { name: '\"name\" is badly formed.',\n  //   password: '\"password\" is badly formed.',\n  //   confirmPassword: '\"Confirm password\" is badly formed.'\n  // }\n});\n\n```\n\n\n(4) Replace Joi messages, by searching for substrings in Joi messages.\n\n```javascript\nconst convertToForms = joiToForms([\n  { regex: 'at least 2 characters long',\n    message: '\"${key}\" must be 2 or more chars.'\n  },\n  { regex: /required pattern/,\n    message: '\"${key}\" is badly formed.'\n  }\n]);\n\nJoi.validate(values, schema, joiOptions, (errs, convertedValues) => {\n  console.log(convertToForms(errs));\n  // { name: '\"name\" is badly formed.',\n  //   password: '\"password\" must be 2 or more chars.',\n  //   confirmPassword: '\"Confirm password\" must be 2 or more chars.'\n  // }\n});\n\n```\n\n## Motivation\n\n[Joi](https://github.com/hapijs/joi) is an enterprise strength schema validator and sanitizer\noriginally developed by Walmart.\n\nThe error object it returns, however, usually has to be reformatted for use within web/mobile apps.\nIts error messages may also have to be converted for internationalization or for clarity.\n\nThis package helps with both needs.\n\n## Installation\n\nInstall [Nodejs](https://nodejs.org/en/).\n\nRun `npm install joi-errors-for-forms --save` in your project folder.\n\nYou can then require the package.\n\n```javascript\n// ES5\nvar joiErrorsToForms = require('joi-errors-for-forms');\nvar joiToForms = joiErrorsToForms.form;\nvar joiToMongoose = joiErrorsToForms.mongoose;\n// or ES6\nimport { form as joiToForms, mongoose as joiToMongoose } from 'joi-errors-for-forms';\n```\n\n## API Reference\n\nSee Code Examples.\n\n## Tests\n\n`npm test` to run tests.\n\n## A Note on Internationalization\n\nThe `options` in `Joi.validate(value, schema, options, cb)`supports a\n[`language` option](https://github.com/hapijs/joi/blob/v9.0.0/API.md#validatevalue-schema-options-callback)\nwith which you can change\n[Joi error messages](https://github.com/hapijs/joi/blob/v9.0.0/lib/language.js)\nin bulk.\n\nYou can then internationalize your field names and regex descriptions in the schema, e.g.\n\n```javascript\nJoi.string().regex(/^[\\sa-zA-Z0-9]$/, i18n('letters, number and spaces')).label(i18n('Confirm password'))\n```\n\nThese are suitable methods to internationalize the majority of Joi error messages.\n\n## Contributors\n\n- [eddyystop](https://github.com/eddyystop)\n\n## License\n\nMIT. See LICENSE.\n","readmeFilename":"README.md"}