0

I have a query parameter method which can have 4 values like sum, sub, div, mul. I want to validate if only above 4 values are passed in the query param or combination of above 4 values are passed with comma separated.

Correct cases:

  1. method=sum
  2. method=sum,mul

Incorrect cases:

  1. method=per
  2. method=sum,per

I have tried below to validate the single values: config = validate: { query: { method: joi.string().valid('sum','sub','mul','div')}}

I want to validate combinations as well like method=sum,div

Madhu
  • 1
  • 1
  • 1
  • Disclaimer that I don't know joi but it's possible `query` needs to be a Joi object using `Joi.object().keys` as seen in this answer: https://stackoverflow.com/questions/50156176/how-to-use-enum-values-with-joi-string-validation – Allie Howe Jan 18 '23 at 14:26
  • Thanks for your support. I am able to validate one of the available values like `method=sum` but i want to validate `method=sum,mul` as success validation. – Madhu Jan 19 '23 at 04:33
  • I'd suggest changing `method` to accept an array of strings rather than trying to validate a string that contains multiple valid/invalid options inside of it – Allie Howe Jan 19 '23 at 12:26

1 Answers1

0

If the valid values for method are not that many, just hardcode them. For example:

joi.string().valid(
  'sum', 'sub', 'mul', 'div',
  'sum,div', 'sum,mul', 'sum,sub',
  // etc...
)

If there are too many combinations or you simply don't want to hardcode them, write a function that calculates them for you. Something like this one.

jackdbd
  • 4,583
  • 3
  • 26
  • 36