//=> ArgumentError: Expected argument to be of type `string` but received type `number`
//=> ArgumentError: Expected `input` to be of type `string` but received type `number`
unicorn('yo');
//=> ArgumentError: Expected string to have a minimum length of `5`, got `yo`
//=> ArgumentError: Expected string `input`to have a minimum length of `5`, got `yo`
```
@ -49,7 +50,13 @@ unicorn('yo');
### ow(value, predicate)
Test if `value` matches the provided `predicate`. Throws an `ArgumentError` if the test fails.
Test if `value` matches the provided `predicate`. Throws an `ArgumentError` if the test fails.
### ow(value, label, predicate)
Test if `value` matches the provided `predicate`. Throws an `ArgumentError` with the specified `label` if the test fails.
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.
//=> ArgumentError: Expected `5` to be greater than `10`
```
#### label(string)
This assigns a custom label to be used in any error messages. It is useful for making error messages more user-friendly by including the name of the variable which failed validation.
This predicate does not add any additional validation.
```ts
ow('', ow.string.nonEmpty);
//=> ArgumentError: Expected string to not be empty
ow('', ow.string.label('foo').nonEmpty);
//=> ArgumentError: Expected string `foo` to not be empty
t.throws(()=>m(['foo','b'],m.array.ofType(m.string.minLength(3))),'(array) Expected string to have a minimum length of `3`, got `b`');
t.throws(()=>m(['foo','b'],m.array.label('foo').ofType(m.string.minLength(3))),'(array `foo`) Expected string to have a minimum length of `3`, got `b`');
t.throws(()=>m(['foo','b'],m.array.label('foo').ofType(m.string.label('bar').minLength(3))),'(array `foo`) Expected string `bar` to have a minimum length of `3`, got `b`');
t.throws(()=>m(['foo','b'],'foo',m.array.ofType(m.string.minLength(3))),'(array `foo`) Expected string to have a minimum length of `3`, got `b`');
t.throws(()=>m(newDate('2017-11-25T12:00:00Z')asany,m.date.before(newDate('2017-11-25T12:00:00Z'))),'Expected date 2017-11-25T12:00:00.000Z to be before 2017-11-25T12:00:00.000Z');
t.throws(()=>m(newDate('2017-11-25T12:00:00Z')asany,m.date.label('foo').before(newDate('2017-11-25T12:00:00Z'))),'Expected date `foo` 2017-11-25T12:00:00.000Z to be before 2017-11-25T12:00:00.000Z');
t.throws(()=>m(newDate('2017-11-25T12:00:00Z')asany,m.date.before(newDate('2017-11-25T12:00:00Z')).label('foo')),'Expected date `foo` 2017-11-25T12:00:00.000Z to be before 2017-11-25T12:00:00.000Z');
t.throws(()=>m(newDate('2017-11-25T12:00:00Z')asany,'foo',m.date.before(newDate('2017-11-25T12:00:00Z'))),'Expected date `foo` 2017-11-25T12:00:00.000Z to be before 2017-11-25T12:00:00.000Z');
t.throws(()=>m(newMap([['unicorn','🦄']]),m.map.keysOfType(m.number)),'(Map) Expected argument to be of type `number` but received type `string`');
t.throws(()=>m(newMap([['unicorn','🦄']]),m.map.label('foo').keysOfType(m.number)),'(Map `foo`) Expected argument to be of type `number` but received type `string`');
t.throws(()=>m(newMap([['unicorn','🦄']]),m.map.label('foo').keysOfType(m.number.label('bar'))),'(Map `foo`) Expected `bar` to be of type `number` but received type `string`');
t.throws(()=>m(newMap([['unicorn','🦄']]),'foo',m.map.keysOfType(m.number)),'(Map `foo`) Expected argument to be of type `number` but received type `string`');
t.throws(()=>m(newMap([['unicorn','🦄']]),m.map.valuesOfType(m.number)),'(Map) Expected argument to be of type `number` but received type `string`');
t.throws(()=>m(newMap([['unicorn','🦄']]),m.map.label('foo').valuesOfType(m.number)),'(Map `foo`) Expected argument to be of type `number` but received type `string`');
t.throws(()=>m(newMap([['unicorn','🦄']]),m.map.label('foo').valuesOfType(m.number.label('bar'))),'(Map `foo`) Expected `bar` to be of type `number` but received type `string`');
t.throws(()=>m(newMap([['unicorn','🦄']]),'foo',m.map.valuesOfType(m.number)),'(Map `foo`) Expected argument to be of type `number` but received type `string`');
t.throws(()=>m({unicorn:'🦄',rainbow: 2},m.object.valuesOfType(m.string)),'(object) Expected argument to be of type `string` but received type `number`');
t.throws(()=>m({unicorn:'🦄',rainbow: 2},m.object.label('foo').valuesOfType(m.string)),'(object `foo`) Expected argument to be of type `string` but received type `number`');
t.throws(()=>m({unicorn:'🦄',rainbow: 2},m.object.label('foo').valuesOfType(m.string.label('bar'))),'(object `foo`) Expected `bar` to be of type `string` but received type `number`');
t.throws(()=>m({unicorn:'🦄',rainbow: 2},'foo',m.object.valuesOfType(m.string)),'(object `foo`) Expected argument to be of type `string` but received type `number`');
t.throws(()=>m({unicorn:'a',rainbow:'b'},m.object.valuesOfType(m.string.minLength(2))),'(object) Expected string to have a minimum length of `2`, got `a`');
});
@ -47,10 +41,8 @@ test('object.valuesOfTypeDeep', t => {
t.throws(()=>m({unicorn:{key:'🦄',value: 1}},m.object.deepValuesOfType(m.string)),'(object) Expected argument to be of type `string` but received type `number`');
t.throws(()=>m({unicorn:{key:'🦄',value: 1}},m.object.label('foo').deepValuesOfType(m.string)),'(object `foo`) Expected argument to be of type `string` but received type `number`');
t.throws(()=>m({unicorn:{key:'🦄',value: 1}},m.object.label('foo').deepValuesOfType(m.string.label('bar'))),'(object `foo`) Expected `bar` to be of type `string` but received type `number`');
t.throws(()=>m({unicorn:{key:'🦄',value: 1}},'foo',m.object.deepValuesOfType(m.string)),'(object `foo`) Expected argument to be of type `string` but received type `number`');
t.throws(()=>m({a:{b:{c:{d: 1},e:'2'},f: 3}},m.object.deepValuesOfType(m.number)),'(object) Expected argument to be of type `number` but received type `string`');
t.throws(()=>m(newSet(['unicorn']),m.set.ofType(m.number)),'(Set) Expected argument to be of type `number` but received type `string`');
t.throws(()=>m(newSet(['unicorn']),m.set.label('foo').ofType(m.number)),'(Set `foo`) Expected argument to be of type `number` but received type `string`');
t.throws(()=>m(newSet(['unicorn']),m.set.label('foo').ofType(m.number.label('bar'))),'(Set `foo`) Expected `bar` to be of type `number` but received type `string`');
t.throws(()=>m(newSet(['unicorn']),'foo',m.set.ofType(m.number)),'(Set `foo`) Expected argument to be of type `number` but received type `string`');
t.throws(()=>m('foo',m.string.oneOf(['unicorn','rainbow'])),'Expected string to be one of `["unicorn","rainbow"]`, got `foo`');
t.throws(()=>m('foo',m.string.oneOf(['unicorn','rainbow']).label('hello')),'Expected string `hello` to be one of `["unicorn","rainbow"]`, got `foo`');
t.throws(()=>m('foo','hello',m.string.oneOf(['unicorn','rainbow'])),'Expected string `hello` to be one of `["unicorn","rainbow"]`, got `foo`');
t.throws(()=>m('foo',m.string.oneOf(['a','b','c','d','e'])),'Expected string to be one of `["a","b","c","d","e"]`, got `foo`');
t.throws(()=>m('foo',m.string.oneOf(['1','2','3','4','5','6','7','8','9','10','11','12','13'])),'Expected string to be one of `["1","2","3","4","5","6","7","8","9","10",…+3 more]`, got `foo`');
t.throws(()=>m(newWeakMap([[{rainbow: true},'🌈']]),m.weakMap.hasKeys({rainbow: true})),'Expected WeakMap to have keys `[{"rainbow":true}]`');
t.throws(()=>m(newWeakMap([[{rainbow: true},'🌈']]),m.weakMap.label('foo').hasKeys({rainbow: true})),'Expected WeakMap `foo` to have keys `[{"rainbow":true}]`');
t.throws(()=>m(newWeakMap([[{rainbow: true},'🌈']]),m.weakMap.hasKeys({rainbow: true}).label('foo')),'Expected WeakMap `foo` to have keys `[{"rainbow":true}]`');
t.throws(()=>m(newWeakMap([[{rainbow: true},'🌈']]),'foo',m.weakMap.hasKeys({rainbow: true})),'Expected WeakMap `foo` to have keys `[{"rainbow":true}]`');
t.throws(()=>m(newWeakMap([[unicorn,'🦄'],[rainbow,'🌈']]),m.weakMap.hasKeys(unicorn,{rainbow: true})),'Expected WeakMap to have keys `[{"rainbow":true}]`');
t.throws(()=>m(newWeakMap([[keys[0],1],[keys[2],3]]),m.weakMap.hasKeys(...keys)),'Expected WeakMap to have keys `[{"x":2},{"x":4},{"x":5},{"x":6},{"x":7}]`');