diff --git a/source/predicates/object.ts b/source/predicates/object.ts index b0eedd3..e53c946 100644 --- a/source/predicates/object.ts +++ b/source/predicates/object.ts @@ -7,6 +7,7 @@ import ofType from '../utils/of-type'; import ofTypeDeep from '../utils/of-type-deep'; import {partial, exact, Shape} from '../utils/match-shape'; import {Predicate, PredicateOptions} from './predicate'; +import {BasePredicate} from './base-predicate'; export {Shape}; @@ -53,7 +54,7 @@ export class ObjectPredicate extends Predicate { @param predicate - The predicate that should be applied against every value in the object. */ - valuesOfType(predicate: Predicate) { + valuesOfType(predicate: BasePredicate) { return this.addValidator({ message: (_, label, error) => `(${label}) ${error}`, validator: object => ofType(Object.values(object), predicate) diff --git a/source/utils/of-type.ts b/source/utils/of-type.ts index 72604bb..880de1c 100644 --- a/source/utils/of-type.ts +++ b/source/utils/of-type.ts @@ -1,5 +1,5 @@ -import {Predicate} from '../predicates/predicate'; import ow from '..'; +import {BasePredicate} from '../predicates/base-predicate'; /** Test all the values in the collection against a provided predicate. @@ -8,7 +8,7 @@ Test all the values in the collection against a provided predicate. @param source Source collection to test. @param predicate Predicate to test every item in the source collection against. */ -export default (source: IterableIterator | Set | T[], predicate: Predicate): boolean | string => { +export default (source: IterableIterator | Set | T[], predicate: BasePredicate): boolean | string => { try { for (const item of source) { ow(item, predicate); diff --git a/test/object.ts b/test/object.ts index 4cdb591..3e34b31 100644 --- a/test/object.ts +++ b/test/object.ts @@ -71,6 +71,10 @@ test('object.valuesOfType', t => { ow({unicorn: 1, rainbow: 2}, ow.object.valuesOfType(ow.number)); }); + t.notThrows(() => { + ow(['🦄', true], ow.object.valuesOfType(ow.any(ow.string, ow.boolean))); + }); + t.throws(() => { ow({unicorn: '🦄', rainbow: 2}, ow.object.valuesOfType(ow.string)); }, '(object) Expected argument to be of type `string` but received type `number`'); @@ -82,6 +86,10 @@ test('object.valuesOfType', t => { t.throws(() => { ow({unicorn: 'a', rainbow: 'b'}, ow.object.valuesOfType(ow.string.minLength(2))); }, '(object) Expected string to have a minimum length of `2`, got `a`'); + + t.throws(() => { + ow(['🦄', true, 1], ow.object.valuesOfType(ow.any(ow.string, ow.boolean))); + }, '(object) Any predicate failed with the following errors:\n- Expected argument to be of type `string` but received type `number`\n- Expected argument to be of type `boolean` but received type `number`'); }); test('object.valuesOfTypeDeep', t => {