We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
OneOf
1 parent 471f0ba commit 5fa9139Copy full SHA for 5fa9139
8 files changed
README.md
@@ -121,6 +121,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
121
- [ObjectTypeStrict](docs/object-type-strict.md)
122
- [ObjectType](docs/object-type.md)
123
- [Odd](docs/odd.md)
124
+- [OneOf](docs/one-of.md)
125
- [PerfectSquare](docs/perfect-square.md)
126
- [Pesel](docs/pesel.md)
127
- [Phone](docs/phone.md)
awesome-validator.min.js
docs/one-of.md
@@ -0,0 +1,23 @@
1
+# One Of
2
+
3
+Will validate if exactly one inner validator passes.
4
5
+Valid values:
6
7
+```js
8
+validator.oneOf(
9
+ validator.alwaysInvalid(),
10
11
+ validator.alwaysValid()
12
+).validate(null);
13
+```
14
15
+Invalid values:
16
17
18
19
20
+ validator.alwaysValid(),
21
22
23
src/rules/index.ts
@@ -42,6 +42,7 @@ export * from './object-instance';
42
export * from './object-type-strict';
43
export * from './object-type';
44
export * from './odd';
45
+export * from './one-of';
46
export * from './perfect-square';
47
export * from './pesel';
48
export * from './phone';
src/rules/one-of.ts
@@ -0,0 +1,21 @@
+import { Validatable } from './../validatable';
+import { AbstractComposite } from './abstract-composite';
+export class OneOf extends AbstractComposite {
+ /**
+ * Validate.
+ */
+ public validate(input: any): boolean {
+ const rules: Validatable[] = this.getRules();
+ let rulesPassedCount: number = 0;
+ for (const rule of rules) {
+ if (rule.validate(input)) {
+ rulesPassedCount++;
+ }
+ return rulesPassedCount === 1;
+}
src/validator.ts
@@ -51,6 +51,7 @@ export class Validator extends AllOf {
51
public static objectTypeStrict = (): Validator => new Validator(new rules.ObjectTypeStrict());
52
public static objectType = (): Validator => new Validator(new rules.ObjectType());
53
public static odd = (): Validator => new Validator(new rules.Odd());
54
+ public static oneOf = (...args: Validatable[]): Validator => new Validator(new rules.OneOf(...args));
55
public static perfectSquare = (): Validator => new Validator(new rules.PerfectSquare());
56
public static pesel = (): Validator => new Validator(new rules.Pesel());
57
public static phone = (): Validator => new Validator(new rules.Phone());
@@ -130,6 +131,7 @@ export class Validator extends AllOf {
130
131
public objectTypeStrict = (): this => this.addRule(new rules.ObjectTypeStrict());
132
public objectType = (): this => this.addRule(new rules.ObjectType());
133
public odd = (): this => this.addRule(new rules.Odd());
134
+ public oneOf = (...args: Validatable[]): this => this.addRule(new rules.OneOf(...args));
135
public perfectSquare = (): this => this.addRule(new rules.PerfectSquare());
136
public pesel = (): this => this.addRule(new rules.Pesel());
137
public phone = (): this => this.addRule(new rules.Phone());
test/rules/one-of.ts
@@ -0,0 +1,26 @@
+import { assert } from 'chai';
+import { AbstractRule } from '../../src/rules/abstract-rule';
+import { AlwaysInvalid } from '../../src/rules/always-invalid';
+import { AlwaysValid } from '../../src/rules/always-valid';
+import { OneOf } from '../../src/rules/one-of';
+describe('OneOf', () => {
+ it('is rule', () => {
+ assert.instanceOf(new OneOf(), AbstractRule);
+ });
+ it('values is valid', () => {
+ assert.isTrue(new OneOf(new AlwaysValid()).validate(null));
+ assert.isTrue(new OneOf(new AlwaysValid(), new AlwaysInvalid()).validate(null));
+ assert.isTrue(new OneOf(new AlwaysValid(), new AlwaysInvalid(), new AlwaysInvalid()).validate(null));
+ it('values is not valid', () => {
+ assert.isFalse(new OneOf(new AlwaysInvalid()).validate(null));
+ assert.isFalse(new OneOf(new AlwaysInvalid(), new AlwaysInvalid()).validate(null));
+ assert.isFalse(new OneOf(new AlwaysInvalid(), new AlwaysValid(), new AlwaysValid()).validate(null));
24
25
26
+});
test/validator.ts
@@ -67,6 +67,8 @@ describe('Validator', () => {
67
assert.instanceOf(V.objectTypeStrict(), V);
68
assert.instanceOf(V.objectType(), V);
69
assert.instanceOf(V.odd(), V);
70
+ assert.instanceOf(V.oneOf(), V);
71
+ assert.instanceOf(V.oneOf(V.alwaysValid()), V);
72
assert.instanceOf(V.perfectSquare(), V);
73
assert.instanceOf(V.pesel(), V);
74
assert.instanceOf(V.phone(), V);
@@ -168,6 +170,8 @@ describe('Validator', () => {
168
170
assert.instanceOf(v.objectTypeStrict(), V);
169
171
assert.instanceOf(v.objectType(), V);
172
assert.instanceOf(v.odd(), V);
173
+ assert.instanceOf(v.oneOf(), V);
174
+ assert.instanceOf(v.oneOf(V.alwaysValid()), V);
175
assert.instanceOf(v.perfectSquare(), V);
176
assert.instanceOf(v.pesel(), V);
177
assert.instanceOf(v.phone(), V);
0 commit comments