networknt / json-schema-validator

A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Apache License 2.0
822 stars 323 forks source link

Required validation does not seem to be getting triggered #965

Closed dure0520 closed 6 months ago

dure0520 commented 6 months ago

I am using the latest version 1.3.2 and validating our payload against our schema with a SpecVersion = V7

We have properties defined with "type: "string" and also as "required"

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "header": {
      "type": "object",
      "properties": {
        "order": {
          "type": "string"
        },
        "customer": {
          "type": "string"
        },
        "customerName": {
          "type": "string"
        },
        "orderDate": {
          "type": "string"
        },
      },
      "required": [
        "order",
        "customer",
        "customerName",
        "orderDate",
      ]
    },
    "required": [ "header" ]
}

When I try to validate against a JSON payload that does not contain one of the required fields, instead of getting an error like header.customer: is missing but it is required we are now receiving a message that looks like header.customer: null found, string expected

We had used an earlier version (1.0.67) and required validation was working as expected

justin-tay commented 6 months ago

Your schema isn't valid JSON, and you didn't post your input data, so it's hard to narrow down the issue.

If your property is present in the input data but is null, then what you are seeing is expected behavior and I don't think the version matters.

Given this schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "header": {
      "type": "object",
      "properties": {
        "order": {
          "type": "string"
        },
        "customer": {
          "type": "string"
        },
        "customerName": {
          "type": "string"
        },
        "orderDate": {
          "type": "string"
        }
      },
      "required": [
        "order",
        "customer",
        "customerName",
        "orderDate"
      ]
    }
  },
  "required": [
    "header"
  ]
}

Test 1

Data

{}

Messages

$: required property 'header' not found

Test 2

Data

{
  "header": {}
}

Messages

$.header: required property 'order' not found
$.header: required property 'customer' not found
$.header: required property 'customerName' not found
$.header: required property 'orderDate' not found

Test 3

Data

{
  "header": {
    "customer": null
  }
}

Messages

$.header.customer: null found, string expected
$.header: required property 'order' not found
$.header: required property 'customerName' not found
$.header: required property 'orderDate' not found
dure0520 commented 6 months ago

Sorry, I our schema is a little more complex and I was just trying to simplify it some. I am using SpringBoot and have mapped request objects to the JSON object. After some further expperimentation, I have now discovered that Java is setting the value of missing JSON properties to null in the mapped Java request object. I will have to see if I can change that behavior somehow.

Your validator IS working as expected. Thanks for your help as it lead me to discover the real problem.

stevehu commented 6 months ago

Given @dure0520's comment, close this issue. Thanks.