Skip to content

Commit

Permalink
Remove unnecessary stack reference for ow.array.ofType (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanshsieh authored Jul 16, 2021
1 parent a32e354 commit a87c6f0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
2 changes: 1 addition & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Object.defineProperties(ow, {
isValid: {
value: <T>(value: T, predicate: BasePredicate<T>): boolean => {
try {
ow(value, predicate);
test(value, '', predicate);
return true;
} catch {
return false;
Expand Down
19 changes: 3 additions & 16 deletions source/predicates/array.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import isEqual = require('lodash.isequal');
import {BasePredicate} from './base-predicate';
import {Predicate, PredicateOptions} from './predicate';
import ow from '..';
import {Shape} from './object';
import {exact} from '../utils/match-shape';
import ofType from '../utils/of-type';

export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
/**
Expand Down Expand Up @@ -142,23 +142,10 @@ export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
```
*/
ofType<U extends T>(predicate: BasePredicate<U>): ArrayPredicate<U> {
let error: string;

// TODO [typescript@>=5] If higher-kinded types are supported natively by typescript, refactor `addValidator` to use them to avoid the usage of `any`. Otherwise, bump or remove this TODO.
return this.addValidator({
message: (_, label) => `(${label}) ${error}`,
validator: value => {
try {
for (const item of value) {
ow(item, predicate);
}

return true;
} catch (error_: unknown) {
error = (error_ as Error).message;
return false;
}
}
message: (_, label, error) => `(${label}) ${error}`,
validator: value => ofType(value, 'values', predicate)
}) as ArrayPredicate<any>;
}

Expand Down
12 changes: 10 additions & 2 deletions test/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,19 @@ test('array.ofType', t => {

t.throws(() => {
ow(['foo', 'b'], ow.array.ofType(ow.string.minLength(3)));
}, '(array) Expected string to have a minimum length of `3`, got `b`');
}, '(array) Expected string values to have a minimum length of `3`, got `b`');

t.throws(() => {
ow(['foo', 'b'], 'foo', ow.array.ofType(ow.string.minLength(3)));
}, '(array `foo`) Expected string to have a minimum length of `3`, got `b`');
}, '(array `foo`) Expected string values to have a minimum length of `3`, got `b`');

t.throws(() => {
ow(['foo', 'bar'], 'foo', ow.array.ofType(ow.number));
}, '(array `foo`) Expected values to be of type `number` but received type `string`');

t.throws(() => {
ow([1, -1], 'foo', ow.array.ofType(ow.number.not.negative));
}, '(array `foo`) Expected number values to not be negative, got -1');
});

test('array.exactShape', t => {
Expand Down

0 comments on commit a87c6f0

Please sign in to comment.