1
votes

I am new to JSON. I have a schema mentioned as :

*

const schema = Joi.array().min(3).has({
    Name: Joi.string().required(),
    Type: Joi.string().required(),
    Price: Joi.number().required(),
});

My JSON is like :

{
    "foods": [
    {
      "Name": "Item1",
      "Type": "Category1",
      "Price": 14
    },
    {
      "Name": "Item2",
      "Type": "Category2",
      "Price": 8
    },
    {
      "Name": "Item3",
      "Type": "Category1",
      "Price": 10
    }
  ]
}

But when I validating this, I am getting error as: joi validationerror 'value' must be an array.

Please let me know what needs to be changed in JSON object to be validated by the schema.

1

1 Answers

0
votes

Your schema expects an array with minimum lenght of 3 as root, but you are passing an object with a single property foods, which then is an array. So you must get rid of the surrounding object.

This would be a valid input to your validator

[
  {
    "Name": "Item1",
    "Type": "Category1",
    "Price": 14
  },
  {
    "Name": "Item2",
    "Type": "Category2",
    "Price": 8
  },
  {
    "Name": "Item3",
    "Type": "Category1",
    "Price": 10
  }
]