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

Commit ee5f681

Browse files
feat(rules): Added Graph rule
1 parent 19612de commit ee5f681

8 files changed

Lines changed: 97 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
102102
- [FloatVal](docs/float-val.md)
103103
- [FunctionInstance](docs/function-instance.md)
104104
- [FunctionType](docs/function-type.md)
105+
- [Graph](docs/graph.md)
105106
- [In](docs/in.md)
106107
- [InstanceOf](docs/instance-of.md)
107108
- [IntType](docs/int-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/graph.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Graph
2+
3+
Validates all characters that are graphically represented.
4+
5+
Valid values:
6+
7+
```js
8+
validator.graph().validate('LKA#@%.54');
9+
validator.graph().validate('foobar');
10+
validator.graph().validate('16-50');
11+
validator.graph().validate('123');
12+
validator.graph().validate('#$%&*_');
13+
```
14+
15+
Invalid values:
16+
17+
```js
18+
validator.graph().validate('');
19+
validator.graph().validate(' ');
20+
validator.graph().validate('foo\nbar');
21+
validator.graph().validate('foo\tbar');
22+
validator.graph().validate('foo bar');
23+
validator.graph().validate('( )_{}');
24+
validator.graph().validate(null);
25+
validator.graph().validate(undefined);
26+
```

src/rules/graph.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { AbstractRegexFilter } from './abstract-regex-filter';
2+
3+
export class Graph extends AbstractRegexFilter {
4+
5+
/**
6+
* Get pattern.
7+
*/
8+
protected getPattern(): string | RegExp {
9+
return /^[\u0021-\u007E]+$/g;
10+
}
11+
}

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export * from './float-type';
2323
export * from './float-val';
2424
export * from './function-instance';
2525
export * from './function-type';
26+
export * from './graph';
2627
export * from './in';
2728
export * from './instance-of';
2829
export * from './int-type';

src/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class Validator extends AllOf {
3333
public static floatVal = (): Validator => new Validator(new rules.FloatVal());
3434
public static functionInstance = (): Validator => new Validator(new rules.FunctionInstance());
3535
public static functionType = (): Validator => new Validator(new rules.FunctionType());
36+
public static graph = (additionalChars?: string): Validator => new Validator(new rules.Graph(additionalChars));
3637
public static in = (searcher?: any, contains: boolean = true, identical: boolean = false): Validator => new Validator(new rules.In(searcher, contains, identical));
3738
public static instanceOf = (instanceName: any): Validator => new Validator(new rules.InstanceOf(instanceName));
3839
public static intType = (): Validator => new Validator(new rules.IntType());
@@ -132,6 +133,7 @@ export class Validator extends AllOf {
132133
public floatVal = (): this => this.addRule(new rules.FloatVal());
133134
public functionInstance = (): this => this.addRule(new rules.FunctionInstance());
134135
public functionType = (): this => this.addRule(new rules.FunctionType());
136+
public graph = (additionalChars?: string): this => this.addRule(new rules.Graph(additionalChars));
135137
public in = (searcher?: any, contains: boolean = true, identical: boolean = false): this => this.addRule(new rules.In(searcher, contains, identical));
136138
public instanceOf = (instanceName: any): this => this.addRule(new rules.InstanceOf(instanceName));
137139
public intType = (): this => this.addRule(new rules.IntType());

test/rules/graph.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { Graph } from '../../src/rules/graph';
5+
6+
describe('Graph', () => {
7+
8+
let graph: Graph;
9+
10+
beforeEach(() => {
11+
graph = new Graph();
12+
});
13+
14+
it('is rule', () => {
15+
assert.instanceOf(graph, AbstractRule);
16+
});
17+
18+
it('values is valid with additional characters', () => {
19+
assert.isTrue(new Graph(' ').validate('!@#$%^&*(){} abc 123'));
20+
assert.isTrue(new Graph(' \t\n').validate('[]?+=/\\-_|"\',<>. \t \n abc 123'));
21+
});
22+
23+
it('values is valid', () => {
24+
assert.isTrue(graph.validate('LKA#@%.54'));
25+
assert.isTrue(graph.validate('foobar'));
26+
assert.isTrue(graph.validate('16-50'));
27+
assert.isTrue(graph.validate('123'));
28+
assert.isTrue(graph.validate('#$%&*_'));
29+
});
30+
31+
it('values is not valid', () => {
32+
assert.isFalse(graph.validate(''));
33+
assert.isFalse(graph.validate(null));
34+
assert.isFalse(graph.validate(' '));
35+
assert.isFalse(graph.validate('foo\nbar'));
36+
assert.isFalse(graph.validate('foo\tbar'));
37+
assert.isFalse(graph.validate('foo bar'));
38+
assert.isFalse(graph.validate('( )_{}'));
39+
assert.isFalse(graph.validate(String()));
40+
assert.isFalse(graph.validate(null));
41+
assert.isFalse(graph.validate(undefined));
42+
assert.isFalse(graph.validate([]));
43+
assert.isFalse(graph.validate({}));
44+
assert.isFalse(graph.validate(new Array('foo')));
45+
assert.isFalse(graph.validate(new Object({foo: 'bar'})));
46+
47+
class Foo {}
48+
assert.isFalse(graph.validate(new Foo()));
49+
});
50+
51+
});

test/validator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ describe('Validator', () => {
5252
assert.instanceOf(V.floatVal(), V);
5353
assert.instanceOf(V.functionInstance(), V);
5454
assert.instanceOf(V.functionType(), V);
55+
assert.instanceOf(V.graph(), V);
56+
assert.instanceOf(V.graph('foo'), V);
5557
assert.instanceOf(V.in([]), V);
5658
assert.instanceOf(V.in([], false), V);
5759
assert.instanceOf(V.in([], false, true), V);
@@ -204,6 +206,8 @@ describe('Validator', () => {
204206
assert.instanceOf(v.floatVal(), V);
205207
assert.instanceOf(v.functionInstance(), V);
206208
assert.instanceOf(v.functionType(), V);
209+
assert.instanceOf(v.graph(), V);
210+
assert.instanceOf(v.graph('foo'), V);
207211
assert.instanceOf(v.in([]), V);
208212
assert.instanceOf(v.in([], false), V);
209213
assert.instanceOf(v.in([], false, true), V);

0 commit comments

Comments
 (0)