Skip to content

Commit 21cde84

Browse files
committed
Validators::assert replaced with usual error message
1 parent ab3bfc9 commit 21cde84

File tree

7 files changed

+53
-54
lines changed

7 files changed

+53
-54
lines changed

src/Schema/Elements/Base.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,10 @@ public function doNormalize($value, Context $context)
104104

105105
private function doValidate($value, string $expected, Context $context): bool
106106
{
107-
try {
108-
Nette\Utils\Validators::assert($value, $expected, 'option %path%');
109-
} catch (Nette\Utils\AssertionException $e) {
107+
if (!Nette\Utils\Validators::is($value, $expected)) {
108+
$expected = str_replace(['|', ':'], [' or ', ' in range '], $expected);
110109
$context->addError(
111-
$e->getMessage(),
110+
'The option %path% expects to be %expected%, %value% given.',
112111
Nette\Schema\Message::UNEXPECTED_VALUE,
113112
['value' => $value, 'expected' => $expected]
114113
);

tests/Schema/Expect.anyOf.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ test('with complex structure', function () {
4646

4747
checkValidationErrors(function () use ($schema) {
4848
(new Processor)->process($schema, [123]);
49-
}, ["The option '0' expects to be string, int 123 given."]);
49+
}, ["The option '0' expects to be string, 123 given."]);
5050

5151
Assert::same(['foo'], (new Processor)->process($schema, ['foo']));
5252
});

tests/Schema/Expect.array.phpt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ test('without default value', function () {
2121

2222
checkValidationErrors(function () use ($schema) {
2323
(new Processor)->process($schema, 'one');
24-
}, ["The option expects to be array, string 'one' given."]);
24+
}, ["The option expects to be array, 'one' given."]);
2525

2626
checkValidationErrors(function () use ($schema) {
2727
(new Processor)->process($schema, true);
28-
}, ['The option expects to be array, bool given.']);
28+
}, ['The option expects to be array, true given.']);
2929

3030
checkValidationErrors(function () use ($schema) {
3131
(new Processor)->process($schema, 123);
32-
}, ['The option expects to be array, int 123 given.']);
32+
}, ['The option expects to be array, 123 given.']);
3333

3434
Assert::same([], (new Processor)->process($schema, null));
3535
});
@@ -95,9 +95,9 @@ test('merging & other items validation', function () {
9595
checkValidationErrors(function () use ($schema) {
9696
(new Processor)->process($schema, [1, 2, 3]);
9797
}, [
98-
"The option '0' expects to be string, int 1 given.",
99-
"The option '1' expects to be string, int 2 given.",
100-
"The option '2' expects to be string, int 3 given.",
98+
"The option '0' expects to be string, 1 given.",
99+
"The option '1' expects to be string, 2 given.",
100+
"The option '2' expects to be string, 3 given.",
101101
]);
102102

103103
Assert::same(
@@ -161,9 +161,9 @@ test('items() & scalar', function () {
161161
checkValidationErrors(function () use ($schema) {
162162
(new Processor)->process($schema, [1, 2, 3]);
163163
}, [
164-
"The option '0' expects to be string, int 1 given.",
165-
"The option '1' expects to be string, int 2 given.",
166-
"The option '2' expects to be string, int 3 given.",
164+
"The option '0' expects to be string, 1 given.",
165+
"The option '1' expects to be string, 2 given.",
166+
"The option '2' expects to be string, 3 given.",
167167
]);
168168

169169
Assert::same(['a' => 'val'], (new Processor)->process($schema, ['a' => 'val']));
@@ -174,7 +174,7 @@ test('items() & scalar', function () {
174174

175175
checkValidationErrors(function () use ($schema) {
176176
(new Processor)->process($schema, ['b' => 123]);
177-
}, ["The option 'b' expects to be string, int 123 given."]);
177+
}, ["The option 'b' expects to be string, 123 given."]);
178178

179179
checkValidationErrors(function () use ($schema) {
180180
(new Processor)->process($schema, ['b' => null]);
@@ -193,19 +193,19 @@ test('items() & structure', function () {
193193

194194
checkValidationErrors(function () use ($schema) {
195195
(new Processor)->process($schema, ['a' => 'val']);
196-
}, ["The option 'a' expects to be array, string 'val' given."]);
196+
}, ["The option 'a' expects to be array, 'val' given."]);
197197

198198
checkValidationErrors(function () use ($schema) {
199199
(new Processor)->process($schema, [1, 2, 3]);
200200
}, [
201-
"The option '0' expects to be array, int 1 given.",
202-
"The option '1' expects to be array, int 2 given.",
203-
"The option '2' expects to be array, int 3 given.",
201+
"The option '0' expects to be array, 1 given.",
202+
"The option '1' expects to be array, 2 given.",
203+
"The option '2' expects to be array, 3 given.",
204204
]);
205205

206206
checkValidationErrors(function () use ($schema) {
207207
(new Processor)->process($schema, ['b' => 'val']);
208-
}, ["The option 'b' expects to be array, string 'val' given."]);
208+
}, ["The option 'b' expects to be array, 'val' given."]);
209209

210210
checkValidationErrors(function () use ($schema) {
211211
(new Processor)->process($schema, ['b' => ['a' => 'val']]);
@@ -229,7 +229,7 @@ test('arrayOf() & scalar', function () {
229229

230230
checkValidationErrors(function () use ($schema) {
231231
(new Processor)->process($schema, [1, 2, false]);
232-
}, ["The option '2' expects to be string or int, bool given."]);
232+
}, ["The option '2' expects to be string or int, false given."]);
233233

234234
Assert::same(['key' => 'val'], (new Processor)->process($schema, ['key' => 'val']));
235235
});

tests/Schema/Expect.list.phpt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ test('without default value', function () {
2323

2424
checkValidationErrors(function () use ($schema) {
2525
(new Processor)->process($schema, 'one');
26-
}, ["The option expects to be list, string 'one' given."]);
26+
}, ["The option expects to be list, 'one' given."]);
2727

2828
checkValidationErrors(function () use ($schema) {
2929
(new Processor)->process($schema, true);
30-
}, ['The option expects to be list, bool given.']);
30+
}, ['The option expects to be list, true given.']);
3131

3232
checkValidationErrors(function () use ($schema) {
3333
(new Processor)->process($schema, 123);
34-
}, ['The option expects to be list, int 123 given.']);
34+
}, ['The option expects to be list, 123 given.']);
3535

3636
Assert::same([], (new Processor)->process($schema, null));
3737
});
@@ -58,9 +58,9 @@ test('merging & other items validation', function () {
5858
checkValidationErrors(function () use ($schema) {
5959
(new Processor)->process($schema, [1, 2, 3]);
6060
}, [
61-
"The option '0' expects to be string, int 1 given.",
62-
"The option '1' expects to be string, int 2 given.",
63-
"The option '2' expects to be string, int 3 given.",
61+
"The option '0' expects to be string, 1 given.",
62+
"The option '1' expects to be string, 2 given.",
63+
"The option '2' expects to be string, 3 given.",
6464
]);
6565

6666
Assert::same([1, 2, 3], (new Processor)->process($schema, null));
@@ -75,9 +75,9 @@ test('listOf() & scalar', function () {
7575
checkValidationErrors(function () use ($schema) {
7676
(new Processor)->process($schema, [1, 2, 3]);
7777
}, [
78-
"The option '0' expects to be string, int 1 given.",
79-
"The option '1' expects to be string, int 2 given.",
80-
"The option '2' expects to be string, int 3 given.",
78+
"The option '0' expects to be string, 1 given.",
79+
"The option '1' expects to be string, 2 given.",
80+
"The option '2' expects to be string, 3 given.",
8181
]);
8282

8383
Assert::same(['val', 'val'], (new Processor)->process($schema, ['val', 'val']));

tests/Schema/Expect.minmax.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test('int & min', function () {
1717

1818
checkValidationErrors(function () use ($schema) {
1919
(new Processor)->process($schema, 9);
20-
}, ['The option expects to be int in range 10.., int 9 given.']);
20+
}, ['The option expects to be int in range 10.., 9 given.']);
2121
});
2222

2323

@@ -28,7 +28,7 @@ test('int & max', function () {
2828

2929
checkValidationErrors(function () use ($schema) {
3030
(new Processor)->process($schema, 21);
31-
}, ['The option expects to be int in range ..20, int 21 given.']);
31+
}, ['The option expects to be int in range ..20, 21 given.']);
3232
});
3333

3434

@@ -40,11 +40,11 @@ test('int & min & max', function () {
4040

4141
checkValidationErrors(function () use ($schema) {
4242
(new Processor)->process($schema, 9);
43-
}, ['The option expects to be int in range 10..20, int 9 given.']);
43+
}, ['The option expects to be int in range 10..20, 9 given.']);
4444

4545
checkValidationErrors(function () use ($schema) {
4646
(new Processor)->process($schema, 21);
47-
}, ['The option expects to be int in range 10..20, int 21 given.']);
47+
}, ['The option expects to be int in range 10..20, 21 given.']);
4848
});
4949

5050

@@ -56,7 +56,7 @@ test('nullable int & min & max', function () {
5656

5757
checkValidationErrors(function () use ($schema) {
5858
(new Processor)->process($schema, 9);
59-
}, ['The option expects to be null or int in range 10..20, int 9 given.']);
59+
}, ['The option expects to be null or int in range 10..20, 9 given.']);
6060
});
6161

6262

@@ -68,11 +68,11 @@ test('string', function () {
6868

6969
checkValidationErrors(function () use ($schema) {
7070
(new Processor)->process($schema, '');
71-
}, ["The option expects to be string in range 1..5, string '' given."]);
71+
}, ["The option expects to be string in range 1..5, '' given."]);
7272

7373
checkValidationErrors(function () use ($schema) {
7474
(new Processor)->process($schema, 'foobar');
75-
}, ["The option expects to be string in range 1..5, string 'foobar' given."]);
75+
}, ["The option expects to be string in range 1..5, 'foobar' given."]);
7676
});
7777

7878

tests/Schema/Expect.scalars.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ test('', function () {
3434

3535
checkValidationErrors(function () use ($schema) {
3636
(new Processor)->process($schema, 123);
37-
}, ['The option expects to be string, int 123 given.']);
37+
}, ['The option expects to be string, 123 given.']);
3838

3939
checkValidationErrors(function () use ($schema) {
4040
(new Processor)->process($schema, null);
4141
}, ['The option expects to be string, null given.']);
4242

4343
checkValidationErrors(function () use ($schema) {
4444
(new Processor)->process($schema, false);
45-
}, ['The option expects to be string, bool given.']);
45+
}, ['The option expects to be string, false given.']);
4646

4747
checkValidationErrors(function () use ($schema) {
4848
(new Processor)->process($schema, []);
@@ -59,7 +59,7 @@ test('', function () {
5959

6060
checkValidationErrors(function () use ($schema) {
6161
(new Processor)->process($schema, 123);
62-
}, ['The option expects to be string or bool, int 123 given.']);
62+
}, ['The option expects to be string or bool, 123 given.']);
6363

6464
checkValidationErrors(function () use ($schema) {
6565
(new Processor)->process($schema, null);
@@ -78,7 +78,7 @@ test('', function () {
7878

7979
checkValidationErrors(function () use ($schema) {
8080
(new Processor)->process($schema, 123);
81-
}, ['The option expects to be null or string, int 123 given.']);
81+
}, ['The option expects to be null or string, 123 given.']);
8282

8383
Assert::same(null, (new Processor)->process($schema, null));
8484
});

tests/Schema/Expect.structure.phpt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ test('without items', function () {
2525

2626
checkValidationErrors(function () use ($schema) {
2727
(new Processor)->process($schema, 'one');
28-
}, ["The option expects to be array, string 'one' given."]);
28+
}, ["The option expects to be array, 'one' given."]);
2929

3030
checkValidationErrors(function () use ($schema) {
3131
(new Processor)->process($schema, true);
32-
}, ['The option expects to be array, bool given.']);
32+
}, ['The option expects to be array, true given.']);
3333

3434
checkValidationErrors(function () use ($schema) {
3535
(new Processor)->process($schema, 123);
36-
}, ['The option expects to be array, int 123 given.']);
36+
}, ['The option expects to be array, 123 given.']);
3737

3838
Assert::equal((object) [], (new Processor)->process($schema, null));
3939
});
@@ -48,7 +48,7 @@ test('accepts object', function () {
4848

4949
checkValidationErrors(function () use ($schema) {
5050
(new Processor)->process($schema, (object) ['a' => 1]);
51-
}, ["The option 'a' expects to be string, int 1 given."]);
51+
}, ["The option 'a' expects to be string, 1 given."]);
5252

5353
$schema = Expect::structure(['a' => Expect::string()->before('strrev')]);
5454

@@ -143,7 +143,7 @@ test('with indexed item', function () {
143143
}, [
144144
"Unexpected option '1'.",
145145
"Unexpected option '2'.",
146-
"The option '0' expects to be string, int 1 given.",
146+
"The option '0' expects to be string, 1 given.",
147147
]);
148148

149149
Assert::equal(
@@ -194,7 +194,7 @@ test('with indexed item & otherItems', function () {
194194

195195
checkValidationErrors(function () use ($processor, $schema) {
196196
$processor->process($schema, [1, 2, 3]);
197-
}, ["The option '0' expects to be string, int 1 given."]);
197+
}, ["The option '0' expects to be string, 1 given."]);
198198

199199
Assert::equal(
200200
(object) [
@@ -238,7 +238,7 @@ test('item with default value', function () {
238238

239239
checkValidationErrors(function () use ($schema) {
240240
(new Processor)->process($schema, ['b' => 123]);
241-
}, ["The option 'b' expects to be string, int 123 given."]);
241+
}, ["The option 'b' expects to be string, 123 given."]);
242242

243243
checkValidationErrors(function () use ($schema) {
244244
(new Processor)->process($schema, ['b' => null]);
@@ -257,7 +257,7 @@ test('item without default value', function () {
257257

258258
checkValidationErrors(function () use ($schema) {
259259
(new Processor)->process($schema, ['b' => 123]);
260-
}, ["The option 'b' expects to be string, int 123 given."]);
260+
}, ["The option 'b' expects to be string, 123 given."]);
261261

262262
checkValidationErrors(function () use ($schema) {
263263
(new Processor)->process($schema, ['b' => null]);
@@ -301,7 +301,7 @@ test('other items', function () {
301301

302302
checkValidationErrors(function () use ($schema) {
303303
(new Processor)->process($schema, ['other' => 123]);
304-
}, ["The option 'other' expects to be string, int 123 given."]);
304+
}, ["The option 'other' expects to be string, 123 given."]);
305305
});
306306

307307

@@ -331,7 +331,7 @@ test('structure items', function () {
331331
checkValidationErrors(function () use ($schema) {
332332
(new Processor)->process($schema, ['a' => 'val']);
333333
}, [
334-
"The option 'a' expects to be array, string 'val' given.",
334+
"The option 'a' expects to be array, 'val' given.",
335335
"The mandatory option 'b › y' is missing.",
336336
]);
337337

@@ -341,15 +341,15 @@ test('structure items', function () {
341341

342342
checkValidationErrors(function () use ($schema) {
343343
(new Processor)->process($schema, ['b' => 123]);
344-
}, ["The option 'b' expects to be array, int 123 given."]);
344+
}, ["The option 'b' expects to be array, 123 given."]);
345345

346346
checkValidationErrors(function () use ($schema) {
347347
(new Processor)->process($schema, ['b' => null]);
348348
}, ["The mandatory option 'b › y' is missing."]);
349349

350350
checkValidationErrors(function () use ($schema) {
351351
(new Processor)->process($schema, ['b' => 'val']);
352-
}, ["The option 'b' expects to be array, string 'val' given."]);
352+
}, ["The option 'b' expects to be array, 'val' given."]);
353353

354354
checkValidationErrors(function () use ($schema) {
355355
(new Processor)->process($schema, ['b' => ['x' => 'val']]);
@@ -368,7 +368,7 @@ test('structure items', function () {
368368

369369
checkValidationErrors(function () use ($schema) {
370370
(new Processor)->process($schema, ['b' => ['y' => 123]]);
371-
}, ["The option 'b › y' expects to be string, int 123 given."]);
371+
}, ["The option 'b › y' expects to be string, 123 given."]);
372372

373373
Assert::equal(
374374
(object) ['a' => (object) ['x' => 'defval'], 'b' => (object) ['y' => 'val']],

0 commit comments

Comments
 (0)