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

Commit 98c806a

Browse files
feat(rules): Added NotOptional rule
1 parent f031946 commit 98c806a

9 files changed

Lines changed: 109 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
113113
- [NoneOf](docs/none-of.md)
114114
- [Not](docs/not.md)
115115
- [NotEmpty](docs/not-empty.md)
116+
- [NotOptional](docs/not-optional.md)
116117
- [NullType](docs/null-type.md)
117118
- [NumberInstance](docs/number-instance.md)
118119
- [NumberType](docs/number-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/not-optional.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Not Optional
2+
3+
Validates if the given input is not optional.
4+
5+
Valid values:
6+
7+
```js
8+
validator.notOptional().validate(' ');
9+
validator.notOptional().validate('foo');
10+
validator.notOptional().validate(true);
11+
validator.notOptional().validate(false);
12+
validator.notOptional().validate(0);
13+
validator.notOptional().validate(1);
14+
validator.notOptional().validate([]);
15+
validator.notOptional().validate({});
16+
```
17+
18+
Invalid values:
19+
20+
```js
21+
validator.notOptional().validate('');
22+
validator.notOptional().validate(null);
23+
```

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export * from './no';
3434
export * from './none-of';
3535
export * from './not';
3636
export * from './not-empty';
37+
export * from './not-optional';
3738
export * from './null-type';
3839
export * from './number-instance';
3940
export * from './number-type';

src/rules/not-optional.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 { In } from './in';
3+
4+
export class NotOptional extends AbstractRule {
5+
6+
/**
7+
* Validate.
8+
*/
9+
public validate(input: any): boolean {
10+
return !new In([null, ''], false).validate(input);
11+
}
12+
}

src/rules/optional.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
12
import { AbstractWrapper } from './abstract-wrapper';
2-
import { In } from './in';
3+
import { NotOptional } from './not-optional';
34

45
export class Optional extends AbstractWrapper {
56

67
/**
78
* Validate.
89
*/
910
public validate(input: any): boolean {
10-
return new In([null, ''], false).validate(input) || super.validate(input);
11+
return !new NotOptional().validate(input) || super.validate(input);
1112
}
1213
}

src/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class Validator extends AllOf {
4343
public static noneOf = (...args: Validatable[]): Validator => new Validator(new rules.NoneOf(...args));
4444
public static not = (rule: Validatable): Validator => new Validator(new rules.Not(rule));
4545
public static notEmpty = (): Validator => new Validator(new rules.NotEmpty());
46+
public static notOptional = (): Validator => new Validator(new rules.NotOptional());
4647
public static nullType = (): Validator => new Validator(new rules.NullType());
4748
public static numberInstance = (): Validator => new Validator(new rules.NumberInstance());
4849
public static numberType = (): Validator => new Validator(new rules.NumberType());
@@ -124,6 +125,7 @@ export class Validator extends AllOf {
124125
public noneOf = (...args: Validatable[]): this => this.addRule(new rules.NoneOf(...args));
125126
public not = (rule: Validatable): this => this.addRule(new rules.Not(rule));
126127
public notEmpty = (): this => this.addRule(new rules.NotEmpty());
128+
public notOptional = (): this => this.addRule(new rules.NotOptional());
127129
public nullType = (): this => this.addRule(new rules.NullType());
128130
public numberInstance = (): this => this.addRule(new rules.NumberInstance());
129131
public numberType = (): this => this.addRule(new rules.NumberType());

test/rules/not-optional.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { NotOptional } from '../../src/rules/not-optional';
5+
6+
describe('NotOptional', () => {
7+
8+
let notOptional: NotOptional;
9+
10+
beforeEach(() => {
11+
notOptional = new NotOptional();
12+
});
13+
14+
it('is rule', () => {
15+
assert.instanceOf(notOptional, AbstractRule);
16+
});
17+
18+
it('values is valid', () => {
19+
assert.isTrue(notOptional.validate(Boolean()));
20+
assert.isTrue(notOptional.validate(Boolean(true)));
21+
assert.isTrue(notOptional.validate(Boolean(false)));
22+
assert.isTrue(notOptional.validate(Boolean('1')));
23+
assert.isTrue(notOptional.validate(Boolean('0')));
24+
assert.isTrue(notOptional.validate(true));
25+
assert.isTrue(notOptional.validate(false));
26+
assert.isTrue(notOptional.validate(Number()));
27+
assert.isTrue(notOptional.validate(undefined));
28+
assert.isTrue(notOptional.validate('true'));
29+
assert.isTrue(notOptional.validate('false'));
30+
assert.isTrue(notOptional.validate('-1.1'));
31+
assert.isTrue(notOptional.validate('1.1'));
32+
assert.isTrue(notOptional.validate('1'));
33+
assert.isTrue(notOptional.validate('-1'));
34+
assert.isTrue(notOptional.validate('0'));
35+
assert.isTrue(notOptional.validate(' '));
36+
assert.isTrue(notOptional.validate(' '));
37+
assert.isTrue(notOptional.validate(0));
38+
assert.isTrue(notOptional.validate(-0));
39+
assert.isTrue(notOptional.validate(1));
40+
assert.isTrue(notOptional.validate(-1));
41+
assert.isTrue(notOptional.validate(0.0));
42+
assert.isTrue(notOptional.validate(-0.0));
43+
assert.isTrue(notOptional.validate(1.0));
44+
assert.isTrue(notOptional.validate(-1.0));
45+
assert.isTrue(notOptional.validate(0.1));
46+
assert.isTrue(notOptional.validate(-0.1));
47+
assert.isTrue(notOptional.validate(1.1));
48+
assert.isTrue(notOptional.validate(-1.1));
49+
assert.isTrue(notOptional.validate([]));
50+
assert.isTrue(notOptional.validate({}));
51+
assert.isTrue(notOptional.validate(new Array('foo')));
52+
assert.isTrue(notOptional.validate(new Object({foo: 'bar'})));
53+
54+
class Foo {}
55+
assert.isTrue(notOptional.validate(new Foo()));
56+
});
57+
58+
it('values is not valid', () => {
59+
assert.isFalse(notOptional.validate(''));
60+
assert.isFalse(notOptional.validate(null));
61+
assert.isFalse(notOptional.validate(String()));
62+
});
63+
64+
});

test/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe('Validator', () => {
5959
assert.instanceOf(V.noneOf(V.alwaysValid()), V);
6060
assert.instanceOf(V.not(V.alwaysValid()), V);
6161
assert.instanceOf(V.notEmpty(), V);
62+
assert.instanceOf(V.notOptional(), V);
6263
assert.instanceOf(V.nullType(), V);
6364
assert.instanceOf(V.numberInstance(), V);
6465
assert.instanceOf(V.numberType(), V);
@@ -163,6 +164,7 @@ describe('Validator', () => {
163164
assert.instanceOf(v.noneOf(V.alwaysValid()), V);
164165
assert.instanceOf(v.not(V.alwaysValid()), V);
165166
assert.instanceOf(v.notEmpty(), V);
167+
assert.instanceOf(v.notOptional(), V);
166168
assert.instanceOf(v.nullType(), V);
167169
assert.instanceOf(v.numberInstance(), V);
168170
assert.instanceOf(v.numberType(), V);

0 commit comments

Comments
 (0)