{"id":22226463,"url":"https://github.com/zhw2590582/option-validator","last_synced_at":"2025-07-27T18:31:59.456Z","repository":{"id":57315860,"uuid":"155029487","full_name":"zhw2590582/option-validator","owner":"zhw2590582","description":"A simple option validator","archived":false,"fork":false,"pushed_at":"2020-10-07T14:31:15.000Z","size":47,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-08-10T23:16:00.755Z","etag":null,"topics":["option","scheme","validator"],"latest_commit_sha":null,"homepage":"","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/zhw2590582.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}},"created_at":"2018-10-28T02:51:28.000Z","updated_at":"2023-03-08T03:20:40.000Z","dependencies_parsed_at":"2022-09-12T14:32:32.561Z","dependency_job_id":null,"html_url":"https://github.com/zhw2590582/option-validator","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/zhw2590582%2Foption-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhw2590582%2Foption-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhw2590582%2Foption-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhw2590582%2Foption-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhw2590582","download_url":"https://codeload.github.com/zhw2590582/option-validator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227827796,"owners_count":17825742,"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":["option","scheme","validator"],"created_at":"2024-12-03T00:29:10.354Z","updated_at":"2024-12-03T00:29:10.892Z","avatar_url":"https://github.com/zhw2590582.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# option-validator\n\n[![Build Status](https://travis-ci.com/zhw2590582/option-validator.svg?branch=master)](https://travis-ci.com/zhw2590582/option-validator)\n![version](https://badgen.net/npm/v/option-validator)\n![license](https://badgen.net/npm/license/option-validator)\n![size](https://badgen.net/bundlephobia/minzip/option-validator)\n[![npm Downloads](https://img.shields.io/npm/dt/option-validator.svg)](https://www.npmjs.com/package/option-validator)\n[![dependencies Status](https://david-dm.org/zhw2590582/option-validator/status.svg)](https://david-dm.org/zhw2590582/option-validator)\n\n\u003e A simple option validator\n\n## Install\n\n```\n$ npm install option-validator\n```\n\n```js\nimport optionValidator from 'option-validator';\n```\n\nOR umd builds are also available\n\n```html\n\u003cscript src=\"path/to/option-validator.js\"\u003e\u003c/script\u003e\n```\n\nWill expose the global variable to `window.optionValidator`.\n\n## Usage\n\n- Only one api is to receive a option object and a scheme.\n- If the verification is passed, the original option object will be returned.\n- If the verification fails, an exception will be thrown.\n- Support all js type detection [JS Types](./__test__/testData.js)\n- Support for custom validator functions\n\n```js\nconst option = {\n  a: 1,\n  b: '2',\n  c: {\n    d: () =\u003e null,\n  },\n  g: {\n    h: new Error('error'),\n  },\n  i: [1, '2', () =\u003e 3],\n  j: [1, 2, 3, 4, 5, 6],\n  k: '123456',\n};\n\nconst scheme = {\n  // Shallow verification\n  a: 'number',\n  b: 'string',\n\n  // Deep verification\n  c: {\n    d: 'function',\n  },\n\n  // Validator functions\n  g: {\n    h: (value, type, path) =\u003e {\n      // value --\u003e new Error('error')\n      // type --\u003e 'error'\n      // path --\u003e ['option', 'g', 'h']\n\n      // Returns string mean validation failed, and the string will thrown\n      return 'I will throw an exception';\n\n      // Returns error also mean validation failed, and the error will thrown\n      return new Error('I will throw an exception');\n\n      // Returns true mean verification passed\n      return type === 'error';\n    },\n  },\n\n  // Verify array elements\n  i: ['number', 'string', 'function'],\n\n  // If there is no corresponding validator, the first one is taken by default.\n  j: ['number'],\n\n  // Verify one of them\n  k: 'number|string',\n};\n\noptionValidator(option, scheme);\n```\n\n## License\n\nMIT © [Harvey Zack](https://sleepy.im/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhw2590582%2Foption-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhw2590582%2Foption-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhw2590582%2Foption-validator/lists"}