Skip to content
This repository was archived by the owner on Oct 20, 2021. It is now read-only.

Commit f833dba

Browse files
fix(rules): Validate number before in of Even, Negavite, Positive rules
1 parent ab85ca7 commit f833dba

5 files changed

Lines changed: 7 additions & 14 deletions

File tree

awesome-validator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rules/even.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { AbstractRule } from './abstract-rule';
2+
import { IntVal } from './int-val';
23

34
export class Even extends AbstractRule {
45

56
/**
67
* Validate.
78
*/
89
public validate(input: any): boolean {
9-
return Number(input) % 2 === 0;
10+
return new IntVal().validate(input) && input % 2 === 0;
1011
}
1112
}

src/rules/negative.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ export class Negative extends AbstractRule {
77
* Validate.
88
*/
99
public validate(input: any): boolean {
10-
if (!new NumberVal().validate(input)) {
11-
return false;
12-
}
13-
14-
return input < 0;
10+
return new NumberVal().validate(input) && input < 0;
1511
}
1612
}

src/rules/positive.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ export class Positive extends AbstractRule {
77
* Validate.
88
*/
99
public validate(input: any): boolean {
10-
if (!new NumberVal().validate(input)) {
11-
return false;
12-
}
13-
14-
return input > 0;
10+
return new NumberVal().validate(input) && input > 0;
1511
}
1612
}

test/rules/even.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ describe('Even', () => {
1616
});
1717

1818
it('values is valid', () => {
19-
assert.isTrue(even.validate(null));
20-
assert.isTrue(even.validate(''));
2119
assert.isTrue(even.validate('2'));
2220
assert.isTrue(even.validate(-2));
2321
assert.isTrue(even.validate(-0));
@@ -26,6 +24,8 @@ describe('Even', () => {
2624
});
2725

2826
it('values is not valid', () => {
27+
assert.isFalse(even.validate(''));
28+
assert.isFalse(even.validate(null));
2929
assert.isFalse(even.validate(undefined));
3030
assert.isFalse(even.validate(-3));
3131
assert.isFalse(even.validate(-1));

0 commit comments

Comments
 (0)