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

Required validation does not seem to be getting triggered #965

Closed
dure0520 opened this issue Feb 12, 2024 · 3 comments
Closed

Required validation does not seem to be getting triggered #965

dure0520 opened this issue Feb 12, 2024 · 3 comments

Comments

@dure0520
Copy link

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

stevehu commented Feb 13, 2024

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

@stevehu stevehu closed this as completed Feb 13, 2024
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