Skip to content

Array length cannot reference to another field #1017

@anhthang

Description

@anhthang

Context

  • node version: 6.9.1
  • joi version: 9.2.0
  • environment: node
  • used with: expressjs

What are you trying to achieve or the steps to reproduce ?

Describe your issue here, include schemas and inputs you are validating if needed.

const schema = Joi.object().keys({
    numberOfAdults: Joi.number().integer().min(1).max(10).required(),
    numberOfChildren: Joi.number().integer().min(1).max(10),
    childAges: Joi.when('numberOfChildren', {
        is: Joi.number().required(),
        then: Joi.array().items(Joi.number().integer().required()).length(Joi.ref('numberOfChildren')).required()
    })
})

Which result you had ?

Error: limit must be a positive integer
<br> &nbsp; &nbsp;at Object.exports.assert (/Users/buianhthang/my_project/node_modules/joi/node_modules/hoek/lib/index.js:736:11)
<br> &nbsp; &nbsp;at length (/Users/buianhthang/my_project/node_modules/joi/lib/array.js:421:14)

What did you expect ?

expect childAges length equals numberOfChildren value

{
  "code": 400,
  "message": "Missing or invalid params",
  "verbosity": [
    {
      "message": "\"childAges\" must contain (x) items",
      "path": "roomList.0.childAges",
      "type": "array.length",
      "context": {
        "limit": 5,
        "value": [
          4
        ],
        "key": "childAges"
      }
    }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew functionality or improvement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions