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

Commit 483db2b

Browse files
feat(rules): Added Callable rule
1 parent 325c1ba commit 483db2b

6 files changed

Lines changed: 114 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
9494
- [BooleanType](docs/boolean-type.md)
9595
- [BooleanVal](docs/boolean-val.md)
9696
- [Bsn](docs/bsn.md)
97+
- [Callable](docs/callable.md)
9798
- [Charset](docs/charset.md)
9899
- [Cnh](docs/cnh.md)
99100
- [Cnpj](docs/cnpj.md)

awesome-validator.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/callable.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Callable
2+
3+
Validates if the input is a callable.
4+
5+
Valid values:
6+
7+
```js
8+
const obj: any = {
9+
foo(): string {
10+
return 'foo';
11+
}
12+
};
13+
14+
validator.callable().validate((): string => 'foo');
15+
validator.callable().validate('eval');
16+
validator.callable().validate('Array.isArray');
17+
validator.callable().validate([obj, 'foo']);
18+
```
19+
20+
Invalid values:
21+
22+
```js
23+
validator.callable().validate('');
24+
validator.callable().validate('foo');
25+
validator.callable().validate(null);
26+
validator.callable().validate(undefined);
27+
```

src/rules/callable.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { AbstractTryCatch } from './abstract-try-catch';
2+
import { ArrayType } from './array-type';
3+
import { FunctionType } from './function-type';
4+
import { ObjectType } from './object-type';
5+
import { StringType } from './string-type';
6+
7+
export class Callable extends AbstractTryCatch {
8+
9+
/**
10+
* Validate Function.
11+
*/
12+
protected validateFunction(input: any): boolean {
13+
if (new FunctionType().validate(input)) {
14+
return true;
15+
}
16+
17+
if (new StringType().validate(input)) {
18+
/* tslint:disable no-eval */
19+
return new FunctionType().validate(eval(input));
20+
/* tslint:enable no-eval */
21+
}
22+
23+
if (new ArrayType().validate(input) &&
24+
input.length === 2 &&
25+
new ObjectType().validate(input[0]) &&
26+
new StringType().validate(input[1])
27+
) {
28+
return new FunctionType().validate(input[0][input[1]]);
29+
}
30+
31+
return false;
32+
}
33+
}

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export * from './boolean-instance';
1515
export * from './boolean-type';
1616
export * from './boolean-val';
1717
export * from './bsn';
18+
export * from './callable';
1819
export * from './charset';
1920
export * from './cnh';
2021
export * from './cnpj';

test/rules/callable.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { Callable } from '../../src/rules/callable';
5+
6+
describe('Callable', () => {
7+
8+
let callable: Callable;
9+
10+
beforeEach(() => {
11+
callable = new Callable();
12+
});
13+
14+
it('is rule', () => {
15+
assert.instanceOf(callable, AbstractRule);
16+
});
17+
18+
it('values is valid', () => {
19+
const obj: any = {
20+
/**
21+
* Foo.
22+
*/
23+
foo(): string {
24+
return 'foo';
25+
}
26+
};
27+
28+
assert.isTrue(callable.validate((): string => 'foo'));
29+
assert.isTrue(callable.validate('eval'));
30+
assert.isTrue(callable.validate('Array.isArray'));
31+
assert.isTrue(callable.validate([obj, 'foo']));
32+
});
33+
34+
it('values is not valid', () => {
35+
assert.isFalse(callable.validate(null));
36+
assert.isFalse(callable.validate(undefined));
37+
assert.isFalse(callable.validate([]));
38+
assert.isFalse(callable.validate({}));
39+
assert.isFalse(callable.validate(new Array('foo')));
40+
assert.isFalse(callable.validate(new Object({foo: 'bar'})));
41+
assert.isFalse(callable.validate(true));
42+
assert.isFalse(callable.validate(false));
43+
assert.isFalse(callable.validate(1));
44+
assert.isFalse(callable.validate(''));
45+
assert.isFalse(callable.validate('foo'));
46+
});
47+
48+
});

0 commit comments

Comments
 (0)