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