{"id":42267421,"url":"https://github.com/thanhngch/js-validate-type","last_synced_at":"2026-01-27T06:50:13.601Z","repository":{"id":57141286,"uuid":"299070142","full_name":"thanhngch/js-validate-type","owner":"thanhngch","description":"Validate type in javascript at runtime","archived":false,"fork":false,"pushed_at":"2024-11-30T04:46:49.000Z","size":44,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-09T09:11:06.631Z","etag":null,"topics":["javascript","type-validation","validation"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/thanhngch.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-09-27T16:16:12.000Z","updated_at":"2024-12-09T10:24:39.000Z","dependencies_parsed_at":"2025-04-26T16:03:59.191Z","dependency_job_id":"813eb3d8-831e-438e-8143-173105cdce68","html_url":"https://github.com/thanhngch/js-validate-type","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/thanhngch/js-validate-type","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanhngch%2Fjs-validate-type","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanhngch%2Fjs-validate-type/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanhngch%2Fjs-validate-type/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanhngch%2Fjs-validate-type/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thanhngch","download_url":"https://codeload.github.com/thanhngch/js-validate-type/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanhngch%2Fjs-validate-type/sbom","scorecard":{"id":876616,"data":{"date":"2025-08-11","repo":{"name":"github.com/thanhngch/js-validate-type","commit":"91392dbc8798589f59e0f8305605acdcd6625417"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"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 0/20 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":"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":"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":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"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":"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":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENCE:0","Info: FSF or OSI recognized license: MIT License: LICENCE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"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"}}]},"last_synced_at":"2025-08-24T06:15:44.952Z","repository_id":57141286,"created_at":"2025-08-24T06:15:44.952Z","updated_at":"2025-08-24T06:15:44.952Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28806721,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T06:25:51.065Z","status":"ssl_error","status_checked_at":"2026-01-27T06:25:50.640Z","response_time":168,"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":["javascript","type-validation","validation"],"created_at":"2026-01-27T06:50:12.747Z","updated_at":"2026-01-27T06:50:13.586Z","avatar_url":"https://github.com/thanhngch.png","language":"TypeScript","readme":"## About\n\nValidate type in javascript in runtime. A lightweight, no-dependency library. It's ~320 line of code.\n\n[![npm version](http://img.shields.io/npm/v/js-validate-type.svg?style=flat-square)](https://npmjs.org/package/js-validate-type)\n[![GitHub license](https://img.shields.io/github/license/thanhngch/js-validate-type.svg)](https://github.com/thanhngch/js-validate-type/blob/master/LICENCE)\n[![Build Status](http://img.shields.io/travis/thanhngch/js-validate-type.svg?style=flat-square)](https://travis-ci.org/thanhngch/js-validate-type)\n[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)\n[![Codecov](https://img.shields.io/codecov/c/github/thanhngch/js-validate-type.svg?style=flat-square)](https://codecov.io/gh/thanhngch/js-validate-type)\n\n## Installation\n\n`npm i js-validate-type --save`\n\n## How to using\n\n```js\nimport { Type, string } from 'js-validate-type';\n\nconst Person = new Type({\n  name: string,\n  books: [string],\n});\n\nconst me = {\n  name: 'Peter',\n  books: ['Code complete', 'The clean coder'],\n};\n\nconst errors = Person.validate(me);\n// errors is []\n```\n\n## Simple types\n\nSimple types are `optional`, `boolean`, `number`, `string`, `integer`, `Null`, `Undefined`,\n`array`, `object`, `func` (function type), and `promise`.\n\nExample 1\n\n```js\nconst Age = new Type(number);\nconst age = 23;\n\nconst errors = Age.validate(age); // no errors\n```\n\nExample 2 test simple type with object\n\n```js\nimport { Type, string } from 'js-validate-type';\n\nconst Person = new Type({\n  name: string,\n  age: integer,\n  height: number,\n  friends: array,\n  books: optional,\n});\n\nconst me = {\n  name: 'Peter',\n  age: 23,\n  height: 175.0,\n  friends: ['Tom'],\n};\n\nconst errors = Person.validate(me); // no errors\n```\n\n## Union type\n\nUsing union type of simple types using `|`\nEg:\n- `number | string | array`: number or string or array\n- `string | optional`: is optional string\n\nExample 3\n\n```js\nconst Person = new Type({\n  height: number | string,\n});\n\nconst me = {\n  height: '170cm',\n};\nconst myFriend = {\n  height: 172,\n};\n\nconst meErrors = Person.validate(me); // no errors\n\nconst friendErrors = Person.validate(myFriend); // no errors\n```\n\n## Enum type\n\nUsing `[[enum of element]]` for enum type with format:\n\n- `[[1,2,3]]` : value is 1, 2 or 3\n- `[['male', 'female']]` : value is male or female\n\nNote: type of element is `boolean`, `number`, `string`, `null`, `undefined` is support.\n\nExample 4\n\n```js\nconst Person = new Type({\n  gender: [['male', 'female']],\n  age: [[10, 12, 15]],\n});\nconst me = {\n  gender: 'male',\n  age: 12,\n};\n\nconst errors = Person.validate(me);\n```\n\n## Condition type\n\nUsing `[[conditionFunction]]` same as enum type, but `conditionFunction` is function return `true` for valid value.\n\n- Condition function with pararmeter with format `[[ conditionFunction, param ]]`. Only one parameter is support.\n- Multiple condition function is\n\n```js\n[\n  [conditionFunction1, param],\n  [conditionFunction2, param],\n  ...\n]\n```\n\nExample 5\n\n```js\nimport { Type, isString } from 'js-validate-type';\n\nconst isEmail = (value) =\u003e isString(value) \u0026\u0026 /\\S+@\\S+\\.\\S+/.test(value);\nconst isEndsWith = (value, endString) =\u003e value.endsWith(endString);\n\nconst Person = new Type({\n  email: [[isEmail]],\n  email2: [[isEmail], [isEndsWith, 'gmail.com']],\n});\n\nconst me = {\n  email: 'peter@gmail.com',\n  email2: 'peter2@gmail.com',\n};\n\n// it will call isEndsWith('peter2@gmail.com', 'gmail.com'); -\u003e true\n\nconst errors = Person.validate(me); // no errors\n```\n\n## Build-in condition function\n\nBuild-in condition function include:\n\n- `isOptional`\n- `isObject`\n- `isArray`\n- `isBoolean`\n- `isNumber`\n- `isString`\n- `isFunction`\n- `isPromise`\n- `isNull`\n- `isUndefined`\n- `isInteger`\n  It will check type. And there are functions `max`, `min`, `regex`, `length` which define:\n\n```js\nexport const max = (value, a) =\u003e a \u003e= value;\nexport const min = (value, a) =\u003e a \u003c= value;\nexport const regex = (value, regexExpress) =\u003e regexExpress.test(value);\n\nexport const length = (arrOrString, [a, b = Number.POSITIVE_INFINITY]) =\u003e\n  arrOrString.length \u003e= a \u0026\u0026 arrOrString.length \u003c= b;\n```\n\nExample 6\n\n```js\nimport { Type, isString, isArray, min, length } from 'js-validate-type';\n\nconst Person = new Type({\n  age: [[min, 10]],\n\n  // name is string with length great than 2\n  name: [[isString], [length, [2]]],\n\n  // books is array with array length from 2 to 10\n  books: [[isArray], [length, [2, 10]]],\n});\n\nconst me = {\n  age: 23,\n  name: 'Peter',\n  books: ['Code complete', 'The clean coder'],\n};\n\nconst errors = Person.validate(me); // no errors\n```\n\n## Array type\n\nUsing `[elementType]` if all element has type `elementType`.\nExample:\n\n- `[string]` : array of string\n- `[integer]` : array of string\n\nExample 7\n\n```js\nconst Person = new Type({\n  // books is array of string\n  books: [string],\n});\n\nconst me = {\n  books: ['Code complete', 'The clean coder'],\n};\n\nconst errors = Person.validate(me); // no errors\n```\n\n## Check element type with condition in Array\n\n### Using isArrayElement function\n\nUsing condition function `isArrayElement` to check element of array\n\nExample 8\n```js\nimport { Type, isString, isArrayElement } from 'js-validate-type';\nconst Person = new Type({\n  // books is array of string same as `books: [string]`\n  books: [[isArrayElement], [isString]],\n});\n\nconst me = {\n  books: ['Code complete', 'The clean coder'],\n};\n\nconst errors = Person.validate(me); // no errors\n```\n\nDifference of `isArrayElement` and `isArray` is condition function in `isArrayElement` will call in every element.\n\n### isArrayElement function and array check function\n\nArray check function will call with 3 parameter and it's call once in array.\n\nExample below will check array type `[string, number]`, that is first element is `string` and\nsecond element is `number`:\n\n```js\n// Example 9\nimport { Type, isString, isArrayElement } from 'js-validate-type';\n\n// fist and second parameter is null\n// only using arrayValue\nconst isCustomArray = (_, __, arrayValue) =\u003e {\n  return (\n    arrayValue.length === 2\n    \u0026\u0026 isString(arrayValue[0]) \n    \u0026\u0026 isNumber(arrayValue[1])\n  );\n};\n\nconst Person = new Type({\n  devices: [[isArrayElement], [isCustomArray]],\n});\n\nconst me = {\n  devices: ['iPhone', 2],\n};\n\nconst errors = Person.validate(me); // no errors\n```\n\n### `isFixedArrayElement` function is check array with fixed element\n`isFixedArrayElement(type1, type2, ...)` function will check element in array has element1 has type1, \nelement1 has type2,...\n\n```js\nconst isEndsWith = (value, endString) =\u003e isString(value) \u0026\u0026 value.endsWith(endString);\n\nconst Person = new Type({\n  books: [[isFixedArrayElement(string, number, [[isEndsWith, ['@gmail.com']]])]],\n});\n\nconst me = {\n  books: ['Harry Porter', 1984, 'harry@gmail.com'],\n};\n\nconst errors = Person.validate(me);\n```\n\nThis type same with:\n\n```js\nconst isCustomArray = (_: null, __: null, arrayValue: Array\u003cany\u003e) =\u003e {\n  return (\n    arrayValue.length === 3\n    \u0026\u0026 new Type(string).check(arrayValue[0])\n    \u0026\u0026 new Type(number).check(arrayValue[1])\n    \u0026\u0026 new Type([[isEndsWith, ['@gmail.com']]]).check(arrayValue[2])\n  );\n};\n\nconst Person = new Type({\n  books: [[isArrayElement], [isCustomArray]],\n});\n```\n\n## Object type\n\nUsing object can compose other type.\nExample 10:\n\n```js\nconst Person = new Type({\n  name: string,\n  age: number,\n  gender: string | optional,\n  friends: [\n    {\n      name: string,\n    },\n  ],\n  email: {\n    default: string,\n    others: [[isOptional], [isArrayElement], [isString]],\n  },\n});\n\nconst me = {\n  name: 'Peter',\n  age: 23,\n  friends: [\n    {\n      name: 'John',\n    },\n    {\n      name: 'Bob',\n    },\n  ],\n  email: {\n    default: 'peter@gmail.com',\n  },\n};\n\nconst errors = Person.validate(me); // no error\n```\n\n## Using Type in Type\n\nExample 11\n```js\nconst Books = new Type([string]); // type Books\n\nconst Person = new Type({\n  name: string,\n  books: Books, // using type Books\n});\n\nconst me = {\n  name: 'Peter',\n  books: ['Code complete', 'The clean coder'],\n};\n\nconst errors = Person.validate(me);\n```\n\n## Using recursive type\n\nUsing `Self` type for recusive object or recusive array.\n\nExample 12\n\n```js\nimport Type, { string, Self } from '../src/type.js';\n\nconst Person = new Type({\n  name: string,\n  friends: [Self],\n});\nconst me = {\n  name: 'Peter',\n  friends: [\n    {\n      name: 'Bod',\n    },\n    {\n      name: 'Ana',\n      friends: [\n        {\n          name: 'John',\n        },\n      ],\n    },\n  ],\n};\n\nconst errors = Person.validate(me); // no errors\n```\n\nExample 12.1 for recusive object\n\n```js\nimport Type, { string, Self } from '../src/type.js'\nconst Person = new Type({\n  name: string,\n  bestFriend: Self,\n});\n\nconst me = {\n  name: 'Peter',\n  bestFriend: {\n    name: 'Ana',\n    bestFriend: {\n      name: 'John',\n    },\n  },\n};\n\nconst errors = Person.validate(me); // no errors\n```\n\n## Class validate\n\nIf type is function (or class) it will check with `instanceof` operator.\n\n```js\nimport Type, { promise } from '../src/type.js';\n\nclass Person {\n  constructor(name) {\n    this.name = name;\n  }\n};\n\nconst PersonType = new Type({\n  bestFriend: Object,\n  age: Number,\n  myFriends: [Person],\n});\n\nconst me = {\n  bestFriend: new Person('Hana'), // is instanceof class Object\n  age: new Number(123), // is instanceof class Number\n  myFriends: [new Person('John'), new Person('Peter')],\n};\n\nconst errors = PersonType.validate(me); // no errors\n```\n\n## Error handle\n\nUsing `validate` function to show list all errors.\nIf it's no error it will return an empty array.\nOtherwhise, it return error with format\n\n```js\n[\n  {\n    path: `key in object`,\n    message: `message of error`,\n  },\n];\n```\n\nExample 13:\n\n```js\nconst Person = new Type({\n  name: string,\n  age: number,\n  gender: string | optional,\n  friends: [\n    {\n      name: string,\n    },\n  ],\n  email: {\n    default: string,\n    others: [[isOptional], [isArrayElement], [isString]],\n  },\n});\n\nconst me = {\n  name: 'Peter',\n  age: 23,\n  email: null,\n};\n\nconst errors = Person.validate(me);\n// It return: [\n//  {\n//    key: 'email.default',\n//    message: \"'email.default' is not valid type\"\n//  },\n//  { \n//    key: 'friends',\n//    message: \"value of 'friends' is not array\" \n//  }\n//]\n```\n\n### Using assert or check function\nIf has error assert function will throw an error.\nCheck function return boolean.\n\nExample 14\n```js\nconst Person = new Type({\n  name: string,\n});\n\nconst me = {\n  name: 12,\n};\n\nconst errors = Person.assert(me);\n// -\u003e throw a new Error\n\nconst ok = Person.check(me);\n// =\u003e false\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthanhngch%2Fjs-validate-type","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthanhngch%2Fjs-validate-type","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthanhngch%2Fjs-validate-type/lists"}