Home > Software engineering >  Custom string validation with Joi.js
Custom string validation with Joi.js

Time:10-19

I have an input in my API and I need to validate it and see if it is one the two string that I desire. Is there way to validate the string with Joi.js

type DummyValidatorInput = {
  firstInput: string;
  secondInput: string;
}

export const DummyValidator = (
  input: DummyValidatorInput,
): Joi.ValidationResult => {
  return Joi.object({
    firstInput: Joi.string().min(5).max(64).required(),
    secondInput: Joi.string().required()
  }).validate(input);
};

I want to validate that if the secondInput is customerTypeOne string or customerTypeTwo string, is this possible with Joi? Any idea or suggestion is appreciated :)

CodePudding user response:

Did you try using any.valid?

secondInput: Joi.string().valid('customerTypeOne', 'customerTypeTwo').required()
  • Related