ajv icon indicating copy to clipboard operation
ajv copied to clipboard

useDefaults option produces incorrect result with different inputs

Open kpanugan opened this issue 1 year ago • 0 comments

What version of Ajv are you using? Does the issue happen if you use the latest version? Ajv Version:8.12.0 Ajv options object const ajv = new Ajv({useDefaults: true, strict: false})


JSON Schema

{
  "type": "object",
  "properties": {
    "foo": {
      "type": "number",
      "default": 5
    },
    "yyy": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "default": {
            "xyz": "testValue",
            "ssms": [
              5
            ]
          },
          "properties": {
            "ssms": {
              "type": "array",
              "items": [
                {
                  "type": "array",
                  "default": [
                    2
                  ]
                },
                {
                  "type": "number"
                },
                {
                  "type": "string",
                  "default": "abcdef"
                }
              ]
            }
          }
        }
      ]
    },
    "bar": {
      "type": "object",
      "default": {
        "abc": "abc-outer"
      },
      "properties": {
        "efg": {
          "type": "number",
          "default": 88
        }
      }
    },
    "prop1": {
      "type": "array",
      "default": [],
      "items": [
        {
          "type": "array",
          "default": [
            5
          ],
          "items": {
            "type": "number"
          }
        }
      ]
    }
  }
}

Sample data

//input for first test case
{
  "foo": 6
}
//input for second test case

{
  "foo": 6,
  "yyy":[]
}

Your code

const ajv = new Ajv({useDefaults: true, strict: false})
const validate = ajv.compile(schema)
console.log(JSON.stringify(data))

Validation result, data AFTER validation, error messages

{"foo":6,"bar":{"abc":"abc-outer","efg":88},"prop1":[[5]]}-- Output for first test case
{"foo":6,"yyy":[{"xyz":"testValue","ssms":[5,null,"abcdef"]}],"bar":{"abc":"abc-outer"},"prop1":[]}--output for second test case

What results did you expect? {"foo":6,"bar":{"abc":"abc-outer","efg":88},"prop1":[[5]]}-- Output for first test case is fine and works as expected {"foo":6,"yyy":[{"xyz":"testValue","ssms":[5,null,"abcdef"]}],"bar":{"abc":"abc-outer","efg":88},"prop1":[[5]]} -- This is what i expected for second test case. The output for first test case is as expected but the output for second test case is not expected. I was expecting {"foo":6,"yyy":[{"xyz":"testValue","ssms":[5,null,"abcdef"]}],"bar":{"abc":"abc-outer","efg":88},"prop1":[[5]]} why does the output for "bar" and "prop1" change when an empty array is provided for a different field (yyy) Are you going to resolve the issue?

kpanugan avatar Jul 05 '23 06:07 kpanugan