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

No support for multiple error message formats per keyword #802

Open
anjnerajat opened this issue Jun 2, 2023 · 1 comment
Open

No support for multiple error message formats per keyword #802

anjnerajat opened this issue Jun 2, 2023 · 1 comment

Comments

@anjnerajat
Copy link
Contributor

Hi @stevehu
Looking for some suggestions around this use-case. Please redirect me to the threads if this has already been discussed.

Say there is a custom keyword defined in a custom meta schema whose validator does multiple kinds of validations and needs more than one message format to be used for showing error for those cases.
Ex: a custom keyword - "isRecordReference". Its validation logic does two things, check existence of the referenced record id and also checks whether referenced record allows being referenced or not (assume that the record can put some limitations on its access)
So now the error message for these two cases can't be same. For former scenario it should be "Record {0} not found" and for later "Record {0} can't be referenced here"

Sample Consuming Schema:

{
	"type": "string",
	"isContentReference": true
}

With the current one on one mapping between a keyword and a message format this cannot be achieved.

One way to achieve this could be to have one to many relationship b/w a keyword and its error codes and then have one message corresponding to each error code. The messages could be fetched based on error code the validator provides while constructing ValidationMessage instead of its keyword name.

Am I thinking in the wrong direction here? Can this library be modified to achieve such use-case? And does this go against any of JSON spec guidelines?
Please let me know your thoughts and thanks for all the help!

fyi @prashanth-chaitanya

@justin-tay
Copy link
Contributor

This has been supported since 1.2.0 by specifying a messageKey when building the message.

public S messageKey(String messageKey) {
this.messageKey = messageKey;
return self();
}

return format.validate(executionContext, validationContext, node, rootNode, instanceLocation,
assertionsEnabled,
() -> this.message().instanceNode(node).instanceLocation(instanceLocation)
.messageKey(format.getMessageKey())
.locale(executionContext.getExecutionConfig().getLocale())
.failFast(executionContext.isFailFast()),
this);

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

2 participants