OptimumCode / json-schema-validator

The JSON schema validation library that works with https://github.com/Kotlin/kotlinx.serialization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Too much validation errors

OussemaHammami opened this issue · comments

I faced an issue when using this library.
I have an object named "type" marked as required. When it didn't appear in the JSON object to be validated i am expecting only an error mentioning the missing object. But, in addition to this error, some other errors related to conditions from the block of "allOf" based on the missing object("type") are also thrown also. It is supposed to return only the error of the missing object "type".

My JSON schema:

"title": "resource",
  "type": "object",
  "required": [
    "type"
  ],
  ...
  
  "allOf": [
    {
      "if": {
        "properties": {
          "type": {
            "enum": [
              "enum1",
              "enum2"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "host": {
            "properties": {
		.
		.
		.
              }
            }
          }
        },
        "required": [
          "host"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "enum": [
              "enum1",
              "enum2",
              "enum3",
              "enum4",
            ]
          }
        }
      },
      "then": {
        "required": [
          "amount"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "enum": [
              "enum5"
            ]
          }
        }
      },
      "then": {
        "properties": {
		.
		.
		.
        },
        "required": [
          "payment_type"
        ]
      }
    }
  ]

json to be validated:

"resource": {}

Returned errors:

  • missing required properties: [host]
  • missing required properties: [payment_type]
  • missing required properties: [type]

I think it is supposed to return :

  • missing required properties: [type]

Hi, @OussemaHammami.
Thank you for raising this to me and sorry for the inconvenience. I will check this as soon as possible

Hi, @OussemaHammami.
I reviewed the schema you shared and it looks like the behavior of the library is correct.

In the allOf block you have an array of schemas with if-then applicators. If the object passes if applicator then applicator is invoked. The if applicator looks like this:

{
  "if": {
    "properties": {
      "type": {
         "enum": ["enum1", "enum2"]
      }
    }
  }
}

According to JSON schema specification (I am referring to the latest draft but it is also true for previous ones):

Validation succeeds if, for each name that appears in both the instance and as a name within this keyword's value, the child instance for that name successfully validates against the corresponding schema.

This means if an object has field type the schema for this property will be applied to the field's value. If the object does not have this field (which is the case here) the schema for type property won't be applied. As a result, the object passes if applicator, and then applicator is applied.

I think you could modify the if schema to check if the object contains type field:

{
  "if": {
    "properties": {
      "type": {
         "enum": ["enum1", "enum2"]
      }
    },
   "required": ["type"]
  }
}

If you do this the then applicator should not be invoked and you should have only one validation error.

If you have any questions don't hesitate to ask. Kindly, close the issue if the above solves the problem.

Closing this issue as the library behavior matches the JSON specification.
@OussemaHammami if you have any more question about the library don't hesitate to ask by creating a new issue