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

Commit 03569c6

Browse files
feat(rules): Added rules for Symbol
1 parent f833dba commit 03569c6

14 files changed

Lines changed: 319 additions & 1 deletion

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
145145
- [StringInstance](docs/string-instance.md)
146146
- [StringType](docs/string-type.md)
147147
- [StringVal](docs/string-val.md)
148+
- [SymbolInstance](docs/symbol-instance.md)
149+
- [SymbolType](docs/symbol-type.md)
150+
- [SymbolVal](docs/symbol-val.md)
148151
- [SymbolicLink](docs/symbolic-link.md)
149152
- [Tld](docs/tld.md)
150153
- [TrueVal](docs/true-val.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/symbol-instance.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Symbol Instance
2+
3+
Validates if the given input is symbol instance.
4+
5+
Valid values:
6+
7+
```js
8+
validator.symbolInstance().validate(Symbol());
9+
validator.symbolInstance().validate(Symbol('foo'));
10+
validator.symbolInstance().validate(Symbol(10));
11+
```
12+
13+
Invalid values:
14+
15+
```js
16+
validator.symbolInstance().validate(null);
17+
validator.symbolInstance().validate('foo');
18+
validator.symbolInstance().validate(10);
19+
```

docs/symbol-type.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Symbol Type
2+
3+
Validates whether the type of an input is symbol.
4+
5+
Valid values:
6+
7+
```js
8+
validator.symbolType().validate(Symbol());
9+
validator.symbolType().validate(Symbol('foo'));
10+
validator.symbolType().validate(Symbol(10));
11+
```
12+
13+
Invalid values:
14+
15+
```js
16+
validator.symbolType().validate(null);
17+
validator.symbolType().validate('foo');
18+
validator.symbolType().validate(10);
19+
```

docs/symbol-val.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Symbol Val
2+
3+
Validates whether the value of input is symbol.
4+
5+
Valid values:
6+
7+
```js
8+
validator.symbolVal().validate(0);
9+
validator.symbolVal().validate(1);
10+
validator.symbolVal().validate(Number(1));
11+
validator.symbolVal().validate(Number());
12+
validator.symbolVal().validate('');
13+
validator.symbolVal().validate('foo');
14+
validator.symbolVal().validate(String());
15+
validator.symbolVal().validate(String('foo'));
16+
validator.symbolVal().validate(Symbol());
17+
validator.symbolVal().validate(undefined);
18+
```
19+
20+
Invalid values:
21+
22+
```js
23+
validator.symbolVal().validate(null);
24+
validator.symbolVal().validate(false);
25+
validator.symbolVal().validate(true);
26+
validator.symbolVal().validate([]);
27+
validator.symbolVal().validate({});
28+
```

src/rules/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ export * from './space';
6666
export * from './string-instance';
6767
export * from './string-type';
6868
export * from './string-val';
69+
export * from './symbol-instance';
70+
export * from './symbol-type';
71+
export * from './symbol-val';
6972
export * from './symbolic-link';
7073
export * from './tld';
7174
export * from './true-val';

src/rules/symbol-instance.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { AbstractConstructotName } from './abstract-constructor-name';
2+
3+
export class SymbolInstance extends AbstractConstructotName {
4+
5+
/**
6+
* Get constructor name.
7+
*/
8+
protected getConstructorName(): string {
9+
return 'Symbol';
10+
}
11+
}

src/rules/symbol-type.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 { TypeOf } from './type-of';
3+
4+
export class SymbolType extends AbstractRule {
5+
6+
/**
7+
* Validate.
8+
*/
9+
public validate(input: any): boolean {
10+
return new TypeOf(/symbol/i).validate(input);
11+
}
12+
}

src/rules/symbol-val.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 { TypeOf } from './type-of';
3+
4+
export class SymbolVal extends AbstractRule {
5+
6+
/**
7+
* Validate.
8+
*/
9+
public validate(input: any): boolean {
10+
return new TypeOf(/number|string|symbol|undefined/i).validate(input);
11+
}
12+
}

src/validator.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ export class Validator extends AllOf {
7575
public static stringInstance = (): Validator => new Validator(new rules.StringInstance());
7676
public static stringType = (): Validator => new Validator(new rules.StringType());
7777
public static stringVal = (): Validator => new Validator(new rules.StringVal());
78+
public static symbolInstance = (): Validator => new Validator(new rules.SymbolInstance());
79+
public static symbolType = (): Validator => new Validator(new rules.SymbolType());
80+
public static symbolVal = (): Validator => new Validator(new rules.SymbolVal());
7881
public static symbolicLink = (): Validator => new Validator(new rules.SymbolicLink());
7982
public static tld = (): Validator => new Validator(new rules.Tld());
8083
public static trueVal = (): Validator => new Validator(new rules.TrueVal());
@@ -158,6 +161,9 @@ export class Validator extends AllOf {
158161
public stringInstance = (): this => this.addRule(new rules.StringInstance());
159162
public stringType = (): this => this.addRule(new rules.StringType());
160163
public stringVal = (): this => this.addRule(new rules.StringVal());
164+
public symbolInstance = (): this => this.addRule(new rules.SymbolInstance());
165+
public symbolType = (): this => this.addRule(new rules.SymbolType());
166+
public symbolVal = (): this => this.addRule(new rules.SymbolVal());
161167
public symbolicLink = (): this => this.addRule(new rules.SymbolicLink());
162168
public tld = (): this => this.addRule(new rules.Tld());
163169
public trueVal = (): this => this.addRule(new rules.TrueVal());

0 commit comments

Comments
 (0)