json-schema: Custom error message inside JSON Schema.

The problem is that it gives generic error messages that are not relevant to me. I want to send custom message to user.

Do we have any option like this:

"properties": {
        "myKey": {
            "type": "string"
            **"errorMessage" : "My error message"**
        },
}

Thanks.

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 1
  • Comments: 16

Most upvoted comments

v6.0.0 has already overhauled the error structure in a way that allows you to fully customise the error messages. This work was done several months ago by @shmax.

There isn’t an official 6.0.0 release yet, but for most purposes it works fairly well, so you’re welcome to use the 6.0.0-dev branch for now until we get the release out.

@ravipxm That is what the constraint field of the error is for.

Each error has a unique name - these are defined in JsonSchema\ConstraintError. To customise the message, use something like the following:

$customError = getCustomErrorFor($error['constraint']['name']);
$errorParams = $error['constraint']['params'];
$error['message'] = sprintf($customError, ...$errorParams);

Note that you may also wish to consider the param types - they are provided to the user as-is, and some of them will not be scalar (the internal error messages handle this within the library, but the params presented to the user are not modified).