everit-org / json-schema

JSON Schema validator for java, based on the org.json API
Apache License 2.0
864 stars 282 forks source link

message null for oneOf schema validation exception. #294

Open NaveenGurram opened 5 years ago

NaveenGurram commented 5 years ago

Hi,

When there is an exception for oneOf schemas message is empty, it is possible to put some message here.

Json Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Sample enum const",
  "type": "object",
  "required": [
    "state"
  ],
  "properties": {
    "state": {
      "type": "string",
      "oneOf": [
        {
          "const": "AL",
          "title": "Alabama"
        },
        {
          "const": "AK",
          "title": "Alaska"
        }
      ]
    }
  }
}

Input

{
  "state": "XYZ"
}

and in exception, for oneOf sub schemas, message is empty for eg: schemaLocation "#/properties/state/oneOf/0" message is empty, is it possible to put some message here, because of this null i had to get the violatedSchema and look at the allowedValue to formulate a meaningful message.

Exception

{
    "schemaLocation": "#/properties/state",
    "pointerToViolation": "#/state",
    "causingExceptions": [
        {
            "pointerToViolation": "#/state",
            "causingExceptions": [
                {
                    "schemaLocation": "#/properties/state/oneOf/0",
                    "pointerToViolation": "#/state",
                    "causingExceptions": [],
                    "keyword": "const",
                    "message": ""
                },
                {
                    "schemaLocation": "#/properties/state/oneOf/1",
                    "pointerToViolation": "#/state",
                    "causingExceptions": [],
                    "keyword": "const",
                    "message": ""
                }
            ],
            "keyword": "oneOf",
            "message": "#: 0 subschemas matched instead of one"
        }
    ],
    "keyword": "allOf",
    "message": "#: only 1 subschema matches out of 2"
}
erosb commented 5 years ago

Hello @NaveenGurram , can you please confirm that this issue is about requesting a change of the exception message of const violations? Also, any suggestions on the exception message you would like to see?

NaveenGurram commented 5 years ago

@erosb yes this is requesting a change for the "const" violation exception message. I didn't think of what the message should look like, let me think about it and comment.

NaveenGurram commented 5 years ago

If possible instead of this at the oneOf level.

            "keyword": "oneOf",
            "message": "#: 0 subschemas matched instead of one"

can we have something like

            "keyword": "oneOf",
            "message": "#: has to be oneOf ['AL','AK']"

if not some message at the each oneOf violation

             {
                    "schemaLocation": "#/properties/state/oneOf/0",
                    "pointerToViolation": "#/state",
                    "causingExceptions": [],
                    "keyword": "const",
                    "message": "NEW MESSAGE HERE"
                }
erosb commented 5 years ago

Just for curiosity, why don't you use an enum schema instead of a oneOf with 2 const subschemas?

On Thu, 18 Apr 2019, 14:48 Naveen Kumar Gurram, notifications@github.com wrote:

If possible instead of this at the oneOf level.

        "keyword": "oneOf",
        "message": "#: 0 subschemas matched instead of one"

can we have something like

        "keyword": "oneOf",
        "message": "#: has to be oneOf ['AL','AK']"

if not some message at the each oneOf violation

         {
                "schemaLocation": "#/properties/state/oneOf/0",
                "pointerToViolation": "#/state",
                "causingExceptions": [],
                "keyword": "const",
                "message": "NEW MESSAGE HERE"
            }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/everit-org/json-schema/issues/294#issuecomment-484492455, or mute the thread https://github.com/notifications/unsubscribe-auth/AACCWBVBVPLIZ6EKG7W7BKLPRBU3JANCNFSM4HFGYHJQ .

NaveenGurram commented 5 years ago

yes, but we are using this schema to generate form and display value is different from the code, so only option is use this construct and this is sample we have list of all 52 US states.