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.
Class type error
Section titled “Class type error”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;}<?php
return [ App\User::class => [ 'type_error_message' => 'A user is expected, {{value}} given', ],];{ "App\\User": { "type_error_message": "A user is expected, {{value}} given" }}App\User: type_error_message: 'A user is expected, {{value}} given'App\User: type_error_message: 'A user is expected, {{value}} given'$mapper->denormalize(42, User::class);// throws InvalidValueException: $: A user is expected, 42 givenProperty type error
Section titled “Property type error”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;}<?php
return [ App\User::class => [ 'properties' => [ 'age' => [ 'type_error_message' => 'Age must be a number, {{value}} given', ], ], ],];{ "App\\User": { "properties": { "age": { "type_error_message": "Age must be a number, {{value}} given" } } }}App\User: properties: age: type_error_message: 'Age must be a number, {{value}} given'App\User: properties: age: type_error_message: 'Age must be a number, {{value}} given'$mapper->denormalize(['age' => 'old'], User::class);// throws InvalidValueException: $.age: Age must be a number, "old" givenMissing field error
Section titled “Missing field error”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;}<?php
return [ App\User::class => [ 'properties' => [ 'email' => [ 'undefined_error_message' => 'The {{field}} is required', ], ], ],];{ "App\\User": { "properties": { "email": { "undefined_error_message": "The {{field}} is required" } } }}App\User: properties: email: undefined_error_message: 'The {{field}} is required'App\User: properties: email: undefined_error_message: 'The {{field}} is required'$mapper->denormalize([], User::class);// throws InvalidValueException: $.email: The email is required