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

Commit c415c05

Browse files
feat(rules): Added Unique rule
1 parent bb71cf6 commit c415c05

File tree

8 files changed

+102
-1
lines changed

8 files changed

+102
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
121121
- [Tld](docs/tld.md)
122122
- [TrueVal](docs/true-val.md)
123123
- [TypeOf](docs/type-of.md)
124+
- [Unique](docs/unique.md)
124125
- [Uppercase](docs/uppercase.md)
125126
- [Url](docs/url.md)
126127
- [Uuid](docs/uuid.md)

awesome-validator.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/unique.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Unique
2+
3+
Validates whether the input array contains only unique values.
4+
5+
Valid values:
6+
7+
```js
8+
validator.unique().validate([]);
9+
validator.unique().validate([1, 2, 3]);
10+
validator.unique().validate([true, false]);
11+
validator.unique().validate(['alpha', 'beta', 'gamma', 'delta']);
12+
validator.unique().validate([0, 2.71, 3.14]);
13+
validator.unique().validate([[], ['str'], [1]]);
14+
validator.unique().validate([{'key': 'value'}, {'other_key': 'value'}]);
15+
```
16+
17+
Invalid values:
18+
19+
```js
20+
validator.unique().validate('test');
21+
validator.unique().validate([1, 2, 2, 3]);
22+
validator.unique().validate([1, 2, 3, 1]);
23+
validator.unique().validate([true, false, false]);
24+
validator.unique().validate(['alpha', 'beta', 'gamma', 'delta', 'beta']);
25+
validator.unique().validate([0, 3.14, 2.71, 3.14]);
26+
validator.unique().validate([[], [1], [1]]);
27+
validator.unique().validate([{'key': 'value'}, {'key': 'value'}]);
28+
validator.unique().validate([1, true, 'test']);
29+
```

src/rules/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export * from './string-type';
4242
export * from './tld';
4343
export * from './true-val';
4444
export * from './type-of';
45+
export * from './unique';
4546
export * from './uppercase';
4647
export * from './url';
4748
export * from './uuid';

src/rules/unique.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ArrayVal } from './array-val';
2+
3+
export class Unique extends ArrayVal {
4+
5+
/**
6+
* Validate.
7+
*/
8+
public validate(input: any): boolean {
9+
if (!super.validate(input)) {
10+
return false;
11+
}
12+
13+
const unique: any[] = input
14+
.map((value: any) => JSON.stringify(value))
15+
.reverse()
16+
.filter((e: any, i: number, a: any[]) => a.indexOf(e, i + 1) === -1)
17+
.reverse()
18+
.map((value: any) => JSON.parse(value))
19+
;
20+
21+
return JSON.stringify(input) === JSON.stringify(unique);
22+
}
23+
}

src/validator.ts

+7
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ export class Validator extends rules.AllOf {
313313
return this.addRule(new rules.TypeOf(pattern));
314314
}
315315

316+
/**
317+
* Unique.
318+
*/
319+
public unique(): this {
320+
return this.addRule(new rules.Unique());
321+
}
322+
316323
/**
317324
* Uppercase.
318325
*/

test/rules/unique.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { Unique } from '../../src/rules/unique';
5+
6+
describe('Unique', () => {
7+
8+
let unique: Unique;
9+
10+
beforeEach(() => {
11+
unique = new Unique();
12+
});
13+
14+
it('is rule', () => {
15+
assert.instanceOf(unique, AbstractRule);
16+
});
17+
18+
it('values is valid', () => {
19+
assert.isTrue(unique.validate([]));
20+
assert.isTrue(unique.validate([1, 2, 3]));
21+
assert.isTrue(unique.validate([true, false]));
22+
assert.isTrue(unique.validate(['alpha', 'beta', 'gamma', 'delta']));
23+
assert.isTrue(unique.validate([0, 2.71, 3.14]));
24+
assert.isTrue(unique.validate([[], ['str'], [1]]));
25+
assert.isTrue(unique.validate([{key: 'value'}, {other_key: 'value'}]));
26+
});
27+
28+
it('values is not valid', () => {
29+
assert.isFalse(unique.validate('test'));
30+
assert.isFalse(unique.validate([1, 2, 2, 3]));
31+
assert.isFalse(unique.validate([1, 2, 3, 1]));
32+
assert.isFalse(unique.validate([true, false, false]));
33+
assert.isFalse(unique.validate(['alpha', 'beta', 'gamma', 'delta', 'beta']));
34+
assert.isFalse(unique.validate([0, 3.14, 2.71, 3.14]));
35+
assert.isFalse(unique.validate([[], [1], [1]]));
36+
assert.isFalse(unique.validate([{key: 'value'}, {key: 'value'}]));
37+
});
38+
39+
});

test/validator.ts

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe('Validator', () => {
5959
assert.instanceOf(validator.tld(), Validator);
6060
assert.instanceOf(validator.trueVal(), Validator);
6161
assert.instanceOf(validator.typeOf(/foo/), Validator);
62+
assert.instanceOf(validator.unique(), Validator);
6263
assert.instanceOf(validator.uppercase(), Validator);
6364
assert.instanceOf(validator.url(), Validator);
6465
assert.instanceOf(validator.uuid(), Validator);

0 commit comments

Comments
 (0)