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

useDefaults option produces incorrect result with different inputs #2304

Open
kpanugan opened this issue Jul 5, 2023 · 0 comments
Open

useDefaults option produces incorrect result with different inputs #2304

kpanugan opened this issue Jul 5, 2023 · 0 comments

Comments

@kpanugan
Copy link

kpanugan commented Jul 5, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant