|
2 | 2 |
|
3 | 3 | use DaniloPolani\JsonValidation\JsonValidation;
|
4 | 4 | use Illuminate\Contracts\Validation\Rule as RuleContract;
|
| 5 | +use Illuminate\Support\Collection; |
5 | 6 | use Illuminate\Support\Facades\App;
|
6 | 7 | use Illuminate\Translation\ArrayLoader;
|
7 | 8 | use Illuminate\Translation\Translator;
|
8 | 9 |
|
9 |
| -it('returns the error message for built-in rules', function () { |
| 10 | +$rules = require './tests/validation.php'; |
| 11 | +$rulesToLoad = Collection::make($rules)->mapWithKeys(fn (mixed $value, string $key) => [ |
| 12 | + 'validation.' . $key => $value, |
| 13 | +])->toArray(); |
| 14 | + |
| 15 | +it('returns the error message for built-in rules', function () use ($rulesToLoad) { |
10 | 16 | $trans = new Translator(new ArrayLoader(), 'en');
|
11 |
| - $trans->addLines([ |
12 |
| - 'validation.required_array_keys' => 'The :attribute field must contain entries for :values', |
13 |
| - 'validation.required' => 'The :attribute field is required.', |
14 |
| - 'validation.required_without' => 'The :attribute field is required when :values is not present.', |
15 |
| - ], 'en'); |
| 17 | + $trans->addLines($rulesToLoad, 'en'); |
16 | 18 |
|
17 | 19 | /** @var \DaniloPolani\JsonValidation\Tests\TestCase $this */
|
18 | 20 | App::getFacadeApplication()->instance('translator', $trans);
|
|
21 | 23 | 'The foo field is required.',
|
22 | 24 | ]);
|
23 | 25 | expect(JsonValidation::getRuleErrorMessage('foo', 'required_array_keys:bar,baz'))->toBe([
|
24 |
| - 'The foo field must contain entries for bar, baz', |
| 26 | + 'The foo field must contain entries for: bar, baz.', |
25 | 27 | ]);
|
26 | 28 | expect(JsonValidation::getRuleErrorMessage('foo', 'required_without:bar'))->toBe([
|
27 | 29 | 'The foo field is required when bar is not present.',
|
@@ -51,3 +53,193 @@ public function message(): string
|
51 | 53 |
|
52 | 54 | expect(JsonValidation::getRuleErrorMessage('foo', $rule))->toBe(['foo must be baz']);
|
53 | 55 | });
|
| 56 | + |
| 57 | +it('handles dynamic "between" rule', function (string $shape) use ($rules, $rulesToLoad) { |
| 58 | + $trans = new Translator(new ArrayLoader(), 'en'); |
| 59 | + $trans->addLines($rulesToLoad, 'en'); |
| 60 | + |
| 61 | + /** @var \DaniloPolani\JsonValidation\Tests\TestCase $this */ |
| 62 | + App::getFacadeApplication()->instance('translator', $trans); |
| 63 | + |
| 64 | + $expected = str_replace( |
| 65 | + [':min', ':max', ':attribute'], |
| 66 | + [3, 5, 'foo'], |
| 67 | + $rules['between'][$shape], |
| 68 | + ); |
| 69 | + |
| 70 | + expect(JsonValidation::getRuleErrorMessage('foo', sprintf('between.%s:3,5', $shape)))->toBe([$expected]); |
| 71 | +})->with([ |
| 72 | + 'array', |
| 73 | + 'file', |
| 74 | + 'numeric', |
| 75 | + 'string', |
| 76 | +]); |
| 77 | + |
| 78 | +it('handles dynamic "gt" rule', function (string $shape) use ($rules, $rulesToLoad) { |
| 79 | + $trans = new Translator(new ArrayLoader(), 'en'); |
| 80 | + $trans->addLines($rulesToLoad, 'en'); |
| 81 | + |
| 82 | + /** @var \DaniloPolani\JsonValidation\Tests\TestCase $this */ |
| 83 | + App::getFacadeApplication()->instance('translator', $trans); |
| 84 | + |
| 85 | + $expected = str_replace( |
| 86 | + [':value', ':attribute'], |
| 87 | + [3, 'foo'], |
| 88 | + $rules['gt'][$shape], |
| 89 | + ); |
| 90 | + |
| 91 | + expect(JsonValidation::getRuleErrorMessage('foo', sprintf('gt.%s:3', $shape)))->toBe([$expected]); |
| 92 | +})->with([ |
| 93 | + 'array', |
| 94 | + 'file', |
| 95 | + 'numeric', |
| 96 | + 'string', |
| 97 | +]); |
| 98 | + |
| 99 | +it('handles dynamic "gte" rule', function (string $shape) use ($rules, $rulesToLoad) { |
| 100 | + $trans = new Translator(new ArrayLoader(), 'en'); |
| 101 | + $trans->addLines($rulesToLoad, 'en'); |
| 102 | + |
| 103 | + /** @var \DaniloPolani\JsonValidation\Tests\TestCase $this */ |
| 104 | + App::getFacadeApplication()->instance('translator', $trans); |
| 105 | + |
| 106 | + $expected = str_replace( |
| 107 | + [':value', ':attribute'], |
| 108 | + [3, 'foo'], |
| 109 | + $rules['gte'][$shape], |
| 110 | + ); |
| 111 | + |
| 112 | + expect(JsonValidation::getRuleErrorMessage('foo', sprintf('gte.%s:3', $shape)))->toBe([$expected]); |
| 113 | +})->with([ |
| 114 | + 'array', |
| 115 | + 'file', |
| 116 | + 'numeric', |
| 117 | + 'string', |
| 118 | +]); |
| 119 | + |
| 120 | +it('handles dynamic "lt" rule', function (string $shape) use ($rules, $rulesToLoad) { |
| 121 | + $trans = new Translator(new ArrayLoader(), 'en'); |
| 122 | + $trans->addLines($rulesToLoad, 'en'); |
| 123 | + |
| 124 | + /** @var \DaniloPolani\JsonValidation\Tests\TestCase $this */ |
| 125 | + App::getFacadeApplication()->instance('translator', $trans); |
| 126 | + |
| 127 | + $expected = str_replace( |
| 128 | + [':value', ':attribute'], |
| 129 | + [3, 'foo'], |
| 130 | + $rules['lt'][$shape], |
| 131 | + ); |
| 132 | + |
| 133 | + expect(JsonValidation::getRuleErrorMessage('foo', sprintf('lt.%s:3', $shape)))->toBe([$expected]); |
| 134 | +})->with([ |
| 135 | + 'array', |
| 136 | + 'file', |
| 137 | + 'numeric', |
| 138 | + 'string', |
| 139 | +]); |
| 140 | + |
| 141 | +it('handles dynamic "lte" rule', function (string $shape) use ($rules, $rulesToLoad) { |
| 142 | + $trans = new Translator(new ArrayLoader(), 'en'); |
| 143 | + $trans->addLines($rulesToLoad, 'en'); |
| 144 | + |
| 145 | + /** @var \DaniloPolani\JsonValidation\Tests\TestCase $this */ |
| 146 | + App::getFacadeApplication()->instance('translator', $trans); |
| 147 | + |
| 148 | + $expected = str_replace( |
| 149 | + [':value', ':attribute'], |
| 150 | + [3, 'foo'], |
| 151 | + $rules['lte'][$shape], |
| 152 | + ); |
| 153 | + |
| 154 | + expect(JsonValidation::getRuleErrorMessage('foo', sprintf('lte.%s:3', $shape)))->toBe([$expected]); |
| 155 | +})->with([ |
| 156 | + 'array', |
| 157 | + 'file', |
| 158 | + 'numeric', |
| 159 | + 'string', |
| 160 | +]); |
| 161 | + |
| 162 | +it('handles dynamic "max" rule', function (string $shape) use ($rules, $rulesToLoad) { |
| 163 | + $trans = new Translator(new ArrayLoader(), 'en'); |
| 164 | + $trans->addLines($rulesToLoad, 'en'); |
| 165 | + |
| 166 | + /** @var \DaniloPolani\JsonValidation\Tests\TestCase $this */ |
| 167 | + App::getFacadeApplication()->instance('translator', $trans); |
| 168 | + |
| 169 | + $expected = str_replace( |
| 170 | + [':max', ':attribute'], |
| 171 | + [3, 'foo'], |
| 172 | + $rules['max'][$shape], |
| 173 | + ); |
| 174 | + |
| 175 | + expect(JsonValidation::getRuleErrorMessage('foo', sprintf('max.%s:3', $shape)))->toBe([$expected]); |
| 176 | +})->with([ |
| 177 | + 'array', |
| 178 | + 'file', |
| 179 | + 'numeric', |
| 180 | + 'string', |
| 181 | +]); |
| 182 | + |
| 183 | +it('handles dynamic "min" rule', function (string $shape) use ($rules, $rulesToLoad) { |
| 184 | + $trans = new Translator(new ArrayLoader(), 'en'); |
| 185 | + $trans->addLines($rulesToLoad, 'en'); |
| 186 | + |
| 187 | + /** @var \DaniloPolani\JsonValidation\Tests\TestCase $this */ |
| 188 | + App::getFacadeApplication()->instance('translator', $trans); |
| 189 | + |
| 190 | + $expected = str_replace( |
| 191 | + [':min', ':attribute'], |
| 192 | + [3, 'foo'], |
| 193 | + $rules['min'][$shape], |
| 194 | + ); |
| 195 | + |
| 196 | + expect(JsonValidation::getRuleErrorMessage('foo', sprintf('min.%s:3', $shape)))->toBe([$expected]); |
| 197 | +})->with([ |
| 198 | + 'array', |
| 199 | + 'file', |
| 200 | + 'numeric', |
| 201 | + 'string', |
| 202 | +]); |
| 203 | + |
| 204 | +it('handles dynamic "password" rule', function (string $shape) use ($rules, $rulesToLoad) { |
| 205 | + $trans = new Translator(new ArrayLoader(), 'en'); |
| 206 | + $trans->addLines($rulesToLoad, 'en'); |
| 207 | + |
| 208 | + /** @var \DaniloPolani\JsonValidation\Tests\TestCase $this */ |
| 209 | + App::getFacadeApplication()->instance('translator', $trans); |
| 210 | + |
| 211 | + $expected = str_replace( |
| 212 | + ':attribute', |
| 213 | + 'foo', |
| 214 | + $rules['password'][$shape], |
| 215 | + ); |
| 216 | + |
| 217 | + expect(JsonValidation::getRuleErrorMessage('foo', sprintf('password.%s', $shape)))->toBe([$expected]); |
| 218 | +})->with([ |
| 219 | + 'letters', |
| 220 | + 'mixed', |
| 221 | + 'numbers', |
| 222 | + 'symbols', |
| 223 | + 'uncompromised', |
| 224 | +]); |
| 225 | + |
| 226 | +it('handles dynamic "size" rule', function (string $shape) use ($rules, $rulesToLoad) { |
| 227 | + $trans = new Translator(new ArrayLoader(), 'en'); |
| 228 | + $trans->addLines($rulesToLoad, 'en'); |
| 229 | + |
| 230 | + /** @var \DaniloPolani\JsonValidation\Tests\TestCase $this */ |
| 231 | + App::getFacadeApplication()->instance('translator', $trans); |
| 232 | + |
| 233 | + $expected = str_replace( |
| 234 | + [':size', ':attribute'], |
| 235 | + [3, 'foo'], |
| 236 | + $rules['size'][$shape], |
| 237 | + ); |
| 238 | + |
| 239 | + expect(JsonValidation::getRuleErrorMessage('foo', sprintf('size.%s:3', $shape)))->toBe([$expected]); |
| 240 | +})->with([ |
| 241 | + 'array', |
| 242 | + 'file', |
| 243 | + 'numeric', |
| 244 | + 'string', |
| 245 | +]); |
0 commit comments