json-schema icon indicating copy to clipboard operation
json-schema copied to clipboard

Custom error message inside JSON Schema.

Open ViktorKudlai opened this issue 7 years ago • 16 comments

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.

ViktorKudlai avatar Sep 05 '17 08:09 ViktorKudlai

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.

erayd avatar Sep 05 '17 08:09 erayd

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.

erayd avatar Sep 05 '17 08:09 erayd

That said, it does give you an error path, so it would be fairly straightforward to map embedded messages with a simple post-processor.

erayd avatar Sep 05 '17 08:09 erayd

Thanks for your answer. It's helped me a lot.

ViktorKudlai avatar Sep 05 '17 08:09 ViktorKudlai

Most welcome :-)

erayd avatar Sep 05 '17 08:09 erayd

@erayd

Can you please give some example or something for adding custom error in the schema.

ravipxm avatar Feb 08 '18 10:02 ravipxm

@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 avatar Feb 08 '18 18:02 erayd

@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 avatar Feb 09 '18 03:02 ravipxm

@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 avatar Feb 11 '18 06:02 erayd

@erayd is there any prediction for 6.0.0 release?

lfdubiela avatar Jul 26 '18 20:07 lfdubiela

@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 avatar Jul 26 '18 21:07 erayd

@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 avatar Jul 26 '18 21:07 bighappyface

@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.

erayd avatar Jul 26 '18 22:07 erayd

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 avatar Jul 26 '18 23:07 shmax

@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.

erayd avatar Jul 27 '18 05:07 erayd

Righto, then. I'll just take the one at the top of the list: https://github.com/justinrainbow/json-schema/issues/525

shmax avatar Jul 27 '18 14:07 shmax