Home > database >  array not required returns the message: "1 item required; only 0 were given" in the json s
array not required returns the message: "1 item required; only 0 were given" in the json s

Time:08-18

I configured the json schema and it looked like this:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "data": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "organisationId": {
          "type": "string"
        },
        "clientId": {
          "type": "string"
        },
        "issuer": {
          "oneOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ]
        },
        "createdDate": {
          "type": "string",
          "format": "date-time"
        },
        "lastModifiedDate": {
          "type": "string",
          "format": "date-time"
        },
        "consentId": {
          "type": "string"
        },
        "internalStatus": {
          "type": "string",
          "enum": [
            "AUTHORISED",
            "AWAITING_AUTHORISATION",
            "REJECTED",
            "TIMEOUT_EXPIRED",
            "OVERDUE",
            "REVOKED"
          ]
        },
        "permissions": {
          "type": "array",
          "items": [
            {
              "type": "string"
            }
          ]
        },
        "approverType": {
          "type": "string",
          "enum": [
            "AND",
            "OR"
          ]
        },
        "status": {
          "type": "string",
          "enum": [
            "AUTHORISED",
            "AWAITING_AUTHORISATION",
            "REJECTED",
            "REVOKED",
            "CONSUMED"
          ]
        },
        "statusUpdateDateTime": {
          "type": "string",
          "format": "date-time"
        },
        "expirationDateTime": {
          "type": "string",
          "format": "date-time"
        },
        "resourceGroups": {
          "uniqueItems": true,
          "oneOf": [
            {
              "type": "array"
            },
            {
              "type": "null"
            }
          ],
          "items": [
            {
              "type": "object",
              "properties": {
                "resourceGroupId": {
                  "type": "integer"
                },
                "permissions": {
                  "type": "array",
                  "items": [
                    {
                      "type": "string"
                    }
                  ]
                },
                "resources": {
                  "type": "array",
                  "uniqueItems": true,
                  
                  
                  "items": [
                    {
                      "type": "object",
                      "properties": {
                        "status": {
                          "type": "string",
                          "enum": [
                            "AVAILABLE",
                            "UNAVAILABLE",
                            "TEMPORARY_UNAVAILABLE",
                            "PENDING_AUTHORISATION"
                          ]
                        },
                        "additionalInfos": {
                          "type": "array",
                          "items": [
                            {
                              "type": "object",
                              "properties": {
                                "key": {
                                  "type": "string"
                                },
                                "value": {
                                  "type": "string"
                                }
                              }
                            }
                          ]
                        },
                        "type": {
                          "type": "string",
                          "enum": [
                            "CUSTOMERS_PERSONAL_IDENTIFICATIONS",
                            "CUSTOMERS_PERSONAL_QUALIFICATION",
                            "CUSTOMERS_PERSONAL_ADITTIONALINFO",
                            "CUSTOMERS_BUSINESS_IDENTIFICATIONS",
                            "CUSTOMERS_BUSINESS_QUALIFICATION",
                            "CUSTOMERS_BUSINESS_ADITTIONALINFO",
                            "CAPITALIZATION_TITLES",
                            "PENSION",
                            "DAMAGES_AND_PEOPLE_PATRIMONIAL",
                            "DAMAGES_AND_PEOPLE_AERONAUTICAL",
                            "DAMAGES_AND_PEOPLE_NAUTICAL",
                            "DAMAGES_AND_PEOPLE_NUCLEAR",
                            "DAMAGES_AND_PEOPLE_OIL",
                            "DAMAGES_AND_PEOPLE_RESPONSABILITY",
                            "DAMAGES_AND_PEOPLE_TRANSPORT",
                            "DAMAGES_AND_PEOPLE_FINANCIAL_RISKS",
                            "DAMAGES_AND_PEOPLE_RURAL",
                            "DAMAGES_AND_PEOPLE_AUTO",
                            "DAMAGES_AND_PEOPLE_HOUSING",
                            "DAMAGES_AND_PEOPLE_PEOPLE",
                            "DAMAGES_AND_PEOPLE_ACCEPTANCE_AND_BRANCHES_ABROAD"
                          ]
                        },
                        "hidden": {
                          "type": "boolean"
                        },
                        "resourceId": {
                          "type": "string"
                        }
                      }
                    }
                  ]
                },
                "additionalInfos": {
                  "type": "array",
                  "items": [
                    {
                      "type": "object",
                      "properties": {
                        "key": {
                          "type": "string"
                        },
                        "value": {
                          "type": "string"
                        }
                      }
                    }
                  ]
                },
                "type": {
                  "type": "string",
                  "enum": [
                    "ACCOUNT",
                    "CREDIT_CARD_ACCOUNT",
                    "LOAN",
                    "INVOICE_FINANCING",
                    "UNARRANGED_ACCOUNT_OVERDRAFT",
                    "FINANCING",
                    "RESOURCE",
                    "CUSTOMER"
                  ]
                }
              },
              "required": [
                "permissions",
                "type"
              ]
            }
          ]
        },
        "approvers": {
          "type": "array",
          "items": [
            {
              "type": "object",
              "properties": {
                "status": {
                  "type": "string",
                  "enum": [
                    "AUTHORISED",
                    "AWAITING_AUTHORISATION",
                    "REJECTED",
                    "REVOKED",
                    "CONSUMED"
                  ]
                },
                "approverId": {
                  "type": "string"
                }
              },
              "required": [
                "approverId"
              ]
            }
          ]
        },
        "loggedUser": {
          "type": "object",
          "properties": {
            "document": {
              "type": "object",
              "properties": {
                "identification": {
                  "type": "string"
                },
                "rel": {
                  "type": "string"
                }
              },
              "required": [
                "identification",
                "rel"
              ]
            }
          },
          "required": [
            "document"
          ]
        },
        "businessEntity": {
          "type": "object",
          "properties": {
            "document": {
              "type": "object",
              "properties": {
                "identification": {
                  "type": "string"
                },
                "rel": {
                  "type": "string"
                }
              },
              "required": [
                "identification",
                "rel"
              ]
            }
          },
          "required": [
            "document"
          ]
        }
      },
      "required": [
        "organisationId",
        "clientId",
        "consentId",
        "permissions",
        "approverType",
        "status",
        "statusUpdateDateTime",
        "expirationDateTime",
        "loggedUser"
      ]
    },
    "links": {
      "type": "object",
      "properties": {
        "self": {
          "type": "string"
        },
        "first": {
          "type": "string"
        },
        "prev": {
          "type": "string"
        },
        "next": {
          "type": "string"
        },
        "last": {
          "type": "string"
        }
      },
      "required": [
        "self"
      ]
    },
    "meta": {
      "type": "object",
      "properties": {
        "totalRecords": {
          "type": "integer"
        },
        "totalPages": {
          "type": "integer"
        }
      },
      "required": [
        "totalRecords",
        "totalPages"
      ]
    }
  }
}

Note that the "resources" array is not required, it is not mandatory.

However... when I run my test and it returns an empty array in "resources":

"resourceGroupId":1,
      "permissions":[
         "CUSTOMERS_PERSONAL_QUALIFICATION_READ",
         "CUSTOMERS_PERSONAL_IDENTIFICATIONS_READ"
      ],
      "resources":[],
      "type":"CUSTOMER"
   }

I get the following message:

"#/data/resourceGroups/0/resources: failed schema #/properties/data/properties/resourceGroups/items/0/properties/resources: 1 item required; only 0 were supplied."

I don't understand how 1 item is required if the array is not required.

and still have( "uniqueItems": true )

which in theory would accept a [] in the return, according to the Json schema documentation.

I've tried passing minItems=0 and many other things and nothing has worked.

CodePudding user response:

This looks like a combination of a bug in the validator you are using and an incorrect usage of items. The good news is that when you use items correctly, the bug will probably not apply.

The items keyword has two forms: one that takes a single schema and the other that takes an array of schemas. The form must people need most of the time is the single schema form.

{
  "type": "array",
  "items": { "type": "string" }
}

This schemas asserts that every item in the array is a string.

{
  "type": "array",
  "items": [
    { "type": "string" },
    { "type": "number" }
  ]
}

This schema asserts that the first item in the array is a string and the second is a number. However, it should not require that those items are present or assert anything on the rest of the items in the array. So, the bug is that your validator seems to require those values when it shouldn't.

But, that bug shouldn't affect you because you I'm sure you really meant to use the single schema version of items that validates all the items in the array against the schema. Just remove the [ and ] and your schema should work as you expected.

  • Related