{"id":13448595,"url":"https://github.com/sindresorhus/ow","last_synced_at":"2025-05-13T00:03:43.821Z","repository":{"id":37294936,"uuid":"105227249","full_name":"sindresorhus/ow","owner":"sindresorhus","description":"Function argument validation for humans","archived":false,"fork":false,"pushed_at":"2024-05-05T19:10:27.000Z","size":1425,"stargazers_count":3822,"open_issues_count":35,"forks_count":107,"subscribers_count":21,"default_branch":"main","last_synced_at":"2025-05-13T00:03:32.514Z","etag":null,"topics":["javascript","nodejs","npm-package","type-checking","validation"],"latest_commit_sha":null,"homepage":"https://sindresorhus.com/ow/","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/sindresorhus.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":".github/security.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"sindresorhus","open_collective":"sindresorhus","buy_me_a_coffee":"sindresorhus","custom":"https://sindresorhus.com/donate"}},"created_at":"2017-09-29T03:46:31.000Z","updated_at":"2025-05-05T08:26:50.000Z","dependencies_parsed_at":"2023-02-14T14:15:35.865Z","dependency_job_id":"74aa4f2b-a0d9-4066-b194-b2ea427de551","html_url":"https://github.com/sindresorhus/ow","commit_stats":{"total_commits":243,"total_committers":38,"mean_commits":6.394736842105263,"dds":"0.44855967078189296","last_synced_commit":"2b7fedac1d86d0827a657109c633286890dc8eeb"},"previous_names":[],"tags_count":42,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/ow/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253843205,"owners_count":21972873,"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":["javascript","nodejs","npm-package","type-checking","validation"],"created_at":"2024-07-31T05:01:49.934Z","updated_at":"2025-05-13T00:03:43.797Z","avatar_url":"https://github.com/sindresorhus.png","language":"TypeScript","readme":"\u003cp align=\"center\"\u003e\n\t\u003cimg src=\"media/logo.png\" width=\"300\"\u003e\n\t\u003cbr\u003e\n\t\u003cbr\u003e\n\u003c/p\u003e\n\n[![Coverage Status](https://codecov.io/gh/sindresorhus/ow/branch/main/graph/badge.svg)](https://codecov.io/gh/sindresorhus/ow)\n\n\u003e Function argument validation for humans\n\nFor schema validation, I recommend [`zod`](https://github.com/colinhacks/zod).\n\n## Highlights\n\n- Expressive chainable API\n- Lots of built-in validations\n- Supports custom validations\n- Automatic label inference in Node.js\n- Written in TypeScript\n\n## Install\n\n```sh\nnpm install ow\n```\n\n## Usage\n\n```ts\nimport ow from 'ow';\n\nconst unicorn = input =\u003e {\n\tow(input, ow.string.minLength(5));\n\n\t// …\n};\n\nunicorn(3);\n//=\u003e ArgumentError: Expected `input` to be of type `string` but received type `number`\n\nunicorn('yo');\n//=\u003e ArgumentError: Expected string `input` to have a minimum length of `5`, got `yo`\n```\n\nWe can also match the shape of an object.\n\n```ts\nimport ow from 'ow';\n\nconst unicorn = {\n\trainbow: '🌈',\n\tstars: {\n\t\tvalue: '🌟'\n\t}\n};\n\now(unicorn, ow.object.exactShape({\n\trainbow: ow.string,\n\tstars: {\n\t\tvalue: ow.number\n\t}\n}));\n//=\u003e ArgumentError: Expected property `stars.value` to be of type `number` but received type `string` in object `unicorn`\n```\n\n***Note:*** If you intend on using `ow` for development purposes only, use `import ow from 'ow/dev-only'` instead of the usual `import ow from 'ow'`, and run the bundler with `NODE_ENV` set to `production` (e.g. `$ NODE_ENV=\"production\" parcel build index.js`). This will make `ow` automatically export a shim when running in production, which should result in a significantly lower bundle size.\n\n## API\n\n[Complete API documentation](https://sindresorhus.com/ow/)\n\nOw includes TypeScript type guards, so using it will narrow the type of previously-unknown values.\n\n```ts\nfunction (input: unknown) {\n\tinput.slice(0, 3) // Error, Property 'slice' does not exist on type 'unknown'\n\n\tow(input, ow.string)\n\n\tinput.slice(0, 3) // OK\n}\n```\n\n### ow(value, predicate)\n\nTest if `value` matches the provided `predicate`. Throws an `ArgumentError` if the test fails.\n\n### ow(value, label, predicate)\n\nTest if `value` matches the provided `predicate`. Throws an `ArgumentError` with the specified `label` if the test fails.\n\nThe `label` is automatically inferred in Node.js but you can override it by passing in a value for `label`. The automatic label inference doesn't work in the browser.\n\n### ow.isValid(value, predicate)\n\nReturns `true` if the value matches the predicate, otherwise returns `false`.\n\n### ow.create(predicate)\n\nCreate a reusable validator.\n\n```ts\nconst checkPassword = ow.create(ow.string.minLength(6));\n\nconst password = 'foo';\n\ncheckPassword(password);\n//=\u003e ArgumentError: Expected string `password` to have a minimum length of `6`, got `foo`\n```\n\n### ow.create(label, predicate)\n\nCreate a reusable validator with a specific `label`.\n\n```ts\nconst checkPassword = ow.create('password', ow.string.minLength(6));\n\ncheckPassword('foo');\n//=\u003e ArgumentError: Expected string `password` to have a minimum length of `6`, got `foo`\n```\n\n### ow.any(...predicate[])\n\nReturns a predicate that verifies if the value matches at least one of the given predicates.\n\n```ts\now('foo', ow.any(ow.string.maxLength(3), ow.number));\n```\n\n### ow.optional.{type}\n\nMakes the predicate optional. An optional predicate means that it doesn't fail if the value is `undefined`.\n\n```ts\now(1, ow.optional.number);\n\now(undefined, ow.optional.number);\n```\n\n### ow.{type}\n\nAll the below types return a predicate. Every predicate has some extra operators that you can use to test the value even more fine-grained.\n\n[Predicate docs.](https://sindresorhus.com/ow/types/Predicates.html)\n\n#### Primitives\n\n- `undefined`\n- `null`\n- `string`\n- `number`\n- `boolean`\n- `symbol`\n\n#### Built-in types\n\n- `array`\n- `function`\n- `buffer`\n- `object`\n- `regExp`\n- `date`\n- `error`\n- `promise`\n- `map`\n- `set`\n- `weakMap`\n- `weakSet`\n\n#### Typed arrays\n\n- `int8Array`\n- `uint8Array`\n- `uint8ClampedArray`\n- `int16Array`\n- `uint16Array`\n- `int32Array`\n- `uint32Array`\n- `float32Array`\n- `float64Array`\n\n#### Structured data\n\n- `arrayBuffer`\n- `dataView`\n- `sharedArrayBuffer`\n\n#### Miscellaneous\n\n- `nan`\n- `nullOrUndefined`\n- `iterable`\n- `typedArray`\n\n### Predicates\n\nThe following predicates are available on every type.\n\n#### not\n\nInverts the following predicate.\n\n```ts\now(1, ow.number.not.infinite);\n\now('', ow.string.not.empty);\n//=\u003e ArgumentError: Expected string to not be empty, got ``\n```\n\n#### is(fn)\n\nUse a custom validation function. Return `true` if the value matches the validation, return `false` if it doesn't.\n\n```ts\now(1, ow.number.is(x =\u003e x \u003c 10));\n\now(1, ow.number.is(x =\u003e x \u003e 10));\n//=\u003e ArgumentError: Expected `1` to pass custom validation function\n```\n\nInstead of returning `false`, you can also return a custom error message which results in a failure.\n\n```ts\nconst greaterThan = (max: number, x: number) =\u003e {\n\treturn x \u003e max || `Expected \\`${x}\\` to be greater than \\`${max}\\``;\n};\n\now(5, ow.number.is(x =\u003e greaterThan(10, x)));\n//=\u003e ArgumentError: Expected `5` to be greater than `10`\n```\n\n#### validate(fn)\n\nUse a custom validation object. The difference with `is` is that the function should return a validation object, which allows more flexibility.\n\n```ts\now(1, ow.number.validate(value =\u003e ({\n\tvalidator: value \u003e 10,\n\tmessage: `Expected value to be greater than 10, got ${value}`\n})));\n//=\u003e ArgumentError: (number) Expected value to be greater than 10, got 1\n```\n\nYou can also pass in a function as `message` value which accepts the label as argument.\n\n```ts\now(1, 'input', ow.number.validate(value =\u003e ({\n\tvalidator: value \u003e 10,\n\tmessage: label =\u003e `Expected ${label} to be greater than 10, got ${value}`\n})));\n//=\u003e ArgumentError: Expected number `input` to be greater than 10, got 1\n```\n\n#### message(string | fn)\n\nProvide a custom message:\n\n```ts\now('🌈', 'unicorn', ow.string.equals('🦄').message('Expected unicorn, got rainbow'));\n//=\u003e ArgumentError: Expected unicorn, got rainbow\n```\n\nYou can also pass in a function which receives the value as the first parameter and the label as the second parameter and is expected to return the message.\n\n```ts\now('🌈', ow.string.minLength(5).message((value, label) =\u003e `Expected ${label}, to have a minimum length of 5, got \\`${value}\\``));\n//=\u003e ArgumentError: Expected string, to be have a minimum length of 5, got `🌈`\n```\n\nIt's also possible to add a separate message per validation:\n\n```ts\now(\n\t'1234',\n\tow.string\n\t\t.minLength(5).message((value, label) =\u003e `Expected ${label}, to be have a minimum length of 5, got \\`${value}\\``)\n\t\t.url.message('This is no url')\n);\n//=\u003e ArgumentError: Expected string, to be have a minimum length of 5, got `1234`\n\now(\n\t'12345',\n\tow.string\n\t\t.minLength(5).message((value, label) =\u003e `Expected ${label}, to be have a minimum length of 5, got \\`${value}\\``)\n\t\t.url.message('This is no url')\n);\n//=\u003e ArgumentError: This is no url\n```\n\nThis can be useful for creating your own reusable validators which can be extracted to a separate npm package.\n\n### TypeScript\n\n**Requires TypeScript 4.7 or later.**\n\nOw includes a type utility that lets you to extract a TypeScript type from the given predicate.\n\n```ts\nimport ow, {Infer} from 'ow';\n\nconst userPredicate = ow.object.exactShape({\n\tname: ow.string\n});\n\ntype User = Infer\u003ctypeof userPredicate\u003e;\n```\n\n## Related\n\n- [@sindresorhus/is](https://github.com/sindresorhus/is) - Type check values\n- [ngx-ow](https://github.com/SamVerschueren/ngx-ow) - Angular form validation on steroids\n","funding_links":["https://github.com/sponsors/sindresorhus","https://opencollective.com/sindresorhus","https://buymeacoffee.com/sindresorhus","https://sindresorhus.com/donate"],"categories":["Packages","TypeScript","包","Javascript","nodejs","Miscellaneous","Validation"],"sub_categories":["Miscellaneous","其他","npm packages","Runtime"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2Fow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fow/lists"}