Nested Array, Validation on min items/required

I have a nested array that is required and has a minItems of 1, however it does not cause the validation to fail. Unlike a non-nested array which immediately fails the validation. Both the array and nested array are indicated as required, however it does not seem to be working on the nested array.

Here is the schema.json I am using for testing:

{
  "type" : "object",
  "required": [
      "array_1"
  ],
  "properties": {
      "array_1": {
          "minItems": 1,
          "items": {
              "required": [
                  "method_array_1",
                  "detail_array_1"
              ],
              "properties": {
                  "method_array_1": {
                      "type": "string",
                      "oneOf": [
                          {
                              "title": "Email",
                              "const": "EMAIL"
                          },
                          {
                              "title": "Phone",
                              "const": "PHONE"
                          }
                      ],
                      "title": "Method"
                  },
                  "detail_array_1": {
                      "type": "string",
                      "title": "Detail"
                  }
              },
              "type": "object"
          },
          "type": "array",
          "title": "Array 1 Title"
      },
      "neseted_array_2": {
          "type": "object",
          "title": "Nested Array 2",
          "required" : [
            "array_2"
          ],
          "properties": {
              "array_2": {
                  "minItems": 1,
                  "items": {
                      "required": [
                          "method_array_2",
                          "detail_array_2"
                      ],
                      "properties": {
                          "method_array_2": {
                              "type": "string",
                              "oneOf": [
                                  {
                                      "title": "Email",
                                      "const": "EMAIL"
                                  },
                                  {
                                      "title": "Phone",
                                      "const": "PHONE"
                                  }
                              ],
                              "title": "Method"
                          },
                          "detail_array_2": {
                              "type": "string",
                              "title": "Detail"
                          }
                      },
                      "type": "object"
                  },
                  "type": "array",
                  "title": "Array 2 Title"
              }
          }
      }
  }
}

Hi @hellsfantasy,

I assume that your data is undefined or an empty object. In that case there will only be one error: That array_1 is missing from the root object. There will be no further errors as the neseted_array2 does not exist yet, so because it does not exist, there will also no required error reported for its array_2 property, i.e. a missing neseted_array2 is valid.

You can try it yourself via an online JSON Schema validator. Entering the JSON Schema there with an empty object value will also only report one error.

Thank you. If I add that nested_array_2 is required, see below, when using the online JSON Schema validator, it now reports both fields, “Required properties are missing from object: array_1, neseted_array_2.”, however within JSONForms it still has the same screenshot and not having a validation error. Ultimately I am trying to enforce both arrays are populated.

{
    "type": "object",
    "required": [
        "array_1",
        "neseted_array_2"
    ],
    "properties": {
        "array_1": {
            "minItems": 1,
            "items": {
                "required": [
                    "method_array_1",
                    "detail_array_1"
                ],
                "properties": {
                    "method_array_1": {
                        "type": "string",
                        "oneOf": [{
                                "title": "Email",
                                "const": "EMAIL"
                            }, {
                                "title": "Phone",
                                "const": "PHONE"
                            }
                        ],
                        "title": "Method"
                    },
                    "detail_array_1": {
                        "type": "string",
                        "title": "Detail"
                    }
                },
                "type": "object"
            },
            "type": "array",
            "title": "Array 1 Title"
        },
        "neseted_array_2": {
            "type": "object",
            "title": "Nested Array 2",
            "required": [
                "array_2"
            ],
            "properties": {
                "array_2": {
                    "minItems": 1,
                    "items": {
                        "required": [
                            "method_array_2",
                            "detail_array_2"
                        ],
                        "properties": {
                            "method_array_2": {
                                "type": "string",
                                "oneOf": [{
                                        "title": "Email",
                                        "const": "EMAIL"
                                    }, {
                                        "title": "Phone",
                                        "const": "PHONE"
                                    }
                                ],
                                "title": "Method"
                            },
                            "detail_array_2": {
                                "type": "string",
                                "title": "Detail"
                            }
                        },
                        "type": "object"
                    },
                    "type": "array",
                    "title": "Array 2 Title"
                }
            }
        }
    }
}

Hi @hellsfantasy,

You’re right, the “Nested Array 2” is not shown with an error in the UI. The reason for that is that neseted_array_2 is actually an object and not an array. We don’t show any errors on “object” themselves. Our off-the-shelf default object renderer is more a “view” than a “control”. What I mean by that is that the object renderer itself does not have any controls, i.e. the user can not even create the object if they wanted to, nor can they delete it. The object will be automatically created as soon as a user modifies a nested value, e.g. adding an entry to array_2.

To workaround this issue you could initialize neseted_array_2 always with an empty object. You could implement this yourself, or more generically, use AJV’s default support.