{"id":13682423,"url":"https://github.com/Marcisbee/letype","last_synced_at":"2025-04-30T09:32:27.794Z","repository":{"id":51582365,"uuid":"243271112","full_name":"Marcisbee/letype","owner":"Marcisbee","description":"🔏 Type checker for any data structures","archived":false,"fork":false,"pushed_at":"2023-04-05T09:16:46.000Z","size":564,"stargazers_count":16,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-05T03:43:58.841Z","etag":null,"topics":["schema","type","type-check","type-checker","types","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/Marcisbee.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-02-26T13:37:46.000Z","updated_at":"2023-04-05T09:16:51.000Z","dependencies_parsed_at":"2024-01-14T17:05:10.770Z","dependency_job_id":"843839a1-775f-4b6e-8313-e875f604ba8f","html_url":"https://github.com/Marcisbee/letype","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/Marcisbee%2Fletype","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcisbee%2Fletype/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcisbee%2Fletype/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcisbee%2Fletype/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Marcisbee","download_url":"https://codeload.github.com/Marcisbee/letype/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224206055,"owners_count":17273379,"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":["schema","type","type-check","type-checker","types","validator"],"created_at":"2024-08-02T13:01:45.856Z","updated_at":"2024-11-12T02:30:36.923Z","avatar_url":"https://github.com/Marcisbee.png","language":"JavaScript","readme":"# Letype\n\n[![discord](https://dcbadge.vercel.app/api/server/a62gfaDW2e?style=flat-square)](https://discord.gg/a62gfaDW2e)\n\n\u003e Type checker that uses native data types.\n\nLetype is simple and small ([1kB minified + gzipped](https://bundlephobia.com/result?p=letype)) type checker library that can validate any JS data types and structures as well as any custom ones.\n\n## Features\n- Uses native JS data types.\n- Can validate type structures.\n- Supports regex validation as a type.\n- Supports custom types.\n- Small in size.\n\n## Installation\n\nTo install the stable version:\n\n```\nnpm install --save letype\n```\n\nThis assumes you are using [npm](https://www.npmjs.com/) as your package manager.\n\nIf you're not, you can [access these files on unpkg](https://unpkg.com/letype/dist/) (`letype.min.js` is the file you're probalby after), download them, or point your package manager to them.\n\n### Browser Compatibility\n\nLetype.js currently is compatible with all modern browsers.\n\n## Example usage\n\n```js\nimport { types, check } from 'letype';\n\ncheck(1, Number); // -\u003e true\n\ncheck('1', Number); // -\u003e false\n// \"Type error: `1` is not of type `number`\"\n\ncheck({ counter: 1 }, { counter: Number }); // -\u003e true\n\ncheck({\n  id: 1,\n  name: 92942,\n  age: '21',\n  work: null,\n  anythingGoes: 'flamingo',\n  date: new Date(),\n  regexp: 123,\n}, {\n  id: Number,\n  name: String,\n  age: Number,\n  role: String,\n  anythingGoes: types.Any,\n  date: Date,\n  regexp: /123/,\n}); // -\u003e false\n// \"Type error: `92942` is not of type `string` in `name`\"\n// \"Type error: `21` is not of type `number` in `age`\"\n// \"Type error: `role` is undefined! Required value of type `string`\"\n// \"Type error: `work` is defined as `null`! But it should not be defined at all!\"\n```\n\nAll available exports from package:\n```js\nimport {\n  types,\n  check,\n  assert,\n} from 'letype';\n\nconst {\n  Any,\n  Or,\n  Undefined,\n  Custom,\n} = types;\n```\n\n## List of available functions\n- [`check()`](check)\n- [`assert()`](assert)\n\n## Usage of functions\n\n#### `check()`\nIt takes first argument as value that should be checked.\nSecond argument is type that the value should be checked against.\nIt returns `boolean` (`true` if valid, `false` if invalid).\n```js\ncheck('John Doe', String); // -\u003e true\ncheck(123, String); // -\u003e false\n```\n\n#### `assert()`\nIt does exactly the same thing as `check()` function, but with a little difference.\nIf validation fails it throws error.\n```js\nassert('John Doe', String); // -\u003e true\nassert(123, String); // -\u003e Throw\n```\n\n## List of available types\nTypes are meant to be used as data types that does custom validation against given value inside `assert()` or `check()` functions.\n\n#### Types from `letype` library:\n- [`types.Any`](#typesany)\n- [`types.Or(...types)`](#typesor)\n- [`types.Undefined`](#typesundefined)\n- [`types.Custom`](#typescustom)\n\n#### Types from JavaScript language:\n- [`String`](#string)\n- [`Number`](#number)\n- [`Boolean`](#boolean)\n- [`Array`](#array)\n- [`Function`](#function)\n- [`Date`](#date)\n- [`RegExp`](#regexp)\n\nTypes can also be created in structures:\n\n- [Typed Arrays](#typed-arrays)\n- [Typed Objects](#typed-objects)\n- [Regular Expressions](#regular-expressions)\n\n## Usage of types\n\n#### `types.Any`\n```js\ncheck('string', Any); // -\u003e true\ncheck(123, Any);      // -\u003e true\n```\n\n#### `types.Or()`\n```js\ncheck('string', Or(String, Number)); // -\u003e true\ncheck(123, Or(String, Number));  // -\u003e true\ncheck(true, Or(String, Number)); // -\u003e false\n```\n\n#### `types.Undefined`\n```js\ncheck('string', Undefined);  // -\u003e false\ncheck(undefined, Undefined); // -\u003e true\n```\n\n#### `types.Custom`\nCustom type is empty and does no checks against anything. It is meant for creating your own custom types.\n\nTo do that just extend `Custom` class and define public `parse` method that has one parameter - \"value\".\nIt is value to be checked/validated. `parse` method should return boolean (`true` if valid, `false` if invalid).\n\nFor example lets create type that checks if value has first capital letter.\n```js\nclass Capital extends types.Custom {\n  parse(value) {\n    return value[0] === value[0].toUpperCase();\n  }\n}\n```\n\nTo use it simply pass it in any of `assert()` or `check()` functions.\n\n```js\ncheck('John', Capital); // -\u003e true\ncheck('doe', Capital);  // -\u003e false\n```\n\n#### `String`\n```js\ncheck('123', String); // -\u003e true\ncheck(123, String);   // -\u003e false\n```\n\n#### `Number`\n```js\ncheck('123', Number); // -\u003e false\ncheck(123, Number);   // -\u003e true\n```\n\n#### `Boolean`\n```js\ncheck('false', Boolean); // -\u003e false\ncheck(false, Boolean);   // -\u003e true\n```\n\n#### `Array`\n```js\ncheck('array', Array); // -\u003e false\ncheck([], Array);      // -\u003e true\ncheck([1,2,3], Array); // -\u003e true\n```\n\n#### `Function`\n```js\ncheck('fn', Function);     // -\u003e false\ncheck(() =\u003e {}, Function); // -\u003e true\n```\n\n#### `Date`\n```js\ncheck('10-12-2020', Date); // -\u003e false\ncheck(new Date('10-12-2020'), Date); // -\u003e true\n```\n\n#### `RegExp`\n```js\ncheck('A-Z', RegExp); // -\u003e false\ncheck(/A-Z/, RegExp); // -\u003e true\n```\n\n#### Typed Arrays\n```js\ncheck([], [String]);    // -\u003e false\ncheck([1], [String]);   // -\u003e false\ncheck(['1'], [String]); // -\u003e true\n```\n\n#### Typed Objects\n```js\ncheck({}, { name: String }); // -\u003e false\ncheck({ name: 1 }, { name: String }); // -\u003e false\ncheck({ name: 'John' }, { name: String }); // -\u003e true\n```\n\n#### Regular Expressions\n```js\ncheck('a', /A-Z/); // -\u003e false\ncheck('A', /A-Z/); // -\u003e true\n```\n\n---\n\n## Motivation\n\nWe can get awesome type checking in JS with TS, but that only checks types in compile time.\n\nSo I wanted some kind of runtime type checking with types that are already available in JS - not using strings as a types. This feels more JS and more natural.\n\n## License\n\n[MIT](http://opensource.org/licenses/MIT) licenced. Copyright \u0026copy; 2020-present, [Marcis (Marcisbee) Bergmanis](https://twitter.com/marcisbee)\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMarcisbee%2Fletype","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMarcisbee%2Fletype","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMarcisbee%2Fletype/lists"}