Skip to content
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

Custom error message inside JSON Schema. #444

Open
ViktorKudlai opened this issue Sep 5, 2017 · 16 comments
Open

Custom error message inside JSON Schema. #444

ViktorKudlai opened this issue Sep 5, 2017 · 16 comments

Comments

@ViktorKudlai
Copy link

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.

@erayd
Copy link
Collaborator

erayd commented Sep 5, 2017

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
Copy link
Collaborator

erayd commented Sep 5, 2017

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
Copy link
Collaborator

erayd commented Sep 5, 2017

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

@ViktorKudlai
Copy link
Author

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

@erayd
Copy link
Collaborator

erayd commented Sep 5, 2017

Most welcome :-)

@ravipxm
Copy link

ravipxm commented Feb 8, 2018

@erayd

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

@erayd
Copy link
Collaborator

erayd commented Feb 8, 2018

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

@ravipxm
Copy link

ravipxm commented Feb 9, 2018

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

@erayd
Copy link
Collaborator

erayd commented Feb 11, 2018

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

@lfdubiela
Copy link

@erayd is there any prediction for 6.0.0 release?

@erayd
Copy link
Collaborator

erayd commented Jul 26, 2018

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

@bighappyface
Copy link
Collaborator

@erayd do you think that this plan for 6.0.0 is current?

#390 (comment)

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.

@erayd
Copy link
Collaborator

erayd commented Jul 26, 2018

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

@shmax
Copy link
Collaborator

shmax commented Jul 26, 2018

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.

@erayd
Copy link
Collaborator

erayd commented Jul 27, 2018

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

@shmax
Copy link
Collaborator

shmax commented Jul 27, 2018

Righto, then. I'll just take the one at the top of the list: #525

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants