|
| 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 constraint set', ({ assert }) => { |
| 45 | + try { |
| 46 | + Assert.anyOf({ bar: [Assert.equalTo('foo')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'biz' }); |
| 47 | + |
| 48 | + assert.fail(); |
| 49 | + } catch (e) { |
| 50 | + assert.ok(e instanceof Violation); |
| 51 | + assert.equal(e.show().assert, 'AnyOf'); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + it('should include all violations in the error when no constraint set matches', ({ assert }) => { |
| 56 | + try { |
| 57 | + Assert.anyOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'qux' }); |
| 58 | + |
| 59 | + assert.fail(); |
| 60 | + } catch (e) { |
| 61 | + const { violation } = e.show(); |
| 62 | + |
| 63 | + assert.equal(violation.length, 2); |
| 64 | + assert.ok(violation[0].bar[0] instanceof Violation); |
| 65 | + assert.equal(violation[0].bar[0].show().assert, 'EqualTo'); |
| 66 | + assert.equal(violation[0].bar[0].show().violation.value, 'biz'); |
| 67 | + assert.ok(violation[1].bar[0] instanceof Violation); |
| 68 | + assert.equal(violation[1].bar[0].show().assert, 'EqualTo'); |
| 69 | + assert.equal(violation[1].bar[0].show().violation.value, 'baz'); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + it('should validate required fields using `deepRequired`', ({ assert }) => { |
| 74 | + try { |
| 75 | + Assert.anyOf( |
| 76 | + { bar: [Assert.required(), Assert.notBlank()] }, |
| 77 | + { baz: [Assert.required(), Assert.notBlank()] } |
| 78 | + ).validate({}); |
| 79 | + |
| 80 | + assert.fail(); |
| 81 | + } catch (e) { |
| 82 | + assert.ok(e instanceof Violation); |
| 83 | + assert.equal(e.show().assert, 'AnyOf'); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + it('should throw an error if a constraint set with an extra assert does not match', ({ assert }) => { |
| 88 | + try { |
| 89 | + Assert.anyOf( |
| 90 | + { |
| 91 | + bar: [Assert.equalTo('biz')], |
| 92 | + baz: [Assert.anyOf({ qux: [Assert.equalTo('corge')] }, { qux: [Assert.equalTo('grault')] })] |
| 93 | + }, |
| 94 | + { bar: [Assert.equalTo('baz')] } |
| 95 | + ).validate({ bar: 'biz', baz: { qux: 'wrong' } }); |
| 96 | + |
| 97 | + assert.fail(); |
| 98 | + } catch (e) { |
| 99 | + assert.ok(e instanceof Violation); |
| 100 | + assert.equal(e.show().assert, 'AnyOf'); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + it('should throw an error if value does not match any assert instance constraint set', ({ assert }) => { |
| 105 | + try { |
| 106 | + Assert.anyOf(Assert.ofLength({ max: 1 }), Assert.ofLength({ min: 5 })).validate('foo'); |
| 107 | + |
| 108 | + assert.fail(); |
| 109 | + } catch (e) { |
| 110 | + assert.ok(e instanceof Violation); |
| 111 | + assert.equal(e.show().assert, 'AnyOf'); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + it('should pass if value matches more than one constraint set', () => { |
| 116 | + Assert.anyOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('biz')] }).validate({ bar: 'biz' }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should pass if value matches more than one constraint set with different constraints', () => { |
| 120 | + Assert.anyOf({ bar: [Assert.notBlank()] }, { bar: [Assert.equalTo('biz')] }).validate({ bar: 'biz' }); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should pass if value matches the first constraint set', () => { |
| 124 | + Assert.anyOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'biz' }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should pass if value matches the second constraint set', () => { |
| 128 | + Assert.anyOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'baz' }); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should support more than two constraint sets', () => { |
| 132 | + Assert.anyOf( |
| 133 | + { bar: [Assert.equalTo('biz')] }, |
| 134 | + { bar: [Assert.equalTo('baz')] }, |
| 135 | + { bar: [Assert.equalTo('qux')] } |
| 136 | + ).validate({ bar: 'qux' }); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should pass if a constraint set contains an extra assert', () => { |
| 140 | + Assert.anyOf( |
| 141 | + { |
| 142 | + bar: [Assert.equalTo('biz')], |
| 143 | + baz: [Assert.anyOf({ qux: [Assert.equalTo('corge')] }, { qux: [Assert.equalTo('grault')] })] |
| 144 | + }, |
| 145 | + { bar: [Assert.equalTo('baz')] } |
| 146 | + ).validate({ bar: 'biz', baz: { qux: 'corge' } }); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should pass if value matches when using assert instances as constraint sets', () => { |
| 150 | + Assert.anyOf(Assert.ofLength({ max: 2 }), Assert.ofLength({ min: 3 })).validate('foo'); |
| 151 | + }); |
| 152 | +}); |
0 commit comments