Skip to content

Metadata

Error messages

Custom messages of the errors of a class and of its properties. Other details of errors are described in errors.

Every option is shown in each metadata source: choose the tab of the source you use. When a source cannot express an option, its tab says what is used instead.

Replaces the message of the error when the value is not data of the class (denormalization) or not an object of the class (normalization). Placeholders: {{value}}, {{expected}}.

use TypeLang\Mapper\Mapping\OnTypeError;
#[OnTypeError('A user is expected, {{value}} given')]
final class User
{
public string $name;
}
$mapper->denormalize(42, User::class);
// throws InvalidValueException: $: A user is expected, 42 given

Replaces the message of the error of the property value. Errors of its nested values keep their own messages. Placeholders: {{value}} and those of the replaced error: {{expected}} for a wrong type, plus {{check}} for a failed check such as positive-int. The text replaces both errors, a placeholder without data is empty: {{check}} of a wrong type, or {{expected}} on normalization.

use TypeLang\Mapper\Mapping\OnTypeError;
final class User
{
#[OnTypeError('Age must be a number, {{value}} given')]
public int $age;
}
$mapper->denormalize(['age' => 'old'], User::class);
// throws InvalidValueException: $.age: Age must be a number, "old" given

Replaces the message of the error of a missing field. Placeholder: {{field}}.

use TypeLang\Mapper\Mapping\OnUndefinedError;
final class User
{
#[OnUndefinedError('The {{field}} is required')]
public string $email;
}
$mapper->denormalize([], User::class);
// throws InvalidValueException: $.email: The email is required