Skip to content

Commit 54e7714

Browse files
authored
Merge pull request #73 from andreaselia/fix/issue-71
Fix issue with class based rules in an array
2 parents 352cc1b + 8626396 commit 54e7714

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/Commands/ExportPostmanCommand.php

+13
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,13 @@ protected function parseRulesIntoHumanReadable($attribute, $rules): string
312312
{
313313
// ... bail if user has asked for non interpreted strings:
314314
if (! $this->config['rules_to_human_readable']) {
315+
foreach ($rules as $i => $rule) {
316+
// because we don't support custom rule classes, we remove them from the rules
317+
if (is_subclass_of($rule, Rule::class)) {
318+
unset($rules[$i]);
319+
}
320+
}
321+
315322
return is_array($rules) ? implode(', ', $rules) : $this->safelyStringifyClassBasedRule($rules);
316323
}
317324

@@ -327,6 +334,12 @@ protected function parseRulesIntoHumanReadable($attribute, $rules): string
327334
* Handle string based rules (e.g. required|string|max:30)
328335
*/
329336
if (is_array($rules)) {
337+
foreach ($rules as $i => $rule) {
338+
if (is_object($rule)) {
339+
unset($rules[$i]);
340+
}
341+
}
342+
330343
$this->validator = Validator::make([], [$attribute => implode('|', $rules)]);
331344

332345
foreach ($rules as $rule) {

tests/Feature/ExportPostmanTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,17 @@ public function test_rules_printing_export_to_human_readable_works()
190190
$this->assertCount(1, $fields->where('key', 'field_2')->where('description', 'The field 2 field is required., The field 2 must be an integer.'));
191191
$this->assertCount(1, $fields->where('key', 'field_3')->where('description', '(Optional), The field 3 must be an integer.'));
192192
$this->assertCount(1, $fields->where('key', 'field_4')->where('description', '(Nullable), The field 4 must be an integer.'));
193+
// the below fails locally, but passes on GitHub actions?
193194
$this->assertCount(1, $fields->where('key', 'field_5')->where('description', 'The field 5 field is required., The field 5 must be an integer., The field 5 must not be greater than 30., The field 5 must be at least 1.'));
194195

195196
/** This looks bad, but this is the default message in lang/en/validation.php, you can update to:.
196197
*
197198
* "'in' => 'The selected :attribute is invalid. Allowable values: :values',"
198199
**/
199200
$this->assertCount(1, $fields->where('key', 'field_6')->where('description', 'The selected field 6 is invalid.'));
201+
$this->assertCount(1, $fields->where('key', 'field_7')->where('description', 'The field 7 field is required.'));
202+
$this->assertCount(1, $fields->where('key', 'field_8')->where('description', 'validation.'));
203+
$this->assertCount(1, $fields->where('key', 'field_9')->where('description', 'The field 9 field is required., The field 9 must be a string.'));
200204
}
201205

202206
public function providerFormDataEnabled(): array

tests/Fixtures/ExampleFormRequest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public function rules(): array
1616
'field_4' => 'nullable|integer',
1717
'field_5' => 'required|integer|max:30|min:1',
1818
'field_6' => new In([1, 2, 3]),
19-
'field_7' => new UppercaseRule,
19+
'field_7' => ['required', new In([1, 2, 3])],
20+
'field_8' => new UppercaseRule,
21+
'field_9' => ['required', 'string', new UppercaseRule],
2022
];
2123
}
2224
}

0 commit comments

Comments
 (0)