rdb
rdb

Reputation: 1

Problem with Joi validation: Joi.when() condition ignored

I have this Joi schema:

const Joi = require('joi');

const DB_CONST = {
  DEFAULT_MEDIUM_STRING: a255,
  DEFAULT_STRING: 100
};

const FUNCTIONTYPE = {
  A: 'A',
  B: 'B'
};

const Testata = Joi.object().keys({
  flusso: Joi.string().max(DB_CONST.DEFAULT_MEDIUM_STRING).required(),
  functiontype: Joi.string().valid(...Object.values(FUNCTIONTYPE)).required(),
});

const Cliente = Joi.object().keys({
  codclicrm: Joi.string().max(DB_CONST.DEFAULT_STRING).required(),
  idcli: Joi.string().max(DB_CONST.DEFAULT_MEDIUM_STRING).required(),
});

const body = Joi.object().keys({
  TESTATA: Testata.required(),
  CLIENTE: Joi.when('TESTATA.functiontype', {
    is: 'A',
    then: Cliente.optional(),
    otherwise: Cliente.required()
  })
});

if I try to validate a body with TESTATA.functiontype set to 'A' and no CLIENTE aggregate, the validation returns " 'CLIENTE' is required "- While I was expecting that the validation went ok...

Upvotes: 0

Views: 25

Answers (0)

Related Questions