Skip to content

Commit b6e739c

Browse files
Add AnyOf assert
1 parent a90a45e commit b6e739c

6 files changed

Lines changed: 287 additions & 1 deletion

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The following set of extra asserts are provided by this package:
2929
| Assert | Peer Dependency |
3030
| :------------------------------------------------------------------------------ | :--------------------------------------------------- |
3131
| [AbaRoutingNumber](#abaroutingnumber) | [`abavalidator`][abavalidator-url] |
32+
| [AnyOf](#anyof) | |
3233
| [BankIdentifierCode](#bankidentifiercode-bic) (_BIC_) | |
3334
| [BigNumber](#bignumber) | [`bignumber.js`][bignumber-url] |
3435
| [BigNumberEqualTo](#bignumberequalto) | [`bignumber.js`][bignumber-url] |
@@ -74,6 +75,14 @@ The following set of extra asserts are provided by this package:
7475

7576
Tests if the value is a valid [ABA Routing Number](http://www.accuity.com/PageFiles/255/ROUTING_NUMBER_POLICY.pdf).
7677

78+
### AnyOf
79+
80+
Tests if the value matches at least one of the provided constraint sets. Throws a violation if the value matches none of the constraint sets.
81+
82+
#### Arguments
83+
84+
- `...constraintSets` (required) - two or more constraint sets to test the value against. Each constraint set can be either a plain object mapping field names to arrays of asserts, or an assert instance.
85+
7786
### BankIdentifierCode (_BIC_)
7887

7988
Tests if the value is a valid Bank Identifier Code (_BIC_) as defined in the [ISO-9362](http://www.iso.org/iso/home/store/catalogue_tc/catalogue_detail.htm?csnumber=60390) standard.

src/asserts/any-of-assert.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const { Constraint, Validator, Violation } = require('validator.js');
8+
9+
/**
10+
* Export `AnyOfAssert`.
11+
*/
12+
13+
module.exports = function anyOfAssert(...constraintSets) {
14+
/**
15+
* Class name.
16+
*/
17+
18+
this.__class__ = 'AnyOf';
19+
20+
if (constraintSets.length < 2) {
21+
throw new Error('AnyOf constraint requires at least two constraint sets');
22+
}
23+
24+
/**
25+
* Validator instance.
26+
*/
27+
28+
this.validator = new Validator();
29+
30+
/**
31+
* Validation algorithm.
32+
*/
33+
34+
this.validate = value => {
35+
const violations = [];
36+
37+
for (const constraintSet of constraintSets) {
38+
try {
39+
let result;
40+
41+
if (Array.isArray(constraintSet)) {
42+
for (const assert of constraintSet) {
43+
assert.validate(value);
44+
}
45+
46+
result = true;
47+
} else if (typeof constraintSet.validate === 'function') {
48+
result = constraintSet.validate(value);
49+
} else {
50+
const normalized = Object.fromEntries(
51+
Object.entries(constraintSet).map(([key, value]) => [key, Array.isArray(value) ? value.flat() : [value]])
52+
);
53+
54+
result = this.validator.validate(value, new Constraint(normalized, { deepRequired: true }));
55+
}
56+
57+
if (result === true) {
58+
return true;
59+
}
60+
61+
violations.push(result);
62+
} catch (violation) {
63+
violations.push(violation);
64+
}
65+
}
66+
67+
throw new Violation(this, value, violations);
68+
};
69+
70+
return this;
71+
};

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
const AbaRoutingNumber = require('./asserts/aba-routing-number-assert.js');
8+
const AnyOf = require('./asserts/any-of-assert.js');
89
const BankIdentifierCode = require('./asserts/bank-identifier-code-assert.js');
910
const BigNumber = require('./asserts/big-number-assert.js');
1011
const BigNumberEqualTo = require('./asserts/big-number-equal-to-assert.js');
@@ -52,6 +53,7 @@ const Uuid = require('./asserts/uuid-assert.js');
5253

5354
module.exports = {
5455
AbaRoutingNumber,
56+
AnyOf,
5557
BankIdentifierCode,
5658
BigNumber,
5759
BigNumberEqualTo,

src/types/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export interface ValidatorJSAsserts {
2727
*/
2828
abaRoutingNumber(): AssertInstance;
2929

30+
/** Value matches one or more of the provided constraint sets. */
31+
anyOf(...constraintSets: (Record<string, (AssertInstance | AssertInstance[])[]> | AssertInstance | AssertInstance[])[]): AssertInstance;
32+
3033
/** Valid BIC (Bank Identifier Code) used for international wire transfers. */
3134
bankIdentifierCode(): AssertInstance;
3235

test/asserts/any-of-assert.test.js

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const { Assert: BaseAssert, Violation } = require('validator.js');
8+
const { describe, it } = require('node:test');
9+
const AnyOfAssert = require('../../src/asserts/any-of-assert.js');
10+
11+
/**
12+
* Extend `Assert` with `AnyOfAssert`.
13+
*/
14+
15+
const Assert = BaseAssert.extend({
16+
AnyOf: AnyOfAssert
17+
});
18+
19+
/**
20+
* Test `AnyOfAssert`.
21+
*/
22+
23+
describe('AnyOfAssert', () => {
24+
it('should throw an error if no constraint sets are provided', ({ assert }) => {
25+
try {
26+
Assert.anyOf();
27+
28+
assert.fail();
29+
} catch (e) {
30+
assert.equal(e.message, 'AnyOf constraint requires at least two constraint sets');
31+
}
32+
});
33+
34+
it('should throw an error if only one constraint set is provided', ({ assert }) => {
35+
try {
36+
Assert.anyOf({ bar: [Assert.equalTo('foo')] });
37+
38+
assert.fail();
39+
} catch (e) {
40+
assert.equal(e.message, 'AnyOf constraint requires at least two constraint sets');
41+
}
42+
});
43+
44+
it('should throw an error if value does not match any assert array constraint set', ({ assert }) => {
45+
try {
46+
Assert.anyOf(
47+
[Assert.ofLength({ min: 5 }), Assert.notBlank()],
48+
[Assert.ofLength({ min: 10 }), Assert.notBlank()]
49+
).validate('foo');
50+
51+
assert.fail();
52+
} catch (e) {
53+
assert.ok(e instanceof Violation);
54+
assert.equal(e.show().assert, 'AnyOf');
55+
}
56+
});
57+
58+
it('should pass if value matches an assert array constraint set', () => {
59+
Assert.anyOf(
60+
[Assert.ofLength({ min: 2 }), Assert.notBlank()],
61+
[Assert.ofLength({ min: 10 }), Assert.notBlank()]
62+
).validate('foo');
63+
});
64+
65+
it('should throw an error if value does not match any assert instance constraint set', ({ assert }) => {
66+
try {
67+
Assert.anyOf(Assert.ofLength({ max: 1 }), Assert.ofLength({ min: 5 })).validate('foo');
68+
69+
assert.fail();
70+
} catch (e) {
71+
assert.ok(e instanceof Violation);
72+
assert.equal(e.show().assert, 'AnyOf');
73+
}
74+
});
75+
76+
it('should pass if value matches when using assert instances as constraint sets', () => {
77+
Assert.anyOf(Assert.ofLength({ max: 2 }), Assert.ofLength({ min: 3 })).validate('foo');
78+
});
79+
80+
it('should pass if value matches one of mixed constraint set types', () => {
81+
Assert.anyOf([Assert.ofLength({ min: 2 }), Assert.notBlank()], Assert.ofLength({ max: 1 })).validate('foo');
82+
});
83+
84+
it('should throw an error if value does not match any constraint set', ({ assert }) => {
85+
try {
86+
Assert.anyOf({ bar: [Assert.equalTo('foo')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'biz' });
87+
88+
assert.fail();
89+
} catch (e) {
90+
assert.ok(e instanceof Violation);
91+
assert.equal(e.show().assert, 'AnyOf');
92+
}
93+
});
94+
95+
it('should include all violations in the error when no constraint set matches', ({ assert }) => {
96+
try {
97+
Assert.anyOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'qux' });
98+
99+
assert.fail();
100+
} catch (e) {
101+
const { violation } = e.show();
102+
103+
assert.equal(violation.length, 2);
104+
assert.ok(violation[0].bar[0] instanceof Violation);
105+
assert.equal(violation[0].bar[0].show().assert, 'EqualTo');
106+
assert.equal(violation[0].bar[0].show().violation.value, 'biz');
107+
assert.ok(violation[1].bar[0] instanceof Violation);
108+
assert.equal(violation[1].bar[0].show().assert, 'EqualTo');
109+
assert.equal(violation[1].bar[0].show().violation.value, 'baz');
110+
}
111+
});
112+
113+
it('should validate required fields using `deepRequired`', ({ assert }) => {
114+
try {
115+
Assert.anyOf(
116+
{ bar: [Assert.required(), Assert.notBlank()] },
117+
{ baz: [Assert.required(), Assert.notBlank()] }
118+
).validate({});
119+
120+
assert.fail();
121+
} catch (e) {
122+
assert.ok(e instanceof Violation);
123+
assert.equal(e.show().assert, 'AnyOf');
124+
}
125+
});
126+
127+
it('should throw an error if a constraint set with an extra assert does not match', ({ assert }) => {
128+
try {
129+
Assert.anyOf(
130+
{
131+
bar: [Assert.equalTo('biz')],
132+
baz: [Assert.anyOf({ qux: [Assert.equalTo('corge')] }, { qux: [Assert.equalTo('grault')] })]
133+
},
134+
{ bar: [Assert.equalTo('baz')] }
135+
).validate({ bar: 'biz', baz: { qux: 'wrong' } });
136+
137+
assert.fail();
138+
} catch (e) {
139+
assert.ok(e instanceof Violation);
140+
assert.equal(e.show().assert, 'AnyOf');
141+
}
142+
});
143+
144+
it('should throw an error if value does not match nested assert arrays in record values', ({ assert }) => {
145+
try {
146+
Assert.anyOf({ bar: [[Assert.equalTo('biz'), Assert.notBlank()]] }, { bar: [Assert.equalTo('baz')] }).validate({
147+
bar: 'qux'
148+
});
149+
150+
assert.fail();
151+
} catch (e) {
152+
assert.ok(e instanceof Violation);
153+
assert.equal(e.show().assert, 'AnyOf');
154+
}
155+
});
156+
157+
it('should pass if a constraint set contains either a required field or an optional field', () => {
158+
Assert.anyOf({ bar: [Assert.required(), Assert.notBlank()] }, { baz: Assert.notBlank() }).validate({});
159+
});
160+
161+
it('should pass if value matches more than one constraint set', () => {
162+
Assert.anyOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('biz')] }).validate({ bar: 'biz' });
163+
});
164+
165+
it('should pass if value matches more than one constraint set with different constraints', () => {
166+
Assert.anyOf({ bar: [Assert.notBlank()] }, { bar: [Assert.equalTo('biz')] }).validate({ bar: 'biz' });
167+
});
168+
169+
it('should pass if value matches the first constraint set', () => {
170+
Assert.anyOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'biz' });
171+
});
172+
173+
it('should pass if value matches the second constraint set', () => {
174+
Assert.anyOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'baz' });
175+
});
176+
177+
it('should support more than two constraint sets', () => {
178+
Assert.anyOf(
179+
{ bar: [Assert.equalTo('biz')] },
180+
{ bar: [Assert.equalTo('baz')] },
181+
{ bar: [Assert.equalTo('qux')] }
182+
).validate({ bar: 'qux' });
183+
});
184+
185+
it('should pass if a constraint set contains an extra assert', () => {
186+
Assert.anyOf(
187+
{
188+
bar: [Assert.equalTo('biz')],
189+
baz: [Assert.anyOf({ qux: [Assert.equalTo('corge')] }, { qux: [Assert.equalTo('grault')] })]
190+
},
191+
{ bar: [Assert.equalTo('baz')] }
192+
).validate({ bar: 'biz', baz: { qux: 'corge' } });
193+
});
194+
195+
it('should support nested assert arrays in record values', () => {
196+
Assert.anyOf({ bar: [[Assert.equalTo('biz'), Assert.notBlank()]] }, { bar: [Assert.equalTo('baz')] }).validate({
197+
bar: 'biz'
198+
});
199+
});
200+
});

test/index.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ describe('validator.js-asserts', () => {
1515
it('should export all asserts', ({ assert }) => {
1616
const assertNames = Object.keys(asserts);
1717

18-
assert.equal(assertNames.length, 41);
18+
assert.equal(assertNames.length, 42);
1919
assert.deepEqual(assertNames, [
2020
'AbaRoutingNumber',
21+
'AnyOf',
2122
'BankIdentifierCode',
2223
'BigNumber',
2324
'BigNumberEqualTo',

0 commit comments

Comments
 (0)