{"id":20339857,"url":"https://github.com/uditkarode/drytype","last_synced_at":"2025-04-11T23:16:21.207Z","repository":{"id":57216609,"uuid":"384478111","full_name":"uditkarode/drytype","owner":"uditkarode","description":"🏜️ A runtime type-validation library with TypeScript in mind.","archived":false,"fork":false,"pushed_at":"2022-08-07T13:15:30.000Z","size":70,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T23:16:15.533Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/uditkarode.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":"2021-07-09T15:26:25.000Z","updated_at":"2022-05-30T16:06:09.000Z","dependencies_parsed_at":"2022-08-28T21:40:08.008Z","dependency_job_id":null,"html_url":"https://github.com/uditkarode/drytype","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uditkarode%2Fdrytype","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uditkarode%2Fdrytype/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uditkarode%2Fdrytype/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uditkarode%2Fdrytype/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uditkarode","download_url":"https://codeload.github.com/uditkarode/drytype/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248492884,"owners_count":21113163,"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-11-14T21:18:47.511Z","updated_at":"2025-04-11T23:16:21.188Z","avatar_url":"https://github.com/uditkarode.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# drytype\n\nDryType is a runtime type-validation library with TypeScript in mind.\n\nRuntime type validations are performed with a runtime type -- a `DryType` as\nI've called it throughout this library (beats me why).\n\nA DryType is an object of the type:\n\n```typescript\nexport type DryType\u003cT\u003e = {\n  validate(param: unknown): ValidationResult;\n  // will throw if check fails\n  strictValidate(\n    param: unknown,\n  ): ValidationResult;\n  guard(param: unknown): param is T;\n  // will throw if check fails\n  strictGuard(param: unknown): param is T;\n  toString(): string;\n\n  intersect\u003cS\u003e(dt: DryType\u003cS\u003e): DryType\u003cT \u0026 S\u003e;\n  /*\n    errorFrom refers to whether the error from the\n    first type, second type, or the default one is\n    to be thrown.\n\n    when this is 0 or undefined, the default error is used\n    when this is 1, the 'left' error is used\n    when this is 2 (or any other value), the 'right' error is used\n  */\n  union\u003cS\u003e(dt: DryType\u003cS\u003e, errorFrom?: number): DryType\u003cT | S\u003e;\n\n  tag: string;\n};\n\ntype ValidationResult = {\n  success: boolean;\n  message?: string;\n  in?: string;\n};\n```\n\nMight look fancy, but it's actually pretty simple.\n\n`validate(param: unknown)` is used to check if a value `param` confirms to the\ntype specification of that `DryType`, for example\n\n```typescript\nimport { String } from \"drytype\";\n\nString.validate(\"hello\"); // { success: true }\nString.validate(20); // { success: false, message: \"expected: string, got: number\" }\n```\n\n`strictValidate` is the same as `validate`, with one big difference: when the\nvalidation fails, instead of returning a ValidationResult object, it throws a\nValidationError(message)`.\n\n`guard` and `strictGuard` are the same as `validate` and `strictValidate`, with\nthe difference being that they are TypeScript guards instead of regular boolean\nreturning functions.\n\n`union` is the same as TypeScript `|`. `A.union(B)` returns a new DryType, which\nnow checks if either A or B succeed. For example,\n\n```typescript\nimport { Number, String } from \"drytype\";\n\nString.union(Number).validate(10); // { success: true }\n```\n\n`union` also takes a second parameter called fromError, which is used when both\nchecks fail. If this is set to 0 or undefined, the default error will be used.\nIf this is set to 1, the custom error from the DryType on the left will be used,\nif any. Any other value will make it use the custom error from the DryType on\nthe right, if any.\n\n`intersect` is the same as TypeScript `\u0026`. `A.intersect(B)` returns a new\nDryType, which now checks if both A and B succeed. For example,\n\n```typescript\nimport { NumberGreaterThan, NumberLessThan } from \"drytype\";\n\n// a number between between 5 and 10\nNumberGreaterThan(5).intersect(NumberLessThan(10)).validate(7); // { success: true }\n```\n\nThat covers what a DryType is composed of. A set of useful DryTypes have been\nprovided by default in the\n[modules](https://github.com/uditkarode/drytype/tree/master/modules) directory\nfor convenience. However, creating a new DryType is also very easy.\n\nTo create a new DryType, you can use `makeDryType`, which has the type:\n\n```typescript\nconst makeDryType = \u003cT\u003e(\n  validator: (x: unknown) =\u003e ValidationResult,\n  tag = \"unknown\",\n): DryType\u003cT\u003e\n```\n\nHere's an example:\n\n```typescript\nexport const Function = makeDryType\u003cFunction\u003e(\n  (x) =\u003e typeof x == \"function\" ? { success: true } : { success: false },\n  \"function\",\n);\n```\n\nThe generic type parameter is the type that the validated value should be of.\nThis is used for TypeScript guards. The first parameter is a function that takes\n`x: unknown` and validates it. This function has to return a `ValidationResult`\nobject, where `success: boolean` is a compulsory field. The `message` field will\nnever be used if `success` is `true`, so you can skip it if `success` is `true`.\nHowever, if `success` is `false`, and you still skip the `message` field, a\ndefault message will be used, which is:\n\n```typescript\n`expected: ${drytypeInQuestion.tag}, got: ${typeof (unknownParam)}${\n  result.in == undefined ? \"\" : `, in: ${result.in}`\n}`;\n```\n\nThe only use of the `in` parameter in `ValidationResult` is to be inserted this\nway into the default error message. This can be useful for, say, Records where\nyou need to give out additional information about where the validation failed.\n\n**NOTE**: Also have a look at the\n[tests](https://github.com/uditkarode/drytype/tree/master/tests) for an example\nusage. Every provided DryType has at least one test present.\n\nThat's all you need to know to use this library. Happy validatin'!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuditkarode%2Fdrytype","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuditkarode%2Fdrytype","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuditkarode%2Fdrytype/lists"}