json_schemer icon indicating copy to clipboard operation
json_schemer copied to clipboard

array not required returns the message: "1 item required; only 0 were given" in the json schema

Open valeriaA opened this issue 2 years ago • 2 comments

I'm using Ruby + Cucumber to write my tests, and Json Schema to validate the contract of the APIs I'm testing.

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.

PS: I already tried other versions draft-06, 07...the same error is presented.

To make it clear, the problem is that the "resources" array even without any mandatory attributes inside it, in the validation of the json schema it returns this error message demanding that at least one item be returned, that is, it does not accept an empty array, even in a non-mandatory field.

valeriaA avatar Aug 17 '22 12:08 valeriaA

to share knowledge with anyone who may be interested... This stackoverflow answer solved it for me:

https://stackoverflow.com/questions/73374808/array-not-required-returns-the-message-1-item-required-only-0-were-given-in/73382238#73382238

valeriaA avatar Aug 17 '22 13:08 valeriaA

Hi @valeriaA—thanks for opening the issue. I'm having trouble reproducing it, though. Do you have a pared down example I could use? Here's my attempt:

?> schema = {
?>   "type" => "object",
?>   "properties" => {
?>     "resources" => {
?>       "type" => "array",
?>       "uniqueItems" => true,
?>       "items" => [
?>         {
?>           "type" => "object"
?>         }
?>       ]
?>     }
?>   }
>> }
=> {"type"=>"object", "properties"=>{"resources"=>{"type"=>"array", "uniqueItems"=>true, "items"=>[{"type"=>"object"}]}}}
>>
>> JSONSchemer.schema(schema).valid?({ "resources" => [] })
=> true
>>
>> JSONSchemer.schema(schema).valid?({ "resources" => [{}] })
=> true
>>
>> JSONSchemer.schema(schema).valid?({ "resources" => ["invalid"] })
=> false

It seems to allow an empty resources array.

"1 item required; only 0 were supplied." also doesn't look like an error message from this library. Do you know where those errors are coming from?

davishmcclurg avatar Aug 31 '22 00:08 davishmcclurg