Skip to content

Commit 2ed99ee

Browse files
committed
Improve validation error messages with detailed context
1 parent 2743d38 commit 2ed99ee

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

.github/workflows/run-tests.yml

Whitespace-only changes.

src/Setting.php

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,16 @@ public static function allSettings() : array
357357
*/
358358
public function validateNewValue($value, bool $throwValidationException = false) : bool
359359
{
360+
$valueString = $this->formatValueForMessage($value);
361+
360362
if ($this->type === 'regex') {
361-
$validator = Validator::make([$this->key => $value], [$this->key => 'string']);
363+
$validator = Validator::make(
364+
[$this->key => $value],
365+
[$this->key => 'string'],
366+
[
367+
$this->key . '.string' => "Setting '{$this->key}' must be a string. Given value: {$valueString} (type: " . gettype($value) . ")"
368+
]
369+
);
362370

363371
if ($validator->fails() && $throwValidationException) {
364372
throw new ValidationException($validator);
@@ -368,7 +376,13 @@ public function validateNewValue($value, bool $throwValidationException = false)
368376
}
369377

370378
$rules = self::getValidationRules();
371-
$validator = Validator::make([$this->key => $value], [$this->key => $rules[$this->key] ?? 'nullable']);
379+
$rule = $rules[$this->key] ?? 'nullable';
380+
381+
$validator = Validator::make(
382+
[$this->key => $value],
383+
[$this->key => $rule],
384+
$this->getCustomValidationMessages($value, $rule)
385+
);
372386

373387
if ($validator->fails() && $throwValidationException) {
374388
throw new ValidationException($validator);
@@ -377,6 +391,61 @@ public function validateNewValue($value, bool $throwValidationException = false)
377391
return !$validator->fails();
378392
}
379393

394+
/**
395+
* Get custom validation messages for better error reporting
396+
*
397+
* @param mixed $value
398+
* @param string $rule
399+
* @return array
400+
*/
401+
private function getCustomValidationMessages($value, string $rule): array
402+
{
403+
$valueString = $this->formatValueForMessage($value);
404+
$typeString = gettype($value);
405+
406+
return [
407+
$this->key . '.required' => "Setting '{$this->key}' is required but was not provided.",
408+
$this->key . '.string' => "Setting '{$this->key}' must be a string. Given value: {$valueString} (type: {$typeString})",
409+
$this->key . '.integer' => "Setting '{$this->key}' must be an integer. Given value: {$valueString} (type: {$typeString})",
410+
$this->key . '.boolean' => "Setting '{$this->key}' must be a boolean. Given value: {$valueString} (type: {$typeString})",
411+
$this->key . '.array' => "Setting '{$this->key}' must be an array. Given value: {$valueString} (type: {$typeString})",
412+
$this->key . '.numeric' => "Setting '{$this->key}' must be numeric. Given value: {$valueString} (type: {$typeString})",
413+
$this->key . '.email' => "Setting '{$this->key}' must be a valid email address. Given value: {$valueString}",
414+
$this->key . '.url' => "Setting '{$this->key}' must be a valid URL. Given value: {$valueString}",
415+
$this->key . '.min' => "Setting '{$this->key}' is too small. Given value: {$valueString}",
416+
$this->key . '.max' => "Setting '{$this->key}' is too large. Given value: {$valueString}",
417+
$this->key . '.in' => "Setting '{$this->key}' has an invalid value. Given value: {$valueString}. Expected one of the allowed values from rule: {$rule}",
418+
];
419+
}
420+
421+
/**
422+
* Format value for error messages
423+
*
424+
* @param mixed $value
425+
* @return string
426+
*/
427+
private function formatValueForMessage($value): string
428+
{
429+
if (is_null($value)) {
430+
return 'null';
431+
}
432+
433+
if (is_bool($value)) {
434+
return $value ? 'true' : 'false';
435+
}
436+
437+
if (is_array($value)) {
438+
return '[' . implode(', ', array_slice($value, 0, 3)) . (count($value) > 3 ? '...' : '') . ']';
439+
}
440+
441+
if (is_object($value)) {
442+
return 'object(' . get_class($value) . ')';
443+
}
444+
445+
$stringValue = (string) $value;
446+
return strlen($stringValue) > 50 ? substr($stringValue, 0, 47) . '...' : $stringValue;
447+
}
448+
380449
/**
381450
* Get the validation rules for setting fields
382451
*

0 commit comments

Comments
 (0)