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

Commit 8fbf5ed

Browse files
feat(rules): Added NoneOf rule
1 parent ccd33b6 commit 8fbf5ed

8 files changed

Lines changed: 70 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
109109
- [Negative](docs/negative.md)
110110
- [NfeAccessKey](docs/nfe-access-key.md)
111111
- [No](docs/no.md)
112+
- [NoneOf](docs/none-of.md)
112113
- [Not](docs/not.md)
113114
- [NotEmpty](docs/not-empty.md)
114115
- [NullType](docs/null-type.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/none-of.md

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

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export * from './multiple';
3030
export * from './negative';
3131
export * from './nfe-access-key';
3232
export * from './no';
33+
export * from './none-of';
3334
export * from './not';
3435
export * from './not-empty';
3536
export * from './null-type';

src/rules/none-of.ts

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

src/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export class Validator extends AllOf {
3737
public static negative = (): Validator => new Validator(new rules.Negative());
3838
public static nfeAccessKey = (): Validator => new Validator(new rules.NfeAccessKey());
3939
public static no = (): Validator => new Validator(new rules.No());
40+
public static noneOf = (...args: Validatable[]): Validator => new Validator(new rules.NoneOf(...args));
4041
public static not = (rule: Validatable): Validator => new Validator(new rules.Not(rule));
4142
public static notEmpty = (): Validator => new Validator(new rules.NotEmpty());
4243
public static nullType = (): Validator => new Validator(new rules.NullType());
@@ -110,6 +111,7 @@ export class Validator extends AllOf {
110111
public negative = (): this => this.addRule(new rules.Negative());
111112
public nfeAccessKey = (): this => this.addRule(new rules.NfeAccessKey());
112113
public no = (): this => this.addRule(new rules.No());
114+
public noneOf = (...args: Validatable[]): this => this.addRule(new rules.NoneOf(...args));
113115
public not = (rule: Validatable): this => this.addRule(new rules.Not(rule));
114116
public notEmpty = (): this => this.addRule(new rules.NotEmpty());
115117
public nullType = (): this => this.addRule(new rules.NullType());

test/rules/none-of.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 { NoneOf } from '../../src/rules/none-of';
7+
8+
describe('NoneOf', () => {
9+
10+
it('is rule', () => {
11+
assert.instanceOf(new NoneOf(), AbstractRule);
12+
});
13+
14+
it('values is valid', () => {
15+
assert.isTrue(new NoneOf(new AlwaysInvalid(), new AlwaysInvalid()).validate(null));
16+
});
17+
18+
it('values is not valid', () => {
19+
assert.isFalse(new NoneOf(new AlwaysInvalid(), new AlwaysValid()).validate(null));
20+
});
21+
22+
});

test/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe('Validator', () => {
4747
assert.instanceOf(V.negative(), V);
4848
assert.instanceOf(V.nfeAccessKey(), V);
4949
assert.instanceOf(V.no(), V);
50+
assert.instanceOf(V.noneOf(), V);
5051
assert.instanceOf(V.not(V.alwaysInvalid()), V);
5152
assert.instanceOf(V.notEmpty(), V);
5253
assert.instanceOf(V.nullType(), V);
@@ -125,6 +126,7 @@ describe('Validator', () => {
125126
assert.instanceOf(v.negative(), V);
126127
assert.instanceOf(v.nfeAccessKey(), V);
127128
assert.instanceOf(v.no(), V);
129+
assert.instanceOf(v.noneOf(), V);
128130
assert.instanceOf(v.not(v.alwaysInvalid()), V);
129131
assert.instanceOf(v.notEmpty(), V);
130132
assert.instanceOf(v.nullType(), V);

0 commit comments

Comments
 (0)