Skip to content

Commit a11e9c0

Browse files
committed
Add "undefined field error" and "type error" error message customization support
1 parent eda7632 commit a11e9c0

20 files changed

+656
-242
lines changed

resources/config.schema.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
"description": "A mapper configuration schema",
77
"additionalProperties": false,
88
"properties": {
9+
"type_error_message": {
10+
"type": "string",
11+
"minLength": 1,
12+
"description": "Custom error message on property type error"
13+
},
14+
"undefined_error_message": {
15+
"type": "string",
16+
"minLength": 1,
17+
"description": "Custom error message on property value missing error"
18+
},
919
"normalize_as_array": {
1020
"type": "boolean",
1121
"description": "Responsible for enabling or disabling the conversion to an associative array"

src/Exception/Mapping/RuntimeException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ public function __construct(
3232
);
3333
}
3434

35+
public function updateMessage(string $message): void
36+
{
37+
$this->template->updateTemplateMessage($message);
38+
39+
if ($this->message instanceof Template) {
40+
$this->message->updateTemplateMessage($message);
41+
}
42+
}
43+
3544
private static function createTemplate(string $template, \Throwable $context, PathInterface $path): Template
3645
{
3746
$suffix = '';

src/Exception/Template.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ final class Template implements \Stringable
2828
public ValuePrinterInterface $values;
2929

3030
public function __construct(
31-
private readonly string $template,
31+
private string $template,
3232
private readonly \Throwable $context,
3333
) {
3434
$this->types = self::createDefaultTypePrinter();
3535
$this->paths = self::createDefaultPathPrinter();
3636
$this->values = self::createDefaultValuePrinter();
3737
}
3838

39+
public function updateTemplateMessage(string $message): void
40+
{
41+
$this->template = $message;
42+
}
43+
3944
private static function createDefaultTypePrinter(): TypePrinterInterface
4045
{
4146
return new PrettyPrinter(

src/Mapping/Driver/ArrayConfigDriver.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ abstract protected function getConfiguration(\ReflectionClass $class): ?array;
4444
*
4545
* @return array{
4646
* normalize_as_array?: bool,
47+
* type_error_message?: non-empty-string,
48+
* undefined_error_message?: non-empty-string,
4749
* discriminator?: array{
4850
* field: non-empty-string,
4951
* map: array<non-empty-string, non-empty-string>,
@@ -189,6 +191,24 @@ class: $reflection,
189191

190192
$metadata = $class->getPropertyOrCreate($propertyName);
191193

194+
// ---------------------------------------------------------------------
195+
// start: Property Error Message
196+
// ---------------------------------------------------------------------
197+
198+
if (\array_key_exists('type_error_message', $propertyConfig)) {
199+
// @phpstan-ignore-next-line : Additional DbC invariant
200+
assert(\is_string($propertyConfig['type_error_message']));
201+
202+
$metadata->typeErrorMessage = $propertyConfig['type_error_message'];
203+
}
204+
205+
if (\array_key_exists('undefined_error_message', $propertyConfig)) {
206+
// @phpstan-ignore-next-line : Additional DbC invariant
207+
assert(\is_string($propertyConfig['undefined_error_message']));
208+
209+
$metadata->undefinedErrorMessage = $propertyConfig['undefined_error_message'];
210+
}
211+
192212
// -----------------------------------------------------------------
193213
// start: Property Type
194214
// -----------------------------------------------------------------

0 commit comments

Comments
 (0)