Skip to content

Commit

Permalink
Fix for static type error for ow.object.valuesOfType() (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilkerceng authored Sep 30, 2020
1 parent 57a1f5b commit b3c5fbd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion source/predicates/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -53,7 +54,7 @@ export class ObjectPredicate extends Predicate<object> {
@param predicate - The predicate that should be applied against every value in the object.
*/
valuesOfType<T>(predicate: Predicate<T>) {
valuesOfType<T>(predicate: BasePredicate<T>) {
return this.addValidator({
message: (_, label, error) => `(${label}) ${error}`,
validator: object => ofType(Object.values(object), predicate)
Expand Down
4 changes: 2 additions & 2 deletions source/utils/of-type.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 <T>(source: IterableIterator<T> | Set<T> | T[], predicate: Predicate<T>): boolean | string => {
export default <T>(source: IterableIterator<T> | Set<T> | T[], predicate: BasePredicate<T>): boolean | string => {
try {
for (const item of source) {
ow(item, predicate);
Expand Down
8 changes: 8 additions & 0 deletions test/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`');
Expand All @@ -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 => {
Expand Down

0 comments on commit b3c5fbd

Please sign in to comment.