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

Commit 6da2645

Browse files
feat(rules): Added Range rule
1 parent 2526fcf commit 6da2645

8 files changed

Lines changed: 163 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
141141
- [PrimeNumber](docs/prime-number.md)
142142
- [Prnt](docs/prnt.md)
143143
- [Punct](docs/punct.md)
144+
- [Range](docs/range.md)
144145
- [RegexInstance](docs/regex-instance.md)
145146
- [RegexType](docs/regex-type.md)
146147
- [RegexVal](docs/regex-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/range.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Range
2+
3+
Validates lengths.
4+
5+
Valid values:
6+
7+
```js
8+
validator.range(1).validate('foo'));
9+
validator.range(100).validate(100));
10+
validator.range(100, 200, false).validate(150));
11+
validator.range(1).validate({foo: 'bar'}));
12+
validator.range(1).validate([1]));
13+
```
14+
15+
Invalid values:
16+
17+
```js
18+
validator.range(5).validate('foo'));
19+
validator.range(3, 5, false).validate('foo'));
20+
validator.range(100).validate(50));
21+
validator.range(100, 200, false).validate(100));
22+
validator.range(2).validate({foo: 'bar'}));
23+
validator.range(2).validate([1]));
24+
```

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export * from './positive';
6262
export * from './prime-number';
6363
export * from './prnt';
6464
export * from './punct';
65+
export * from './range';
6566
export * from './regex-instance';
6667
export * from './regex-type';
6768
export * from './regex-val';

src/rules/range.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { AbstractRule } from './abstract-rule';
2+
import { AllOf } from './all-of';
3+
import { ArrayType } from './array-type';
4+
import { Max } from './max';
5+
import { Min } from './min';
6+
import { NumberVal } from './number-val';
7+
import { ObjectTypeStrict } from './object-type-strict';
8+
9+
export class Range extends AbstractRule {
10+
11+
/**
12+
* Parse.
13+
*/
14+
public static parse(input: any): number {
15+
if (new NumberVal().validate(input)) {
16+
return Number(input);
17+
}
18+
19+
if (new ObjectTypeStrict().validate(input) || new ArrayType().validate(input)) {
20+
return Object.keys(input).length;
21+
}
22+
23+
if (input instanceof Set || input instanceof Map) {
24+
return input.size;
25+
}
26+
27+
return String(input).length;
28+
}
29+
30+
/**
31+
* Length.
32+
*/
33+
public constructor(
34+
public readonly min?: number | null,
35+
public readonly max?: number | null,
36+
public readonly inclusive: boolean = true
37+
) {
38+
super();
39+
}
40+
41+
/**
42+
* Validate.
43+
*/
44+
public validate(input: any): boolean {
45+
return new AllOf(
46+
new Min(this.min, this.inclusive),
47+
new Max(this.max, this.inclusive)
48+
).validate(Range.parse(input));
49+
}
50+
}

src/validator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class Validator extends AllOf {
3838
public static intVal = (): Validator => new Validator(new rules.IntVal());
3939
public static json = (): Validator => new Validator(new rules.Json());
4040
public static label = (): Validator => new Validator(new rules.Label());
41-
public static leapDate= (format?: momment.MomentFormatSpecification): Validator => new Validator(new rules.LeapDate(format));
41+
public static leapDate = (format?: momment.MomentFormatSpecification): Validator => new Validator(new rules.LeapDate(format));
4242
public static leapYear = (format?: momment.MomentFormatSpecification): Validator => new Validator(new rules.LeapYear(format));
4343
public static lowercase = (): Validator => new Validator(new rules.Lowercase());
4444
public static luhn = (): Validator => new Validator(new rules.Luhn());
@@ -72,6 +72,7 @@ export class Validator extends AllOf {
7272
public static primeNumber = (): Validator => new Validator(new rules.PrimeNumber());
7373
public static prnt = (additionalChars?: string): Validator => new Validator(new rules.Prnt(additionalChars));
7474
public static punct = (additionalChars?: string): Validator => new Validator(new rules.Punct(additionalChars));
75+
public static range = (min?: number | null, max?: number | null, inclusive: boolean = true): Validator => new Validator(new rules.Range(min, max, inclusive));
7576
public static regexInstance = (): Validator => new Validator(new rules.RegexInstance());
7677
public static regexType = (): Validator => new Validator(new rules.RegexType());
7778
public static regexVal = (): Validator => new Validator(new rules.RegexVal());
@@ -168,6 +169,7 @@ export class Validator extends AllOf {
168169
public primeNumber = (): this => this.addRule(new rules.PrimeNumber());
169170
public prnt = (additionalChars?: string): this => this.addRule(new rules.Prnt(additionalChars));
170171
public punct = (additionalChars?: string): this => this.addRule(new rules.Punct(additionalChars));
172+
public range = (min?: number | null, max?: number | null, inclusive: boolean = true): this => this.addRule(new rules.Range(min, max, inclusive));
171173
public regexInstance = (): this => this.addRule(new rules.RegexInstance());
172174
public regexType = (): this => this.addRule(new rules.RegexType());
173175
public regexVal = (): this => this.addRule(new rules.RegexVal());

test/rules/range.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { Range } from '../../src/rules/range';
5+
6+
describe('Range', () => {
7+
8+
it('is rule', () => {
9+
assert.instanceOf(new Range(), AbstractRule);
10+
});
11+
12+
it('values is valid', () => {
13+
assert.isTrue(new Range(1).validate('foo'));
14+
assert.isTrue(new Range(null, 5).validate('foo'));
15+
assert.isTrue(new Range(1, 5).validate('foo'));
16+
assert.isTrue(new Range(1, 5, false).validate('foo'));
17+
assert.isTrue(new Range().validate(0));
18+
assert.isTrue(new Range(100).validate(100));
19+
assert.isTrue(new Range(100, 200).validate(100));
20+
assert.isTrue(new Range(null, 200).validate(150));
21+
assert.isTrue(new Range(100, 200, false).validate(150));
22+
assert.isTrue(new Range(1).validate({foo: 'bar'}));
23+
assert.isTrue(new Range(1, 5).validate({foo: 'bar'}));
24+
assert.isTrue(new Range(1, 5, false).validate({foo: 'bar', bar: 'foo'}));
25+
assert.isTrue(new Range(0, 5).validate({}));
26+
assert.isTrue(new Range(1).validate([1]));
27+
assert.isTrue(new Range(1, 5).validate([1]));
28+
assert.isTrue(new Range(1, 5, false).validate([1, 2]));
29+
assert.isTrue(new Range(0, 5).validate([]));
30+
assert.isTrue(new Range(1).validate(new Set([1, 2, 3])));
31+
assert.isTrue(new Range(null, 5).validate(new Set([1, 2, 3])));
32+
assert.isTrue(new Range(1, 5).validate(new Set([1, 2, 3])));
33+
assert.isTrue(new Range(1, 5, false).validate(new Set([1, 2, 3])));
34+
assert.isTrue(new Range(1).validate(new Map<number, number>([[1, 1], [2, 2], [3, 3]])));
35+
assert.isTrue(new Range(null, 5).validate(new Map<number, number>([[1, 1], [2, 2], [3, 3]])));
36+
assert.isTrue(new Range(1, 5).validate(new Map<number, number>([[1, 1], [2, 2], [3, 3]])));
37+
assert.isTrue(new Range(1, 5, false).validate(new Map<number, number>([[1, 1], [2, 2], [3, 3]])));
38+
});
39+
40+
it('values is not valid', () => {
41+
assert.isFalse(new Range(5).validate('foo'));
42+
assert.isFalse(new Range(null, 5).validate('foobar'));
43+
assert.isFalse(new Range(5, 10).validate('foo'));
44+
assert.isFalse(new Range(3, 5, false).validate('foo'));
45+
assert.isFalse(new Range(100).validate(50));
46+
assert.isFalse(new Range(100, 200).validate(50));
47+
assert.isFalse(new Range(100, 200, false).validate(100));
48+
assert.isFalse(new Range(null, 200).validate(250));
49+
assert.isFalse(new Range(2).validate({foo: 'bar'}));
50+
assert.isFalse(new Range(2, 5).validate({foo: 'bar'}));
51+
assert.isFalse(new Range(2, 5, false).validate({foo: 'bar', bar: 'foo'}));
52+
assert.isFalse(new Range(0, 5, false).validate({}));
53+
assert.isFalse(new Range(2).validate([1]));
54+
assert.isFalse(new Range(2, 5).validate([1]));
55+
assert.isFalse(new Range(2, 5, false).validate([1, 2]));
56+
assert.isFalse(new Range(0, 5, false).validate([]));
57+
assert.isFalse(new Range(5).validate(new Set([1, 2, 3])));
58+
assert.isFalse(new Range(null, 2).validate(new Set([1, 2, 3])));
59+
assert.isFalse(new Range(5, 10).validate(new Set([1, 2, 3])));
60+
assert.isFalse(new Range(3, 5, false).validate(new Set([1, 2, 3])));
61+
assert.isFalse(new Range(5).validate(new Map<number, number>([[1, 1], [2, 2], [3, 3]])));
62+
assert.isFalse(new Range(null, 2).validate(new Map<number, number>([[1, 1], [2, 2], [3, 3]])));
63+
assert.isFalse(new Range(5, 10).validate(new Map<number, number>([[1, 1], [2, 2], [3, 3]])));
64+
assert.isFalse(new Range(3, 5, false).validate(new Map<number, number>([[1, 1], [2, 2], [3, 3]])));
65+
});
66+
67+
});

test/validator.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ describe('Validator', () => {
6161
assert.instanceOf(V.luhn(), V);
6262
assert.instanceOf(V.macAddress(), V);
6363
assert.instanceOf(V.max(), V);
64+
assert.instanceOf(V.max(null), V);
65+
assert.instanceOf(V.max(undefined), V);
6466
assert.instanceOf(V.max(100), V);
6567
assert.instanceOf(V.max(100, false), V);
6668
assert.instanceOf(V.min(), V);
69+
assert.instanceOf(V.min(null), V);
70+
assert.instanceOf(V.min(undefined), V);
6771
assert.instanceOf(V.min(100), V);
6872
assert.instanceOf(V.min(100, false), V);
6973
assert.instanceOf(V.multiple(0), V);
@@ -98,6 +102,12 @@ describe('Validator', () => {
98102
assert.instanceOf(V.prnt('foo'), V);
99103
assert.instanceOf(V.punct(), V);
100104
assert.instanceOf(V.punct('foo'), V);
105+
assert.instanceOf(V.range(), V);
106+
assert.instanceOf(V.range(100), V);
107+
assert.instanceOf(V.range(100, 100), V);
108+
assert.instanceOf(V.range(100, 200, false), V);
109+
assert.instanceOf(V.range(null, 100), V);
110+
assert.instanceOf(V.range(undefined, 100), V);
101111
assert.instanceOf(V.regexInstance(), V);
102112
assert.instanceOf(V.regexType(), V);
103113
assert.instanceOf(V.regexVal(), V);
@@ -227,6 +237,12 @@ describe('Validator', () => {
227237
assert.instanceOf(v.prnt('foo'), V);
228238
assert.instanceOf(v.punct(), V);
229239
assert.instanceOf(v.punct('foo'), V);
240+
assert.instanceOf(v.range(), V);
241+
assert.instanceOf(v.range(100), V);
242+
assert.instanceOf(v.range(100, 100), V);
243+
assert.instanceOf(v.range(100, 200, false), V);
244+
assert.instanceOf(v.range(null, 100), V);
245+
assert.instanceOf(v.range(undefined, 100), V);
230246
assert.instanceOf(v.regexInstance(), V);
231247
assert.instanceOf(v.regexType(), V);
232248
assert.instanceOf(v.regexVal(), V);

0 commit comments

Comments
 (0)