Trying to create a logic for my validator functions that involves using objects as errorMaps for input validation. In the code snippet provided, args.drugName
is an optional field. If the user provides text, we want to ensure it is greater than 3 letters; otherwise, if it's empty, the success condition should be considered valid. How can we address this issue for optional parameters in TypeScript?
main.js
{
errorKey: ValidationErrorEnum.InvalidDrugName,
successCondition: (args: DrugPriceParam) => {
let isValid: boolean = false;
isValid = args.drugName.length >= 3 ? true : _.isEmpty(args.drugName) ? true : false;
// if (args.drugName && args.drugName.length >= 3) {
// isValid = true;
// } else if (_.isEmpty(args.drugName)) {
// isValid = true;
// }
return isValid;
}
Error:
error TS2532: Object is possibly 'undefined'.