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

Commit 5fa9139

Browse files
feat(rules): Added OneOf rule
1 parent 471f0ba commit 5fa9139

8 files changed

Lines changed: 79 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
121121
- [ObjectTypeStrict](docs/object-type-strict.md)
122122
- [ObjectType](docs/object-type.md)
123123
- [Odd](docs/odd.md)
124+
- [OneOf](docs/one-of.md)
124125
- [PerfectSquare](docs/perfect-square.md)
125126
- [Pesel](docs/pesel.md)
126127
- [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/one-of.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# One Of
2+
3+
Will validate if exactly one inner validator passes.
4+
5+
Valid values:
6+
7+
```js
8+
validator.oneOf(
9+
validator.alwaysInvalid(),
10+
validator.alwaysInvalid(),
11+
validator.alwaysValid()
12+
).validate(null);
13+
```
14+
15+
Invalid values:
16+
17+
```js
18+
validator.oneOf(
19+
validator.alwaysInvalid(),
20+
validator.alwaysValid(),
21+
validator.alwaysValid()
22+
).validate(null);
23+
```

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export * from './object-instance';
4242
export * from './object-type-strict';
4343
export * from './object-type';
4444
export * from './odd';
45+
export * from './one-of';
4546
export * from './perfect-square';
4647
export * from './pesel';
4748
export * from './phone';

src/rules/one-of.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Validatable } from './../validatable';
2+
import { AbstractComposite } from './abstract-composite';
3+
4+
export class OneOf extends AbstractComposite {
5+
6+
/**
7+
* Validate.
8+
*/
9+
public validate(input: any): boolean {
10+
const rules: Validatable[] = this.getRules();
11+
let rulesPassedCount: number = 0;
12+
13+
for (const rule of rules) {
14+
if (rule.validate(input)) {
15+
rulesPassedCount++;
16+
}
17+
}
18+
19+
return rulesPassedCount === 1;
20+
}
21+
}

src/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class Validator extends AllOf {
5151
public static objectTypeStrict = (): Validator => new Validator(new rules.ObjectTypeStrict());
5252
public static objectType = (): Validator => new Validator(new rules.ObjectType());
5353
public static odd = (): Validator => new Validator(new rules.Odd());
54+
public static oneOf = (...args: Validatable[]): Validator => new Validator(new rules.OneOf(...args));
5455
public static perfectSquare = (): Validator => new Validator(new rules.PerfectSquare());
5556
public static pesel = (): Validator => new Validator(new rules.Pesel());
5657
public static phone = (): Validator => new Validator(new rules.Phone());
@@ -130,6 +131,7 @@ export class Validator extends AllOf {
130131
public objectTypeStrict = (): this => this.addRule(new rules.ObjectTypeStrict());
131132
public objectType = (): this => this.addRule(new rules.ObjectType());
132133
public odd = (): this => this.addRule(new rules.Odd());
134+
public oneOf = (...args: Validatable[]): this => this.addRule(new rules.OneOf(...args));
133135
public perfectSquare = (): this => this.addRule(new rules.PerfectSquare());
134136
public pesel = (): this => this.addRule(new rules.Pesel());
135137
public phone = (): this => this.addRule(new rules.Phone());

test/rules/one-of.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { AlwaysInvalid } from '../../src/rules/always-invalid';
5+
import { AlwaysValid } from '../../src/rules/always-valid';
6+
import { OneOf } from '../../src/rules/one-of';
7+
8+
describe('OneOf', () => {
9+
10+
it('is rule', () => {
11+
assert.instanceOf(new OneOf(), AbstractRule);
12+
});
13+
14+
it('values is valid', () => {
15+
assert.isTrue(new OneOf(new AlwaysValid()).validate(null));
16+
assert.isTrue(new OneOf(new AlwaysValid(), new AlwaysInvalid()).validate(null));
17+
assert.isTrue(new OneOf(new AlwaysValid(), new AlwaysInvalid(), new AlwaysInvalid()).validate(null));
18+
});
19+
20+
it('values is not valid', () => {
21+
assert.isFalse(new OneOf(new AlwaysInvalid()).validate(null));
22+
assert.isFalse(new OneOf(new AlwaysInvalid(), new AlwaysInvalid()).validate(null));
23+
assert.isFalse(new OneOf(new AlwaysInvalid(), new AlwaysValid(), new AlwaysValid()).validate(null));
24+
});
25+
26+
});

test/validator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ describe('Validator', () => {
6767
assert.instanceOf(V.objectTypeStrict(), V);
6868
assert.instanceOf(V.objectType(), V);
6969
assert.instanceOf(V.odd(), V);
70+
assert.instanceOf(V.oneOf(), V);
71+
assert.instanceOf(V.oneOf(V.alwaysValid()), V);
7072
assert.instanceOf(V.perfectSquare(), V);
7173
assert.instanceOf(V.pesel(), V);
7274
assert.instanceOf(V.phone(), V);
@@ -168,6 +170,8 @@ describe('Validator', () => {
168170
assert.instanceOf(v.objectTypeStrict(), V);
169171
assert.instanceOf(v.objectType(), V);
170172
assert.instanceOf(v.odd(), V);
173+
assert.instanceOf(v.oneOf(), V);
174+
assert.instanceOf(v.oneOf(V.alwaysValid()), V);
171175
assert.instanceOf(v.perfectSquare(), V);
172176
assert.instanceOf(v.pesel(), V);
173177
assert.instanceOf(v.phone(), V);

0 commit comments

Comments
 (0)