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 #241

Closed
d-maks opened this issue Nov 13, 2018 · 4 comments
Closed

Custom error message #241

d-maks opened this issue Nov 13, 2018 · 4 comments

Comments

@d-maks
Copy link

d-maks commented Nov 13, 2018

Hi,
We need to have possibility to customize validation error message in order to make it more user friendly and also to make them multilingual. After learning documentation and source code we didn't find how to do it. Can you add example into documentation or develop this feature or explain if there is a workaround.

Or can you explain if it is possible to switch off ignoring of additional keywords in schema (in this extended keyword we could keep our custom error messages):
"Implementations MAY define additional keywords to JSON Schema. Save for explicit agreement, schema authors SHALL NOT expect these additional keywords to be supported by peer implementations. Implementations SHOULD ignore keywords they do not support."?

Thank you so much for your library!

@erosb
Copy link
Contributor

erosb commented Nov 13, 2018

Hello,

a) regarding internationalization: if you want to keep the structure of the errors but you just want to make the actual messages translated, then probably you would benefit from the ValidationException#toJSON() method which returns a JSONObject. That is a mutable structure so you can traverse it and replace the message strings to whatever human-friendly version you want.

The library itself won't provide direct support to i18n, because that would be followed by an unmanageable amount of library-integration & customization needs, so we are not going to do that. The approach is that the library gives programmer-friendly error description which can be turned by the caller into a human-readable form, if necessary.

b) I'm a bit confused about what you mean by "ignoring additional keywords". Can you please elaborate?

Thanks.

@d-maks
Copy link
Author

d-maks commented Nov 14, 2018

Hello,

Thank you for your response.
In question about ignoring additional keywords I mean can I have custom property in schema that will not be ignored by your library.

For example I tried adding to schema property "message" to use it for storing error message for this schema and expected to see this property inside exception threw during validation:

{
   "properties": {
      "radioButton51": {
         "type": "object",
         "properties": {
            "value": {
               "type": "string",
                "not": {
                   "enum": [
                      "No"
                    ]
                 }
             }
         },
         "required": [
            "value"
         ],
         "message": "Custom message"
      }
   }
}

But schema contained in ValidationException didn't have property "message".
So my question is: is there any way to have this property in ValidationException?

Thanks.

@joeydh
Copy link

joeydh commented Nov 16, 2018

Other implementations when they return the violated schema they will return the whole violatedschema and even elements they do not recognize. This way you can use the violated schema to get the unique validation message. So you can do Violatedschema -> messages -> minlength for example to do custom validation errors.

My solution now is to put the custom validation errors in the file I have for generating the front-end.

And couple it by the path.

{
  "type": "VerticalLayout",
  "elements": [
    {
      "type": "HorizontalLayout",
      "elements": [
        {
          "type": "Control",
          "scope": "#/properties/age",
          "messages": {
            "minimum": "This person is too young"
          }
        },
        {
          "type": "Control",
          "scope": "#/properties/firstName"
          "messages": {
            "minLength": "The persons firstname is too short"
          }
        }
      ]
    }
  ]
}

Depending on your needs you can also create a simpler extra structure like this:

[
  {
    "scope": "#/properties/age",
    "messages": {
      "minimum": "This person is too young"
    }
  },
  {
    "scope": "#/properties/firstName",
    "messages": {
      "minimum": "The persons firstname is too short"
    }
  }
]

@erosb
Copy link
Contributor

erosb commented Dec 15, 2018

This is fixed in version 1.10.0 , Schema#getUnprocessedProperties() now returns the non-schema properties.

@erosb erosb closed this as completed Dec 15, 2018
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

3 participants