{"id":13698458,"url":"https://github.com/dsheiko/bycontract","last_synced_at":"2026-02-26T12:01:29.078Z","repository":{"id":38486958,"uuid":"49286892","full_name":"dsheiko/bycontract","owner":"dsheiko","description":"Argument validation library based on JSDOC syntax","archived":false,"fork":false,"pushed_at":"2025-03-27T10:20:23.000Z","size":1581,"stargazers_count":31,"open_issues_count":14,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-22T05:16:49.217Z","etag":null,"topics":["argument-validation","browser","contract-programming","functional-programming","javascript","jsdoc3","nodejs","production-optimization","type-checking","validation-library"],"latest_commit_sha":null,"homepage":"https://dsheiko.gitbook.io/bycontract/","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/dsheiko.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}},"created_at":"2016-01-08T17:41:13.000Z","updated_at":"2025-03-27T10:47:31.000Z","dependencies_parsed_at":"2023-02-03T02:30:48.161Z","dependency_job_id":null,"html_url":"https://github.com/dsheiko/bycontract","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/dsheiko/bycontract","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsheiko%2Fbycontract","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsheiko%2Fbycontract/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsheiko%2Fbycontract/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsheiko%2Fbycontract/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dsheiko","download_url":"https://codeload.github.com/dsheiko/bycontract/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsheiko%2Fbycontract/sbom","scorecard":{"id":357719,"data":{"date":"2025-08-11","repo":{"name":"github.com/dsheiko/bycontract","commit":"ddfd9059fa09f8788e71f387141f3808680048df"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":1.3,"checks":[{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Code-Review","score":0,"reason":"Found 2/28 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"License","score":0,"reason":"license file not detected","details":["Warn: project does not have a license file"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 4 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"32 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw","Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3","Warn: Project is vulnerable to: GHSA-968p-4wvh-cqc8","Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92","Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg","Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5","Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275","Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c","Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq","Warn: Project is vulnerable to: GHSA-fjxv-7rqg-78g4","Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj","Warn: Project is vulnerable to: GHSA-896r-f27r-55mw","Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h","Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq","Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488","Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g","Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv","Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h","Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p","Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9","Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp","Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6","Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw","Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc","Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v","Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3","Warn: Project is vulnerable to: GHSA-hc6q-2mpp-qw7j","Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986","Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7","Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693","Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-18T10:02:44.697Z","repository_id":38486958,"created_at":"2025-08-18T10:02:44.697Z","updated_at":"2025-08-18T10:02:44.697Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29858461,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-26T08:51:08.701Z","status":"ssl_error","status_checked_at":"2026-02-26T08:50:19.607Z","response_time":89,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["argument-validation","browser","contract-programming","functional-programming","javascript","jsdoc3","nodejs","production-optimization","type-checking","validation-library"],"created_at":"2024-08-02T19:00:46.998Z","updated_at":"2026-02-26T12:01:29.062Z","avatar_url":"https://github.com/dsheiko.png","language":"JavaScript","funding_links":[],"categories":["Type checking"],"sub_categories":["Type parsing"],"readme":"ByContract 2\n==============\n[![NPM](https://nodei.co/npm/bycontract.png)](https://nodei.co/npm/bycontract/)\n[![Build Status](https://travis-ci.org/dsheiko/bycontract.png)](https://travis-ci.org/dsheiko/bycontract)\n[![Join the chat at https://gitter.im/dsheiko/bycontract](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/dsheiko/bycontract?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\n`bycontract` – A lightweight runtime type checker for JavaScript and TypeScript, powered by [JSDOC syntax](https://jsdoc.app/tags-type.html). Ensure your functions receive the right arguments with simple, declarative contract definitions. No transpilation needed—just clean, reliable validation.\n\n# Highlights\n\n- **JSDoc-based validation** – Use familiar JSDoc expressions for defining type contracts.\n- **Comprehensive contract checks** – Validate both function inputs and return values.\n- **Clear, actionable errors** – Get detailed exceptions, inspired by [aproba](https://github.com/iarna/aproba).\n- **Deep object validation** – Support for recursive structure checks.\n- **Interface validation** – Ensure objects conform to expected shapes.\n- **Flexible usage** – Available as a template tag and property decorators.\n- **Production-ready** – Can be disabled or stripped out entirely for optimized builds.\n\n\n# Table of contents\n* [Welcome ByContract](#welcome-bycontract)\n* [Where to use it](#where-to-use-it)\n* [Syntax Overview](#syntax-overview)\n* [Types](#types)\n* [Custom Types](#custom-types)\n* [Custom Validators](#custom-validators)\n* [Production Environment](#production-environment)\n\n\n# Welcome ByContract\n\n## Main flavor\n```js\nfunction pdf( path, w, h, options, callback ) {\n  validate( arguments, [\n    \"string\",\n    \"!number\",\n    \"!number\",\n    PdfOptionsType,\n    \"function=\" ] );\n}\n```\n\n## Template tag flavor\n```js\nfunction pdf( path, w, h, options, callback ) {\n  validateContract`\n    {string}          ${ path }\n    {!number}         ${ w }\n    {!number}         ${ h }\n    {#PdfOptionsType} ${ options }\n    {function=}       ${ callback }\n    `;\n}\n```\n\n## Property decorator flavor\n```js\nclass Page {\n  @validateJsdoc(`\n    @param {string}          path\n    @param {!number}         w\n    @param {!number}         h\n    @param {#PdfOptionsType} options\n    @param {function=}       callback\n    @returns {Promise}\n  `)\n  pdf( path, w, h, options, callback ) {\n    return Promise.resolve();\n  }\n}\n```\n\n\n# Where to use it\n\n## Node.js\n\n```bash\nnpm install bycontract\n```\n\n```js\nconst { validate } = require( \"bycontract\" );\nvalidate( 1, \"number|string\" );\n```\n\n## Browser\n\n```js\n\u003cscript src=\"dist/byContract.min.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n  const { validate } =  byContract;\n  validate( 1, \"number|string\" );\n\u003c/script\u003e\n```\n\n## ES6 Module / Webpack\n\n```bash\nnpm install bycontract\n```\n\n```js\nimport { validate } from \"bycontract\";\nvalidate( 1, \"number|string\" );\n```\n\n\n# Syntax Overview\n\n## Main flavor\n\n##### Validate arguments\n```js\nvalidate( arguments, [ \"JSDOC-EXPRESSION\", \"JSDOC-EXPRESSION\" ] );  // ok or exception\n```\n\n##### Validate a single value (e.g. return value)\n```js\nvalidate( value, \"JSDOC-EXPRESSION\" ); // ok or exception\n\n```\n\n##### Example\n\n```js\nimport { validate } from \"bycontract\";\n\nconst PdfOptionsType = {\n  scale: \"?number\"\n}\n\n/**\n * Example\n * @param {string} path\n * @param {!number} w\n * @param {!number} h\n * @param {PdfOptionsType} options\n * @param {function=} callback\n */\nfunction pdf( path, w, h, options, callback ) {\n  validate( arguments, [\n    \"string\",\n    \"!number\",\n    \"!number\",\n    PdfOptionsType,\n    \"function=\" ] );\n  //...\n  const returnValue = Promise.resolve();\n  return validate( returnValue, \"Promise\" );\n}\n\npdf( \"/tmp/test.pdf\", 1, 1, { scale: 1 } );\n\n// Test it\n\npdf( \"/tmp/test.pdf\", \"1\", 1, { scale: 1 } ); // ByContractError: Argument #1: expected non-nullable but got string\n\n```\n\n## Template Tag flavor\n\n\n```js\nvalidateContract`\n    {JSDOC-EXPRESSION} ${ var1 }\n    {JSDOC-EXPRESSION} ${ var2 }\n`;\n```\n\n\n##### Example\n```js\nimport { validate, typedef } from \"bycontract\";\n\ntypedef(\"#PdfOptionsType\", {\n  scale: \"number\"\n});\n\nfunction pdf( path, w, h, options, callback ) {\n  validateContract`\n    {string}          ${ path }\n    {!number}         ${ w }\n    {!number}         ${ h }\n    {#PdfOptionsType} ${ options }\n    {function=}       ${ callback }\n    `;\n}\n```\nor you can copy/paste from JSDoc:\n\n```js\nfunction pdf( path, w, h, options, callback ) {\n  validateContract`\n    @param {string}          ${ path }\n    @param {!number}         ${ w }\n    @param {!number}         ${ h }\n    @param {#PdfOptionsType} ${ options }\n    @param {function=}       ${ callback }\n    `;\n}\n```\n\n\n## Property Decorator flavor\n\n```js\n@validateJsdoc`\n    @param {JSDOC-EXPRESSION} param1\n    @param {JSDOC-EXPRESSION} param2\n`;\n```\n\n##### Example\n```js\nimport { validate, typedef } from \"bycontract\";\n\ntypedef(\"#PdfOptionsType\", {\n  scale: \"number\"\n});\n\nclass Page {\n  @validateJsdoc(`\n    @param {string}          path\n    @param {!number}         w\n    @param {!number}         h\n    @param {#PdfOptionsType} options\n    @param {function=}       callback\n    @returns {Promise}\n  `)\n  pdf( path, w, h, options, callback ) {\n    return Promise.resolve();\n  }\n}\n```\n\n```js\nconst page = new Page();\npage.pdf( \"/tmp/test.pdf\", \"1\", 1, { scale: 1 } );\n// ByContractError:\n// Method: pdf, parameter w: expected non-nullable but got string\n\n```\n\nThis solution requires [legacy decorators proposal](https://babeljs.io/docs/en/babel-plugin-proposal-decorators) support. You can get it with following [Babel](https://babeljs.io) configuration\n```json\n{\n  presets: [\n    [ \"@babel/preset-env\" ]\n  ],\n  plugins: [\n    [ \"@babel/plugin-proposal-decorators\", { \"legacy\": true } ]\n  ]\n}\n```\n\n\n# Types\n\n## Primitive Types\n\nYou can use one of primitive types: `*`, `array`, `string`, `undefined`, `boolean`, `function`, `nan`, `null`, `number`, `object`, `regexp`\n```js\nvalidate( true, \"boolean\" );\n// or\nvalidate( true, \"Boolean\" );\n```\n\n```js\nvalidate( null, \"boolean\" ); // ByContractError: expected boolean but got null\n\nconst fn = () =\u003e validate( arguments, [ \"boolean\", \"*\" ]);\nfn( null, \"any\" ); // ByContractError: Argument #0: expected boolean but got null\n\n```\n\n## Union Types\n\n```js\nvalidate( 100, \"string|number|boolean\" ); // ok\nvalidate( \"foo\", \"string|number|boolean\" ); // ok\nvalidate( true, \"string|number|boolean\" ); // ok\nvalidate( [], \"string|number|boolean\" );\n// ByContractError: expected string|number|boolean but failed on each:\n// expected string but got array, expected number but got array, expected boolean but got array\n```\n\n## Optional Parameters\n```js\nfunction foo( bar, baz ) {\n  validate( arguments, [ \"number=\", \"string=\" ] );\n}\nfoo(); // ok\nfoo( 100 ); // ok\nfoo( 100, \"baz\" ); // ok\nfoo( 100, 100 ); // ByContractError: Argument #1: expected string but got number\nfoo( \"bar\", \"baz\" ); // ByContractError: Argument #0: expected number but got string\n```\n\n## Array Expression\n\n```js\nvalidate( [ 1, 1 ], \"Array.\u003cnumber\u003e\" ); // ok\nvalidate( [ 1, \"1\" ], \"Array.\u003cnumber\u003e\" );\n// ByContractError: array element 1: expected number but got string\n// or\nvalidate( [ 1, 1 ], \"number[]\" ); // ok\nvalidate( [ 1, \"1\" ], \"number[]\" );\n// ByContractError: array element 1: expected number but got string\n\n```\n\n## Object Expression\n```js\nvalidate( { foo: \"foo\", bar: \"bar\" }, \"Object.\u003cstring, string\u003e\" ); // ok\nvalidate( { foo: \"foo\", bar: 100 }, \"Object.\u003cstring, string\u003e\" );\n// ByContractError: object property bar: expected string but got number\n```\n\n## Structure\n```js\nvalidate({\n  foo: \"foo\",\n  bar: 10\n}, {\n  foo: \"string\",\n  bar: \"number\"\n}); // ok\n\nvalidate({\n  foo: \"foo\",\n  bar: {\n    quiz: [10]\n  }\n}, {\n  foo: \"string\",\n  bar: {\n    quiz: \"number[]\"\n  }\n}); // ok\n\nvalidate({\n  foo: \"foo\",\n  bar: 10\n}, {\n  foo: \"string\",\n  bar: \"number\"\n}); // ByContractError:  property #bar expected number but got null\n```\n\n\n## Interface validation\n\nYou can validate if a supplied value is an instance of a declared interface:\n\n```js\nclass MyClass {}\nconst instance = new MyClass();\n\nvalidate( instance, MyClass ); // ok\n```\n\n```js\nclass MyClass {}\nclass Bar {}\nconst instance = new MyClass();\n\nvalidate( instance, Bar );\n// ByContractError: expected instance of Bar but got instance of MyClass\n```\n\nWhen the interface is globally available you can set contract as a string:\n\n```js\nconst instance = new Date();\nvalidate( instance, \"Date\" ); // ok\n\n//..\nvalidate( node, \"HTMLElement\" ); // ok\n//..\nvalidate( ev, \"Event\" ); // ok\n```\n\nGlobally available interfaces can also be used in Array/Object expressions:\n\n```js\nvalidate( [ new Date(), new Date(), new Date() ], \"Array.\u003cDate\u003e\" ); // ok\n```\n\n\n## Nullable Type\n\n```js\nvalidate( 100, \"?number\" ); // ok\nvalidate( null, \"?number\" ); // ok\n```\n\n\n# Validation Exceptions\n\n```js\nimport { validate, Exception } from \"bycontract\";\ntry {\n  validate( 1, \"NaN\" );\n} catch( err ) {\n  console.log( err instanceof Error ); // true\n  console.log( err instanceof TypeError ); // true\n  console.log( err instanceof Exception ); // true\n  console.log( err.name ); // ByContractError\n  console.log( err.message ); // expected nan but got number\n}\n```\n\n# Combinations\n\nSometimes we allow function to accept different sequences of types.\nLet’s take an [example](https://github.com/npm/cli/blob/v6.9.0/lib/fetch-package-metadata.js):\n\n```js\nfunction andLogAndFinish( spec, tracker, done ) {\n  validate( \"SOF|SZF|OOF|OZF\", [ spec, tracker, done ] )\n  //...\n}\n\n```\nWhere the following sequences of types valid:\n- string, object, function\n- string, null, function\n- object, object, function\n- object, null, function\n\n\n```js\nimport { validateCombo } from \"bycontract\";\n\nconst CASE1 = [ \"string\", TRACKER_OPTIONS, \"function\" ],\n      CASE2 = [ \"string\", null, \"function\" ],\n      CASE3 = [ SPEC_OPTIONS, TRACKER_OPTIONS, \"function\" ],\n      CASE4 = [ SPEC_OPTIONS, null, \"function\" ];\n\nvalidateCombo( arguments, [ CASE1, CASE2, CASE3, CASE4 ] );\n```\n\nFunction `validateCombo` throws exception when none of the cases is valid\n\n# Custom Types\n\nPretty much like with [JSDoc @typedef](https://jsdoc.app/tags-typedef.html) one can declare a custom type and use it as a contract.\n\n### Validating against a Union Type\nHere we define a union type for values that can contain either numbers or strings that represent numbers.\n```js\nimport { validate, typedef } from \"bycontract\";\ntypedef( \"NumberLike\", \"number|string\" );\nvalidate( 10, \"NumberLike\" ); // OK\nvalidate( null, \"NumberLike\" ); // ByContractError: expected number|string but got null\n```\n\n### Validating against a Complex Type\nThis example defines a type `Hero` that represents an object/namespace required to have properties `hasSuperhumanStrength` and `hasWaterbreathing` both of boolean type.\n```js\nimport { validate, typedef } from \"bycontract\";\ntypedef( \"#Hero\", {\n  hasSuperhumanStrength: \"boolean\",\n  hasWaterbreathing: \"boolean\"\n});\nvar superman = {\n  hasSuperhumanStrength: true,\n  hasWaterbreathing: false\n};\nvalidate( superman, \"#Hero\" ); // OK\n```\n\nWhen any of properties violates the specified contract an exception thrown\n```js\nvar superman = {\n  hasSuperhumanStrength: 42,\n  hasWaterbreathing: null\n};\nvalidate( superman, \"#Hero\" ); // ByContractError:  property #hasSuperhumanStrength expected boolean but got number\n```\n\nIf value misses a property of the complex type an exception thrown\n```js\nvar auqaman = {\n  hasWaterbreathing: true\n};\nvalidate( superman, \"#Hero\" ); // ByContractError: missing required property #hasSuperhumanStrength\n```\n\n## Custom Validators\n\nBasic type validators exposed exported as `is` object. So you can extend it:\n\n```js\nimport { validate, is } from \"bycontract\";\nis.email = function( val ){\n  var re = /^(([^\u003c\u003e()[\\]\\\\.,;:\\s@\"]+(\\.[^\u003c\u003e()[\\]\\\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/;\n  return re.test( val );\n}\nvalidate( \"me@dsheiko.com\", \"email\" ); // ok\nvalidate( \"bla-bla\", \"email\" ); // ByContractError: expected email but got string\n```\n\n# Production Environment\n\nYou can disable validation logic for production env like\n\n```js\nimport { validate, config } from \"bycontract\";\nif ( process.env.NODE_ENV === \"production\" ) {\n  config({ enable: false });\n}\n```\nAlternatively you can fully remove the library from the production codebase with Webpack:\n\n#### webpack config\n```js\nconst webpack = require( \"webpack\" ),\n      TerserPlugin = require( \"terser-webpack-plugin\" );\n\nmodule.exports = {\n  mode: process.env.NODE_ENV || \"development\",\n  ...\n  optimization: {\n     minimizer: [\n         new TerserPlugin(),\n         new webpack.NormalModuleReplacementPlugin(\n          /dist\\/bycontract\\.dev\\.js/,\n          \".\\/bycontract.prod.js\"\n        )\n     ]\n  }\n};\n```\n\n#### building for development\n```bash\nnpx NODE_ENV=development webpack\n```\n\n#### building for production\n```bash\nnpx NODE_ENV=production webpack\n```\n\n\n[![Analytics](https://ga-beacon.appspot.com/UA-1150677-13/dsheiko/bycontract)](https://github.com/igrigorik/ga-beacon)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdsheiko%2Fbycontract","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdsheiko%2Fbycontract","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdsheiko%2Fbycontract/lists"}