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

Validating optional fields within optional fields within arrays fails incorrectly #8

Open
jeshuamaxey opened this issue Sep 22, 2023 · 0 comments

Comments

@jeshuamaxey
Copy link

This is really hard to explain in words, but the reproduction should help explain. If you are validating an array of objects and those objects have optional properties which themselves have optional properties, the validation fails to take into account the optionality of the nested fields.

reproduction

import { expect } from "chai"
import chaiJsonPattern from "chai-json-pattern"

describe.only("failing check against arrays of objects with optional fields which have optional sub fields", () => {
  const jsonPattern = `{
    "data": [{
      "id": String,
      
      "optional_object"?: {
        "required_string_field": String,
        "optional_string_field"?: String,

        "optional_sub_object"?: {
          "required_string_field": String,
          "optional_string_field"?: String,
        }
      }
    }]
  }`

  describe("without optional sub object", () => {
    it("should pass when array contains element with optional object ", () => {
      expect({
        data: [
          {
            id: "1",
          },
        ],
      }).to.matchPattern(jsonPattern)
    })

    it("should pass when array contains element without optional object", () => {
      expect({
        data: [
          {
            id: "1",
            optional_object: {
              required_string_field: "1",
              optional_string_field: "1",
            },
          },
        ],
      }).to.matchPattern(jsonPattern)
    })

    it("should pass when array contains elements with and without optional object", () => {
      expect({
        data: [
          {
            id: "1",
            optional_object: {
              required_string_field: "1",
              optional_string_field: "1",
            },
          },
          {
            id: "1",
          },
        ],
      }).to.matchPattern(jsonPattern)
    })
  })

  describe("with optional sub object", () => {
    it("should pass when array contains element with optional object with optional sub object", () => {
      expect({
        data: [
          {
            id: "1",
            optional_object: {
              required_string_field: "1",
              optional_string_field: "1",
              optional_sub_object: {
                required_string_field: "1",
                optional_string_field: "1",
              },
            },
          },
        ],
      }).to.matchPattern(jsonPattern)
    })

    it("should pass when array contains 2 elements with optional object, where one contains the optional sub object and the other doesn't", () => {
      expect({
        data: [
          {
            id: "1",
            optional_object: {
              required_string_field: "1",
              optional_string_field: "1",
            },
          },
          {
            id: "1",
            optional_object: {
              required_string_field: "1",
              optional_string_field: "1",
              optional_sub_object: {
                required_string_field: "1",
                optional_string_field: "1",
              },
            },
          },
        ],
      }).to.matchPattern(jsonPattern)
    })
  })
})
Screenshot 2023-09-22 at 16 33 01
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

1 participant