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

Commit faf664f

Browse files
feat(rules): Added KeyValue rule
1 parent 161416e commit faf664f

6 files changed

Lines changed: 80 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
146146
- [Iterable](docs/iterable.md)
147147
- [Json](docs/json.md)
148148
- [KeyNested](docs/key-nested.md)
149+
- [KeyValue](docs/key-value.md)
149150
- [Key](docs/key.md)
150151
- [Label](docs/label.md)
151152
- [LanguageCode](docs/language-code.md)

awesome-validator.min.js

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

docs/key-value.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Key Value
2+
3+
Valid values:
4+
5+
```js
6+
validator.keyValue('foo', 'equals', 'bar').validate({foo: 'foobar', bar: 'foobar'});
7+
validator.keyValue('foo', 'equals', 'bar').validate({foo: 10, bar: 10});
8+
```
9+
10+
Invalid values:
11+
12+
```js
13+
validator.keyValue('invalidCompared', 'equals', 'bar').validate({foz: 'foobar', bar: 'foobar'});
14+
validator.keyValue('foo', 'invalidRule', 'bar').validate({foo: 'foobar', bar: 'foobar'});
15+
validator.keyValue('foo', 'equals', 'invalidBase').validate({foo: 'foobar', baz: 'foobar'});
16+
validator.keyValue('foo', 'equals', 'bar').validate({foo: 'foobar', bar: 'foobaz'});
17+
```

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export * from './ip';
6767
export * from './iterable';
6868
export * from './json';
6969
export * from './key-nested';
70+
export * from './key-value';
7071
export * from './key';
7172
export * from './label';
7273
export * from './language-code';

src/rules/key-value.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Validatable } from './../validatable';
2+
import { Validator } from './../validator';
3+
import { AbstractRule } from './abstract-rule';
4+
import { Key } from './key';
5+
6+
export class KeyValue extends AbstractRule {
7+
8+
/**
9+
* KeyValue.
10+
*/
11+
public constructor(
12+
protected readonly comparedKey: any,
13+
protected readonly ruleName: any,
14+
protected readonly baseKey: any
15+
) {
16+
super();
17+
}
18+
19+
/**
20+
* Validate.
21+
*/
22+
public validate(input: any): boolean {
23+
const ruleCompared: Key = new Key(this.comparedKey);
24+
const ruleBase: Key = new Key(this.baseKey);
25+
26+
if (!ruleCompared.validate(input) || !ruleBase.validate(input) || !(this.ruleName in Validator)) {
27+
return false;
28+
}
29+
30+
const rule: Validatable = Validator[this.ruleName](ruleBase.getReferenceValueStored());
31+
32+
return rule.validate(ruleCompared.getReferenceValueStored());
33+
}
34+
}

test/rules/key-value.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { KeyValue } from '../../src/rules/key-value';
5+
6+
describe('KeyValue', () => {
7+
8+
it('is rule', () => {
9+
assert.instanceOf(new KeyValue('foo', 'alwaysValid', 'foo'), AbstractRule);
10+
});
11+
12+
it('values is valid', () => {
13+
assert.isTrue(new KeyValue('foo', 'equals', 'bar').validate({foo: 'foobar', bar: 'foobar'}));
14+
assert.isTrue(new KeyValue('foo', 'equals', 'bar').validate({foo: 10, bar: 10}));
15+
});
16+
17+
it('values is not valid', () => {
18+
assert.isFalse(new KeyValue('invalidCompared', 'equals', 'bar').validate({foz: 'foobar', bar: 'foobar'}));
19+
assert.isFalse(new KeyValue('foo', 'invalidRule', 'bar').validate({foo: 'foobar', bar: 'foobar'}));
20+
assert.isFalse(new KeyValue('foo', 'equals', 'invalidBase').validate({foo: 'foobar', baz: 'foobar'}));
21+
assert.isFalse(new KeyValue('foo', 'equals', 'bar').validate({foo: 'foobar', bar: 'foobaz'}));
22+
});
23+
24+
});

0 commit comments

Comments
 (0)