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

Commit d54bf18

Browse files
feat(rules): Added Odd rule
1 parent 21a8c8d commit d54bf18

8 files changed

Lines changed: 100 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
120120
- [ObjectInstance](docs/object-instance.md)
121121
- [ObjectTypeStrict](docs/object-type-strict.md)
122122
- [ObjectType](docs/object-type.md)
123+
- [Odd](docs/odd.md)
123124
- [PerfectSquare](docs/perfect-square.md)
124125
- [Pesel](docs/pesel.md)
125126
- [Phone](docs/phone.md)

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.

docs/odd.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Odd
2+
3+
Validates an odd number.
4+
5+
Valid values:
6+
7+
```js
8+
validator.odd().validate(-5);
9+
validator.odd().validate(-1);
10+
validator.odd().validate(1);
11+
validator.odd().validate(13);
12+
validator.odd().validate(165);
13+
validator.odd().validate(1);
14+
validator.odd().validate('1');
15+
validator.odd().validate(165.0);
16+
```
17+
18+
Invalid values:
19+
20+
```js
21+
validator.odd().validate(null);
22+
validator.odd().validate('');
23+
validator.odd().validate(-2);
24+
validator.odd().validate(-0);
25+
validator.odd().validate(0);
26+
validator.odd().validate(32);
27+
validator.odd().validate(1.1);
28+
validator.odd().validate(-1.1);
29+
validator.odd().validate('0.2');
30+
validator.odd().validate('165.7');
31+
```

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export * from './number-val';
4141
export * from './object-instance';
4242
export * from './object-type-strict';
4343
export * from './object-type';
44+
export * from './odd';
4445
export * from './perfect-square';
4546
export * from './pesel';
4647
export * from './phone';

src/rules/odd.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { AbstractRule } from './abstract-rule';
2+
import { IntVal } from './int-val';
3+
4+
export class Odd extends AbstractRule {
5+
6+
/**
7+
* Validate.
8+
*/
9+
public validate(input: any): boolean {
10+
return new IntVal().validate(input) && input % 2 !== 0;
11+
}
12+
}

src/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export class Validator extends AllOf {
5050
public static objectInstance = (): Validator => new Validator(new rules.ObjectInstance());
5151
public static objectTypeStrict = (): Validator => new Validator(new rules.ObjectTypeStrict());
5252
public static objectType = (): Validator => new Validator(new rules.ObjectType());
53+
public static odd = (): Validator => new Validator(new rules.Odd());
5354
public static perfectSquare = (): Validator => new Validator(new rules.PerfectSquare());
5455
public static pesel = (): Validator => new Validator(new rules.Pesel());
5556
public static phone = (): Validator => new Validator(new rules.Phone());
@@ -128,6 +129,7 @@ export class Validator extends AllOf {
128129
public objectInstance = (): this => this.addRule(new rules.ObjectInstance());
129130
public objectTypeStrict = (): this => this.addRule(new rules.ObjectTypeStrict());
130131
public objectType = (): this => this.addRule(new rules.ObjectType());
132+
public odd = (): this => this.addRule(new rules.Odd());
131133
public perfectSquare = (): this => this.addRule(new rules.PerfectSquare());
132134
public pesel = (): this => this.addRule(new rules.Pesel());
133135
public phone = (): this => this.addRule(new rules.Phone());

test/rules/odd.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { Odd } from '../../src/rules/odd';
5+
6+
describe('Odd', () => {
7+
8+
let odd: Odd;
9+
10+
beforeEach(() => {
11+
odd = new Odd();
12+
});
13+
14+
it('is rule', () => {
15+
assert.instanceOf(odd, AbstractRule);
16+
});
17+
18+
it('values is valid', () => {
19+
assert.isTrue(odd.validate(-5));
20+
assert.isTrue(odd.validate(-1));
21+
assert.isTrue(odd.validate(1));
22+
assert.isTrue(odd.validate(13));
23+
assert.isTrue(odd.validate(165));
24+
assert.isTrue(odd.validate(1));
25+
assert.isTrue(odd.validate('1'));
26+
assert.isTrue(odd.validate(165.0));
27+
});
28+
29+
it('values is not valid', () => {
30+
assert.isFalse(odd.validate(''));
31+
assert.isFalse(odd.validate(-2));
32+
assert.isFalse(odd.validate(-0));
33+
assert.isFalse(odd.validate(0));
34+
assert.isFalse(odd.validate(32));
35+
assert.isFalse(odd.validate(1.1));
36+
assert.isFalse(odd.validate(-1.1));
37+
assert.isFalse(odd.validate('0.2'));
38+
assert.isFalse(odd.validate('.2'));
39+
assert.isFalse(odd.validate('-.2'));
40+
assert.isFalse(odd.validate('165.7'));
41+
assert.isFalse(odd.validate(''));
42+
assert.isFalse(odd.validate(null));
43+
assert.isFalse(odd.validate('a'));
44+
assert.isFalse(odd.validate(' '));
45+
assert.isFalse(odd.validate('Foo'));
46+
assert.isFalse(odd.validate('19347e12'));
47+
assert.isFalse(odd.validate(1e12));
48+
});
49+
50+
});

test/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ describe('Validator', () => {
6666
assert.instanceOf(V.objectInstance(), V);
6767
assert.instanceOf(V.objectTypeStrict(), V);
6868
assert.instanceOf(V.objectType(), V);
69+
assert.instanceOf(V.odd(), V);
6970
assert.instanceOf(V.perfectSquare(), V);
7071
assert.instanceOf(V.pesel(), V);
7172
assert.instanceOf(V.phone(), V);
@@ -166,6 +167,7 @@ describe('Validator', () => {
166167
assert.instanceOf(v.objectInstance(), V);
167168
assert.instanceOf(v.objectTypeStrict(), V);
168169
assert.instanceOf(v.objectType(), V);
170+
assert.instanceOf(v.odd(), V);
169171
assert.instanceOf(v.perfectSquare(), V);
170172
assert.instanceOf(v.pesel(), V);
171173
assert.instanceOf(v.phone(), V);

0 commit comments

Comments
 (0)