1 | <p align="center">
|
2 | <img src="media/logo.png" width="300">
|
3 | <br>
|
4 | <br>
|
5 | </p>
|
6 |
|
7 | [![Coverage Status](https://codecov.io/gh/sindresorhus/ow/branch/main/graph/badge.svg)](https://codecov.io/gh/sindresorhus/ow)
|
8 | [![gzip size](https://badgen.net/bundlephobia/minzip/ow)](https://bundlephobia.com/result?p=ow)
|
9 | [![install size](https://packagephobia.now.sh/badge?p=ow)](https://packagephobia.now.sh/result?p=ow)
|
10 |
|
11 | > Function argument validation for humans
|
12 |
|
13 | ## Highlights
|
14 |
|
15 | - Expressive chainable API
|
16 | - Lots of built-in validations
|
17 | - Supports custom validations
|
18 | - Automatic label inference in Node.js
|
19 | - Written in TypeScript
|
20 |
|
21 | ## Install
|
22 |
|
23 | ```
|
24 | $ npm install ow
|
25 | ```
|
26 |
|
27 | ## Usage
|
28 |
|
29 | *If you use CommonJS, you need to import is as `const {default: ow} = require('ow')`.*
|
30 |
|
31 | ```ts
|
32 | import ow from 'ow';
|
33 |
|
34 | const unicorn = input => {
|
35 | ow(input, ow.string.minLength(5));
|
36 |
|
37 | // …
|
38 | };
|
39 |
|
40 | unicorn(3);
|
41 | //=> ArgumentError: Expected `input` to be of type `string` but received type `number`
|
42 |
|
43 | unicorn('yo');
|
44 | //=> ArgumentError: Expected string `input` to have a minimum length of `5`, got `yo`
|
45 | ```
|
46 |
|
47 | We can also match the shape of an object.
|
48 |
|
49 | ```ts
|
50 | import ow from 'ow';
|
51 |
|
52 | const unicorn = {
|
53 | rainbow: '🌈',
|
54 | stars: {
|
55 | value: '🌟'
|
56 | }
|
57 | };
|
58 |
|
59 | ow(unicorn, ow.object.exactShape({
|
60 | rainbow: ow.string,
|
61 | stars: {
|
62 | value: ow.number
|
63 | }
|
64 | }));
|
65 | //=> ArgumentError: Expected property `stars.value` to be of type `number` but received type `string` in object `unicorn`
|
66 | ```
|
67 |
|
68 | ***Note:*** If you intend on using `ow` for development purposes only, use `require('ow/dev-only')` instead of the usual `import '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.
|
69 |
|
70 | ## API
|
71 |
|
72 | [Complete API documentation](https://sindresorhus.com/ow/interfaces/ow.html)
|
73 |
|
74 | Ow includes TypeScript type guards, so using it will narrow the type of previously-unknown values.
|
75 |
|
76 | ```ts
|
77 | function (input: unknown) {
|
78 | input.slice(0, 3) // Error, Property 'slice' does not exist on type 'unknown'
|
79 |
|
80 | ow(input, ow.string)
|
81 |
|
82 | input.slice(0, 3) // OK
|
83 | }
|
84 | ```
|
85 |
|
86 | ### ow(value, predicate)
|
87 |
|
88 | Test if `value` matches the provided `predicate`. Throws an `ArgumentError` if the test fails.
|
89 |
|
90 | ### ow(value, label, predicate)
|
91 |
|
92 | Test if `value` matches the provided `predicate`. Throws an `ArgumentError` with the specified `label` if the test fails.
|
93 |
|
94 | The `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.
|
95 |
|
96 | ### ow.isValid(value, predicate)
|
97 |
|
98 | Returns `true` if the value matches the predicate, otherwise returns `false`.
|
99 |
|
100 | ### ow.create(predicate)
|
101 |
|
102 | Create a reusable validator.
|
103 |
|
104 | ```ts
|
105 | const checkPassword = ow.create(ow.string.minLength(6));
|
106 |
|
107 | const password = 'foo';
|
108 |
|
109 | checkPassword(password);
|
110 | //=> ArgumentError: Expected string `password` to have a minimum length of `6`, got `foo`
|
111 | ```
|
112 |
|
113 | ### ow.create(label, predicate)
|
114 |
|
115 | Create a reusable validator with a specific `label`.
|
116 |
|
117 | ```ts
|
118 | const checkPassword = ow.create('password', ow.string.minLength(6));
|
119 |
|
120 | checkPassword('foo');
|
121 | //=> ArgumentError: Expected string `password` to have a minimum length of `6`, got `foo`
|
122 | ```
|
123 |
|
124 | ### ow.any(...predicate[])
|
125 |
|
126 | Returns a predicate that verifies if the value matches at least one of the given predicates.
|
127 |
|
128 | ```ts
|
129 | ow('foo', ow.any(ow.string.maxLength(3), ow.number));
|
130 | ```
|
131 |
|
132 | ### ow.optional.{type}
|
133 |
|
134 | Makes the predicate optional. An optional predicate means that it doesn't fail if the value is `undefined`.
|
135 |
|
136 | ```ts
|
137 | ow(1, ow.optional.number);
|
138 |
|
139 | ow(undefined, ow.optional.number);
|
140 | ```
|
141 |
|
142 | ### ow.{type}
|
143 |
|
144 | All the below types return a predicate. Every predicate has some extra operators that you can use to test the value even more fine-grained.
|
145 |
|
146 | #### Primitives
|
147 |
|
148 | - [`undefined`](https://sindresorhus.com/ow/interfaces/ow.html#undefined-1)
|
149 | - [`null`](https://sindresorhus.com/ow/interfaces/ow.html#null)
|
150 | - [`string`](https://sindresorhus.com/ow/classes/stringpredicate.html)
|
151 | - [`number`](https://sindresorhus.com/ow/classes/numberpredicate.html)
|
152 | - [`boolean`](https://sindresorhus.com/ow/classes/booleanpredicate.html)
|
153 | - [`symbol`](https://sindresorhus.com/ow/interfaces/ow.html#symbol-1)
|
154 |
|
155 | #### Built-in types
|
156 |
|
157 | - [`array`](https://sindresorhus.com/ow/classes/arraypredicate.html)
|
158 | - [`function`](https://sindresorhus.com/ow/interfaces/ow.html#function)
|
159 | - [`buffer`](https://sindresorhus.com/ow/interfaces/ow.html#buffer)
|
160 | - [`object`](https://sindresorhus.com/ow/classes/objectpredicate.html)
|
161 | - [`regExp`](https://sindresorhus.com/ow/interfaces/ow.html#regexp-1)
|
162 | - [`date`](https://sindresorhus.com/ow/classes/datepredicate.html)
|
163 | - [`error`](https://sindresorhus.com/ow/classes/errorpredicate.html)
|
164 | - [`promise`](https://sindresorhus.com/ow/interfaces/ow.html#promise-1)
|
165 | - [`map`](https://sindresorhus.com/ow/classes/mappredicate.html)
|
166 | - [`set`](https://sindresorhus.com/ow/classes/setpredicate.html)
|
167 | - [`weakMap`](https://sindresorhus.com/ow/classes/weakmappredicate.html)
|
168 | - [`weakSet`](https://sindresorhus.com/ow/classes/weaksetpredicate.html)
|
169 |
|
170 | #### Typed arrays
|
171 |
|
172 | - [`int8Array`](https://sindresorhus.com/ow/interfaces/ow.html#int8Array)
|
173 | - [`uint8Array`](https://sindresorhus.com/ow/interfaces/ow.html#uint8Array-1)
|
174 | - [`uint8ClampedArray`](https://sindresorhus.com/ow/interfaces/ow.html#uint8ClampedArray-1)
|
175 | - [`int16Array`](https://sindresorhus.com/ow/interfaces/ow.html#int16Array)
|
176 | - [`uint16Array`](https://sindresorhus.com/ow/interfaces/ow.html#uint16Array-1)
|
177 | - [`int32Array`](https://sindresorhus.com/ow/interfaces/ow.html#in32Array)
|
178 | - [`uint32Array`](https://sindresorhus.com/ow/interfaces/ow.html#uin32Array-1)
|
179 | - [`float32Array`](https://sindresorhus.com/ow/interfaces/ow.html#float32Array)
|
180 | - [`float64Array`](https://sindresorhus.com/ow/interfaces/ow.html#float64Array)
|
181 |
|
182 | #### Structured data
|
183 |
|
184 | - [`arrayBuffer`](https://sindresorhus.com/ow/interfaces/ow.html#arraybuffer)
|
185 | - [`dataView`](https://sindresorhus.com/ow/interfaces/ow.html#dataview)
|
186 | - [`sharedArrayBuffer`](https://sindresorhus.com/ow/interfaces/ow.html#sharedarraybuffer-1)
|
187 |
|
188 | #### Miscellaneous
|
189 |
|
190 | - [`nan`](https://sindresorhus.com/ow/interfaces/ow.html#nan)
|
191 | - [`nullOrUndefined`](https://sindresorhus.com/ow/interfaces/ow.html#nullorundefined)
|
192 | - [`iterable`](https://sindresorhus.com/ow/interfaces/ow.html#iterable)
|
193 | - [`typedArray`](https://sindresorhus.com/ow/interfaces/ow.html#typedarray-1)
|
194 |
|
195 | ### Predicates
|
196 |
|
197 | The following predicates are available on every type.
|
198 |
|
199 | #### not
|
200 |
|
201 | Inverts the following predicate.
|
202 |
|
203 | ```ts
|
204 | ow(1, ow.number.not.infinite);
|
205 |
|
206 | ow('', ow.string.not.empty);
|
207 | //=> ArgumentError: Expected string to not be empty, got ``
|
208 | ```
|
209 |
|
210 | #### is(fn)
|
211 |
|
212 | Use a custom validation function. Return `true` if the value matches the validation, return `false` if it doesn't.
|
213 |
|
214 | ```ts
|
215 | ow(1, ow.number.is(x => x < 10));
|
216 |
|
217 | ow(1, ow.number.is(x => x > 10));
|
218 | //=> ArgumentError: Expected `1` to pass custom validation function
|
219 | ```
|
220 |
|
221 | Instead of returning `false`, you can also return a custom error message which results in a failure.
|
222 |
|
223 | ```ts
|
224 | const greaterThan = (max: number, x: number) => {
|
225 | return x > max || `Expected \`${x}\` to be greater than \`${max}\``;
|
226 | };
|
227 |
|
228 | ow(5, ow.number.is(x => greaterThan(10, x)));
|
229 | //=> ArgumentError: Expected `5` to be greater than `10`
|
230 | ```
|
231 |
|
232 | #### validate(fn)
|
233 |
|
234 | Use a custom validation object. The difference with `is` is that the function should return a validation object, which allows more flexibility.
|
235 |
|
236 | ```ts
|
237 | ow(1, ow.number.validate(value => ({
|
238 | validator: value > 10,
|
239 | message: `Expected value to be greater than 10, got ${value}`
|
240 | })));
|
241 | //=> ArgumentError: (number) Expected value to be greater than 10, got 1
|
242 | ```
|
243 |
|
244 | You can also pass in a function as `message` value which accepts the label as argument.
|
245 |
|
246 | ```ts
|
247 | ow(1, 'input', ow.number.validate(value => ({
|
248 | validator: value > 10,
|
249 | message: label => `Expected ${label} to be greater than 10, got ${value}`
|
250 | })));
|
251 | //=> ArgumentError: Expected number `input` to be greater than 10, got 1
|
252 | ```
|
253 |
|
254 | #### message(string | fn)
|
255 |
|
256 | Provide a custom message:
|
257 |
|
258 | ```ts
|
259 | ow('🌈', 'unicorn', ow.string.equals('🦄').message('Expected unicorn, got rainbow'));
|
260 | //=> ArgumentError: Expected unicorn, got rainbow
|
261 | ```
|
262 |
|
263 | You 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.
|
264 |
|
265 | ```ts
|
266 | ow('🌈', ow.string.minLength(5).message((value, label) => `Expected ${label}, to have a minimum length of 5, got \`${value}\``));
|
267 | //=> ArgumentError: Expected string, to be have a minimum length of 5, got `🌈`
|
268 | ```
|
269 |
|
270 | It's also possible to add a separate message per validation:
|
271 |
|
272 | ```ts
|
273 | ow(
|
274 | '1234',
|
275 | ow.string
|
276 | .minLength(5).message((value, label) => `Expected ${label}, to be have a minimum length of 5, got \`${value}\``)
|
277 | .url.message('This is no url')
|
278 | );
|
279 | //=> ArgumentError: Expected string, to be have a minimum length of 5, got `1234`
|
280 |
|
281 | ow(
|
282 | '12345',
|
283 | ow.string
|
284 | .minLength(5).message((value, label) => `Expected ${label}, to be have a minimum length of 5, got \`${value}\``)
|
285 | .url.message('This is no url')
|
286 | );
|
287 | //=> ArgumentError: This is no url
|
288 | ```
|
289 |
|
290 | This can be useful for creating your own reusable validators which can be extracted to a separate npm package.
|
291 |
|
292 | ## Maintainers
|
293 |
|
294 | - [Sindre Sorhus](https://github.com/sindresorhus)
|
295 | - [Sam Verschueren](https://github.com/SamVerschueren)
|
296 |
|
297 | ## Related
|
298 |
|
299 | - [@sindresorhus/is](https://github.com/sindresorhus/is) - Type check values
|
300 | - [ngx-ow](https://github.com/SamVerschueren/ngx-ow) - Angular form validation on steroids
|