|
| 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 OneOfAssert = require('../../src/asserts/one-of-assert.js'); |
| 10 | + |
| 11 | +/** |
| 12 | + * Extend `Assert` with `OneOfAssert`. |
| 13 | + */ |
| 14 | + |
| 15 | +const Assert = BaseAssert.extend({ |
| 16 | + OneOf: OneOfAssert |
| 17 | +}); |
| 18 | + |
| 19 | +/** |
| 20 | + * Test `OneOfAssert`. |
| 21 | + */ |
| 22 | + |
| 23 | +describe('OneOfAssert', () => { |
| 24 | + it('should throw an error if no constraint sets are provided', ({ assert }) => { |
| 25 | + try { |
| 26 | + Assert.oneOf(); |
| 27 | + |
| 28 | + assert.fail(); |
| 29 | + } catch (e) { |
| 30 | + assert.equal(e.message, 'OneOf 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.oneOf({ bar: [Assert.equalTo('foo')] }); |
| 37 | + |
| 38 | + assert.fail(); |
| 39 | + } catch (e) { |
| 40 | + assert.equal(e.message, 'OneOf 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.oneOf({ 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, 'OneOf'); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + it('should include all violations in the error when no constraint set matches', ({ assert }) => { |
| 56 | + try { |
| 57 | + Assert.oneOf({ 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.oneOf( |
| 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, 'OneOf'); |
| 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.oneOf( |
| 90 | + { |
| 91 | + bar: [Assert.equalTo('biz')], |
| 92 | + baz: [Assert.oneOf({ 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, 'OneOf'); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + it('should throw an error if value does not match any assert instance constraint set', ({ assert }) => { |
| 105 | + try { |
| 106 | + Assert.oneOf(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, 'OneOf'); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + it('should throw an error if value matches more than one assert instance constraint set', ({ assert }) => { |
| 116 | + try { |
| 117 | + Assert.oneOf(Assert.ofLength({ max: 5 }), Assert.ofLength({ min: 2 })).validate('foo'); |
| 118 | + |
| 119 | + assert.fail(); |
| 120 | + } catch (e) { |
| 121 | + assert.ok(e instanceof Violation); |
| 122 | + assert.equal(e.show().assert, 'OneOf'); |
| 123 | + assert.deepStrictEqual(e.show().violation, { matches: 2 }); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + it('should throw an error if value does not match any assert array constraint set', ({ assert }) => { |
| 128 | + try { |
| 129 | + Assert.oneOf([Assert.equalTo('foo')], [Assert.equalTo('bar')]).validate('baz'); |
| 130 | + |
| 131 | + assert.fail(); |
| 132 | + } catch (e) { |
| 133 | + assert.ok(e instanceof Violation); |
| 134 | + assert.equal(e.show().assert, 'OneOf'); |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + it('should throw an error if value matches more than one assert array constraint set', ({ assert }) => { |
| 139 | + try { |
| 140 | + Assert.oneOf( |
| 141 | + [Assert.notBlank(), Assert.ofLength({ min: 1 })], |
| 142 | + [Assert.notBlank(), Assert.ofLength({ max: 5 })] |
| 143 | + ).validate('foo'); |
| 144 | + |
| 145 | + assert.fail(); |
| 146 | + } catch (e) { |
| 147 | + assert.ok(e instanceof Violation); |
| 148 | + assert.equal(e.show().assert, 'OneOf'); |
| 149 | + assert.deepStrictEqual(e.show().violation, { matches: 2 }); |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + it('should throw an error if value matches more than one constraint set', ({ assert }) => { |
| 154 | + try { |
| 155 | + Assert.oneOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('biz')] }).validate({ bar: 'biz' }); |
| 156 | + |
| 157 | + assert.fail(); |
| 158 | + } catch (e) { |
| 159 | + assert.ok(e instanceof Violation); |
| 160 | + assert.equal(e.show().assert, 'OneOf'); |
| 161 | + assert.deepStrictEqual(e.show().violation, { matches: 2 }); |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + it('should throw an error if value matches more than one constraint set with overlapping schemas', ({ assert }) => { |
| 166 | + try { |
| 167 | + Assert.oneOf({ bar: [Assert.notBlank()] }, { bar: [Assert.equalTo('biz')] }).validate({ bar: 'biz' }); |
| 168 | + |
| 169 | + assert.fail(); |
| 170 | + } catch (e) { |
| 171 | + assert.ok(e instanceof Violation); |
| 172 | + assert.equal(e.show().assert, 'OneOf'); |
| 173 | + assert.deepStrictEqual(e.show().violation, { matches: 2 }); |
| 174 | + } |
| 175 | + }); |
| 176 | + |
| 177 | + it('should pass if value matches the first constraint set', () => { |
| 178 | + Assert.oneOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'biz' }); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should pass if value matches the second constraint set', () => { |
| 182 | + Assert.oneOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'baz' }); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should support more than two constraint sets', () => { |
| 186 | + Assert.oneOf( |
| 187 | + { bar: [Assert.equalTo('biz')] }, |
| 188 | + { bar: [Assert.equalTo('baz')] }, |
| 189 | + { bar: [Assert.equalTo('qux')] } |
| 190 | + ).validate({ bar: 'qux' }); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should pass if a constraint set contains an extra assert', () => { |
| 194 | + Assert.oneOf( |
| 195 | + { |
| 196 | + bar: [Assert.equalTo('biz')], |
| 197 | + baz: [Assert.oneOf({ qux: [Assert.equalTo('corge')] }, { qux: [Assert.equalTo('grault')] })] |
| 198 | + }, |
| 199 | + { bar: [Assert.equalTo('baz')] } |
| 200 | + ).validate({ bar: 'biz', baz: { qux: 'corge' } }); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should pass if value matches exactly one assert instance constraint set', () => { |
| 204 | + Assert.oneOf(Assert.ofLength({ max: 2 }), Assert.ofLength({ min: 3 })).validate('foo'); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should pass if value matches the first assert array constraint set', () => { |
| 208 | + Assert.oneOf([Assert.equalTo('foo')], [Assert.equalTo('bar')]).validate('foo'); |
| 209 | + }); |
| 210 | + |
| 211 | + it('should pass if value matches the second assert array constraint set', () => { |
| 212 | + Assert.oneOf([Assert.equalTo('foo')], [Assert.equalTo('bar')]).validate('bar'); |
| 213 | + }); |
| 214 | +}); |
0 commit comments