-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to customize default messages #80
Comments
Hi, Here is a simple example: $kw_map = [
'minimum' => 'Too low',
'maximum' => 'Too high',
// ... other keywords
];
$custom = function (ValidationError $error) use ($kw_map, $formatter) {
$schema = $error->schema()->info();
return [
'my_custom_message' => $kw_map[$error->keyword()] ?? 'Unknown error';
'keyword' => $error->keyword(),
'args' => $error->args(),
'message' => $error->message(),
'formattedMessage' => $formatter->formatErrorMessage($error),
];
};
$errors = $formatter->format($error, false, $custom); However, we prepare v2.2.0 and we'll add an easy way to format errors directly from schema. |
Correction: we already added support to format error messages directly from schema, we just have to write the docs :) |
I came to this result, but it still does not work
Where am I going wrong? |
|
Here is an example on how to format messages from schema. Currently, we don't have docs for this, but it is pretty simple to use Use the <?php
use Opis\JsonSchema\Validator;
use Opis\JsonSchema\Errors\ErrorFormatter;
$validator = new Validator();
$schema = <<<'JSON'
{
"$error": {
"required": {
"name": "Please provide a name"
}
},
"type": "object",
"properties": {
"name": {
"$error": {
"type": "Name should be a string",
"minLength": "Name length must be at least {min} chars, found {length}",
"maxLength": "Name is too long, only {max} chars are allowed, found {length}"
},
"type": "string",
"minLength": 3,
"maxLength": 5
},
"age": {
"$error": {
"minimum": "You must be at least {min} years long"
},
"type": "number",
"minimum": 18
}
},
"required": ["name"]
}
JSON;
$schema = json_decode($schema);
$formatter = new ErrorFormatter();
$result = $validator->validate((object)[], $schema);
print_r($formatter->format($result->error(), false));
/*
Array
(
[/] => Please provide a name
)
*/
$result = $validator->validate((object)[
'name' => 12 // not a string
], $schema);
print_r($formatter->format($result->error(), false));
/*
Array
(
[/name] => Name should be a string
)
*/
$result = $validator->validate((object)[
'name' => 'op' // too short
], $schema);
print_r($formatter->format($result->error(), false));
/*
Array
(
[/name] => Name length must be at least 3 chars, found 2
)
*/
$result = $validator->validate((object)[
'name' => 'opis/json-schema' // too long
], $schema);
print_r($formatter->format($result->error(), false));
/*
Array
(
[/name] => Name is too long, only 5 chars are allowed, found 16
)
*/
$result = $validator->validate((object)[
'name' => 'opis', // ok
'age' => '18', // not a number (default error message)
], $schema);
print_r($formatter->format($result->error(), false));
/*
Array
(
[/age] => The data (string) must match the type: number
)
*/
$result = $validator->validate((object)[
'name' => 'opis', // ok
'age' => 10, // too low
], $schema);
print_r($formatter->format($result->error(), false));
/*
Array
(
[/age] => You must be at least 18 years long
)
*/
$result = $validator->validate((object)[
'name' => 'opis', // ok
'age' => 18, // ok
], $schema);
if (!$result->hasError()) {
print_r("OK");
}
/*
OK
*/ |
Ohh thanks, this worked!! |
Hi,
I'm using it for valid, but I want to customize the default messages.
Thanks.
The text was updated successfully, but these errors were encountered: