Skip to content

Commit 0f40014

Browse files
committed
Fix bug where parameters of method are not skipped
1 parent cc360b6 commit 0f40014

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ function ValidateClass(errorConstructor = TypeGuardError) {
5959
const originalMethod = target.prototype[propertyKey];
6060
target.prototype[propertyKey] = function (...args) {
6161
for (let i = 0; i < assertions.length; i++) {
62+
if (!assertions[i]) {
63+
continue;
64+
}
6265
const errorObject = assertions[i].assertion(args[i]);
6366
if (errorObject !== null) {
6467
throw new errorConstructor(errorObject, args[i]);

test/issue-75.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* tslint:disable:max-classes-per-file */
2+
3+
import * as assert from 'assert';
4+
import { AssertType, ValidateClass } from '../index';
5+
6+
/* https://github.com/woutervh-/typescript-is/issues/75 */
7+
8+
describe('@ValidateClass, @AssertType', () => {
9+
describe('@ValidateClass(), @AssertType() skipping first parameter', () => {
10+
@ValidateClass()
11+
class TestClass {
12+
testMethod(first: string, @AssertType() second: number) {
13+
// No op.
14+
}
15+
}
16+
const instance = new TestClass();
17+
18+
it('should pass validation for as long as the second parameter is a number', () => {
19+
instance.testMethod('foo', 0);
20+
instance.testMethod('foo', 1);
21+
instance.testMethod('foo', Number.NaN);
22+
instance.testMethod(false as any, 0);
23+
instance.testMethod(false as any, 1);
24+
instance.testMethod(false as any, Number.NaN);
25+
});
26+
27+
it('should throw an error for non-numbers passed to the second parameter', () => {
28+
const expectedMessageRegExp = /validation failed at second: expected a number, found: .*$/;
29+
assert.throws(() => instance.testMethod('foo', '' as any), expectedMessageRegExp);
30+
assert.throws(() => instance.testMethod('foo', '0' as any), expectedMessageRegExp);
31+
assert.throws(() => instance.testMethod('foo', '1' as any), expectedMessageRegExp);
32+
assert.throws(() => instance.testMethod('foo', true as any), expectedMessageRegExp);
33+
assert.throws(() => instance.testMethod('foo', false as any), expectedMessageRegExp);
34+
assert.throws(() => instance.testMethod('foo', {} as any), expectedMessageRegExp);
35+
assert.throws(() => instance.testMethod('foo', [] as any), expectedMessageRegExp);
36+
assert.throws(() => instance.testMethod('foo', null as any), expectedMessageRegExp);
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)