json-schema
json-schema copied to clipboard
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.
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.
Note that if you are wanting to embed error messages inside the actual schema document, it can't do that. I'm not sure if that's what you're asking for though.
That said, it does give you an error path, so it would be fairly straightforward to map embedded messages with a simple post-processor.
Thanks for your answer. It's helped me a lot.
Most welcome :-)
@erayd
Can you please give some example or something for adding custom error in the schema.
@ravipxm. I can. Could you please clarify what you are trying to do and where you are stuck? That way I can provide an example that is as relevant as possible.
@erayd Whenever any validation fails, lib gives me the default error message. Instead of that, i like to have my own error message.
Thanks for your reply.
@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).
@erayd is there any prediction for 6.0.0 release?
@lfdubiela "at some point" is the best I can give you unfortunately. No ETA. This project has almost no manpower to get things done; it's mostly just me and @shmax, and we've both been rather busy with other things.
If you have the time and skills to contribute, that would be greatly appreciated, and would help get the next release out the door sooner.
@erayd do you think that this plan for 6.0.0 is current?
https://github.com/justinrainbow/json-schema/pull/390#issuecomment-301631526
I would like to get the project up to date to show the current status:
https://github.com/justinrainbow/json-schema/projects/2
I think it will be easier for everyone to know the status and where they may help.
As always, thank you for everything.
@bighappyface Yes, I believe that is still current.
Do you reckon it might be worth rolling a v6 dev release now, just to simplify things for composer users who want to use it now, even though it's not finished? We'd need to be really clear that breaking changes are still allowed though, because some stuff off the todo list is breaking.
Btw, I'm available for some light chores. Let me know if you want me to follow up on any of those issues clogging up the queue, or anything.
@shmax Honestly, anything you pick up is useful. I'm quite happy to code review, I just can't sink bulk hours into implementing stuff right now.
Righto, then. I'll just take the one at the top of the list: https://github.com/justinrainbow/json-schema/issues/525