{"id":22096553,"url":"https://github.com/uphold/validator.js-validate","last_synced_at":"2025-03-24T00:57:45.479Z","repository":{"id":19196989,"uuid":"84583240","full_name":"uphold/validator.js-validate","owner":"uphold","description":"Opinionated object validation function based on validator.js","archived":false,"fork":false,"pushed_at":"2023-11-28T02:50:08.000Z","size":364,"stargazers_count":0,"open_issues_count":9,"forks_count":1,"subscribers_count":23,"default_branch":"master","last_synced_at":"2024-04-14T15:39:46.831Z","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/uphold.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2017-03-10T17:14:03.000Z","updated_at":"2017-03-28T14:08:38.000Z","dependencies_parsed_at":"2024-12-02T00:30:32.379Z","dependency_job_id":null,"html_url":"https://github.com/uphold/validator.js-validate","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uphold%2Fvalidator.js-validate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uphold%2Fvalidator.js-validate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uphold%2Fvalidator.js-validate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uphold%2Fvalidator.js-validate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uphold","download_url":"https://codeload.github.com/uphold/validator.js-validate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245191639,"owners_count":20575248,"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-12-01T04:11:36.625Z","updated_at":"2025-03-24T00:57:45.460Z","avatar_url":"https://github.com/uphold.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# validator.js-validate\n\nOpinionated object validation function based on [validator.js](https://github.com/guillaumepotier/validator.js).\n\n## Status\n\n[![npm version][npm-image]][npm-url] ![node version][node-image] [![build status][travis-image]][travis-url]\n\n## Installation\n\nInstall the package via `yarn`:\n\n```sh\n$ yarn add validator.js-validate\n```\n\nor via `npm`:\n\n```sh\n$ npm install validator.js-validate --save\n```\n\n## Usage\n\n### Create validate function\n\nThis module exports a function that creates a validate function, for instance:\n\n```js\nimport createValidateFunction from 'validator.js-validate';\n\nconst validate = createValidateFunction();\n```\n\nThe created validate function works just like [validating an object](https://github.com/guillaumepotier/validator.js#validate-an-object), but replaces the last `groups` argument with an options object:\n\n```\nvalidate(data[Object], constraint[Object|Constraint], options[Object])\n```\n\n### Options\n\n#### `mask` (Default: `true`)\n\nReturns given data masked with given constraint keys:\n\n```js\nconst data = { foo: 'bar', qux: 'qix' };\nconst constraint = { foo: is.equalTo('bar') };\n\nconsole.log(validate(data, constraint));\n// { foo: 'bar' }\n\nconsole.log(validate(data, constraint, { mask: false }));\n// true\n```\n\n#### `throws` (Default: `true`)\n\nThrows a new error when validation fails. To enable this option you must pass an error class when creating the validate function as argument.\n\nThis error constructor should be prepared to receive violations as argument, for example:\n\n```js\nimport StandardError from 'standard-error';\nimport createValidateFunction from 'validator.js-validate';\n\nclass ValidationFailedError extends StandardError {\n  constructor(errors) {\n    super({ errors });\n  }\n}\n\nconst validate = createValidateFunction(ValidationFailedError);\nconst data = { foo: 'bar' };\nconst constraint = { foo: is.equalTo('biz') };\n\ntry {\n  validate(data, constraint);\n} catch (e) {\n  console.log(e);\n  // ValidationFailedError {\n  //   errors: {\n  //     foo: [{\n  //       __class__: 'Violation',\n  //       assert: {\n  //         __class__: 'EqualTo',\n  //         ...\n  //       }\n  //     }]\n  //   }\n  // }\n  }\n}\n\nconsole.log(validate(data, constraint, { throws: false }));\n// {\n//   foo: [{\n//     __class__: 'Violation',\n//     assert: {\n//       __class__: 'EqualTo',\n//       ...\n//     }\n//   }]\n// }\n```\n\n#### `groups`\n\nUse this option to validate with [validation groups](https://github.com/guillaumepotier/validator.js#validation-groups):\n\n```js\nconst data = { foo: 'bar' };\nconst constraint = { foo: [is('bar').EqualTo('bar'), is('biz').equalTo('biz')] };\n\nconsole.log(validate(data, constraint, { groups: 'biz' }));\n// {\n//   foo: [{\n//     __class__: 'Violation',\n//     assert: {\n//       __class__: 'EqualTo',\n//       ...\n//     }\n//   }]\n// }\n\nconsole.log(validate(data, constraint, { groups: 'bar' }));\n// { foo: 'bar' }\n```\n\n## Test suite\n\nUse the `test` script to run the test suite:\n\n```sh\n$ yarn test\n```\n\nTo test check coverage use the `cover` script:\n\n```sh\n$ yarn cover\n```\n\nA full coverage report will be generated on the *coverage* folder.\n\n## Release\n\n```sh\n$ yarn release [\u003cversion\u003e | major | minor | patch]\n```\n\n## License\n\n[MIT](/LICENSE)\n\n[node-image]: https://img.shields.io/node/v/validator.js-validate.svg?style=flat-square\n[npm-image]: https://img.shields.io/npm/v/validator.js-validate.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/validator.js-validate\n[travis-image]: https://img.shields.io/travis/uphold/validator.js-validate/master.svg?style=flat-square\n[travis-url]: https://travis-ci.org/uphold/validator.js-validate\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuphold%2Fvalidator.js-validate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuphold%2Fvalidator.js-validate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuphold%2Fvalidator.js-validate/lists"}