UNPKG

5.01 kBMarkdownView Raw
1<p align="center">
2 <img src="media/logo.png" width="300">
3 <br>
4 <br>
5</p>
6
7[![Build Status](https://travis-ci.org/sindresorhus/ow.svg?branch=master)](https://travis-ci.org/sindresorhus/ow) [![Coverage Status](https://codecov.io/gh/sindresorhus/ow/branch/master/graph/badge.svg)](https://codecov.io/gh/sindresorhus/ow)
8
9> Argument type validation
10
11[View documentation](https://sindresorhus.com/ow)
12
13
14## Install
15
16```
17$ npm install ow
18```
19
20
21## Usage
22
23```ts
24import ow from 'ow';
25
26const unicorn = input => {
27 ow(input, ow.string.minLength(5));
28
29 // …
30};
31
32unicorn(3);
33//=> ArgumentError: Expected argument to be of type `string` but received type `number`
34
35unicorn('yo');
36//=> ArgumentError: Expected string to have a minimum length of `5`, got `yo`
37```
38
39
40## API
41
42### ow(value, predicate)
43
44Test if `value` matches the provided `predicate`.
45
46### ow.create(predicate)
47
48Create a reusable validator.
49
50```ts
51const checkPassword = ow.create(ow.string.minLength(6));
52
53checkPassword('foo');
54//=> ArgumentError: Expected string to have a minimum length of `6`, got `foo`
55```
56
57### ow.any(...predicate[])
58
59Returns a predicate that verifies if the value matches at least one of the given predicates.
60
61```ts
62ow('foo', ow.any(ow.string.maxLength(3), ow.number));
63```
64
65### ow.{type}
66
67All the below types return a predicate. Every predicate has some extra operators that you can use to test the value even more fine-grained.
68
69#### Primitives
70
71- [`undefined`](https://sindresorhus.com/ow/interfaces/ow.html#undefined)
72- [`null`](https://sindresorhus.com/ow/interfaces/ow.html#null)
73- [`string`](https://sindresorhus.com/ow/classes/stringpredicate.html)
74- [`number`](https://sindresorhus.com/ow/classes/numberpredicate.html)
75- [`boolean`](https://sindresorhus.com/ow/classes/booleanpredicate.html)
76- [`symbol`](https://sindresorhus.com/ow/interfaces/ow.html#symbol)
77
78#### Built-in types
79
80- [`array`](https://sindresorhus.com/ow/classes/arraypredicate.html)
81- [`function`](https://sindresorhus.com/ow/interfaces/ow.html#function)
82- [`buffer`](https://sindresorhus.com/ow/interfaces/ow.html#buffer)
83- [`object`](https://sindresorhus.com/ow/classes/objectpredicate.html)
84- [`regExp`](https://sindresorhus.com/ow/interfaces/ow.html#regexp)
85- [`date`](https://sindresorhus.com/ow/classes/datepredicate.html)
86- [`error`](https://sindresorhus.com/ow/classes/errorpredicate.html)
87- [`promise`](https://sindresorhus.com/ow/interfaces/ow.html#promise)
88- [`map`](https://sindresorhus.com/ow/classes/mappredicate.html)
89- [`set`](https://sindresorhus.com/ow/classes/setpredicate.html)
90- [`weakMap`](https://sindresorhus.com/ow/classes/weakmappredicate.html)
91- [`weakSet`](https://sindresorhus.com/ow/classes/weaksetpredicate.html)
92
93#### Typed arrays
94
95- [`int8Array`](https://sindresorhus.com/ow/interfaces/ow.html#int8Array)
96- [`uint8Array`](https://sindresorhus.com/ow/interfaces/ow.html#uint8Array)
97- [`uint8ClampedArray`](https://sindresorhus.com/ow/interfaces/ow.html#uint8ClampedArray)
98- [`int16Array`](https://sindresorhus.com/ow/interfaces/ow.html#int16Array)
99- [`uint16Array`](https://sindresorhus.com/ow/interfaces/ow.html#uint16Array)
100- [`int32Array`](https://sindresorhus.com/ow/interfaces/ow.html#in32Array)
101- [`uint32Array`](https://sindresorhus.com/ow/interfaces/ow.html#uin32Array)
102- [`float32Array`](https://sindresorhus.com/ow/interfaces/ow.html#float32Array)
103- [`float64Array`](https://sindresorhus.com/ow/interfaces/ow.html#float64Array)
104
105#### Structured data
106
107- [`arrayBuffer`](https://sindresorhus.com/ow/interfaces/ow.html#arraybuffer)
108- [`dataView`](https://sindresorhus.com/ow/interfaces/ow.html#dataview)
109
110#### Miscellaneous
111
112- [`nan`](https://sindresorhus.com/ow/interfaces/ow.html#nan)
113- [`nullOrUndefined`](https://sindresorhus.com/ow/interfaces/ow.html#nullorundefined)
114- [`iterable`](https://sindresorhus.com/ow/interfaces/ow.html#iterable)
115- [`typedArray`](https://sindresorhus.com/ow/interfaces/ow.html#typedarray)
116
117### Predicates
118
119The following predicates are available on every type.
120
121#### not
122
123Inverts the following predicates.
124
125```ts
126ow(1, ow.number.not.infinite);
127
128ow('', ow.string.not.empty);
129//=> ArgumentError: [NOT] Expected string to be empty, got ``
130```
131
132#### is(fn)
133
134Use a custom validation function. Return `true` if the value matches the validation, return `false` if it doesn't.
135
136```ts
137ow(1, ow.number.is(x => x < 10));
138
139ow(1, ow.number.is(x => x > 10));
140//=> ArgumentError: Expected `1` to pass custom validation function
141```
142
143Instead of returning `false`, you can also return a custom error message which results in a failure.
144
145```ts
146const greaterThan = (max: number, x: number) => {
147 return x > max || `Expected \`${x}\` to be greater than \`${max}\``;
148};
149
150ow(5, ow.number.is(x => greaterThan(10, x)));
151//=> ArgumentError: Expected `5` to be greater than `10`
152```
153
154
155## Maintainers
156
157- [Sindre Sorhus](https://github.com/sindresorhus)
158- [Sam Verschueren](https://github.com/SamVerschueren)
159
160
161## Related
162
163- [@sindresorhus/is](https://github.com/sindresorhus/is) - Type check values
164
165
166## License
167
168MIT