Validating multiple parameters by regex and length I have a set of validation functions which return a response code along with an error message if the validation fails. Requests can be of three types, and each of them contains a different type of values to be validated. My question is what are the options to avoid the repetitive code, particularly in the message assignment?

const Validation = (function() {

  const isParameterOneLengthValid = (data) =&gt; {
    return data.length &gt;= 5 &amp;&amp; data.length &lt;= 10;
  };

  const isParameterOneValid = (data) =&gt; {
    return !(/[^A-Za-z\-0-9\,\.]/.test(data));
  };

  const isParameterTwoLengthValid = (data) =&gt; {
    return data.length &gt;= 1 &amp;&amp; data.length &lt;= 5;
  };

  const isParameterTwoValid = (data) =&gt; {
    return !(/[^0-9]/.test(data));
  };

  function Validation() {}

  Validation.prototype.validateFirstTypeRequest= (data) =&gt; {
      if (!isParameterOneLengthValid(data.parameterOne)) {
        data.code = 0;
        data.message = 'Length of the parameter one is invalid';
        return data;
      } else if (!isParameterOneValid(data.parameterOne)) {
        data.resStatus = 1;
        data.resMessage = 'Parameter one is invalid';
        return data;
      }

      if (!isParameterTwoLengthValid(data.parameterTwo)) {
        data.code = 3;
        data.message = 'Length of the parameter two is invalid';
        return data;
      } else if (!isParameterTwoValid(data.parameterTwo)) {
        data.resStatus = 4;
        data.resMessage = 'Parameter two is invalid';
        return data;
      }

      return data;
  };

  Validation.prototype.validateSecondTypeRequest= (data) =&gt; {
      if (!isParameterOneLengthValid(data.parameterOne)) {
        data.code = 0;
        data.message = 'Length of the parameter one is invalid';
        return data;
      } else if (!isParameterOneValid(data.parameterOne)) {
        data.resStatus = 1;
        data.resMessage = 'Parameter one is invalid';
        return data;
      }

      return data;
  };

  Validation.prototype.validateThirdTypeRequest= (data) =&gt; {
      if (!isParameterTwoLengthValid(data.parameterTwo)) {
        data.code = 3;
        data.message = 'Length of the parameter two is invalid';
        return data;
      } else if (!isParameterTwoValid(data.parameterTwo)) {
        data.resStatus = 4;
        data.resMessage = 'Parameter two is invalid';
        return data;
      }

      return data;
  };

  module.exports = new Validation();

  return Validation;
})();
Mutating State

I really don't like that your validation functions modify the data object that they are given. It seems like an unnecessary mutation that's mixing data with error codes. Do you have any particular reason for returning the data object? If it's passed into the function, then wouldn't the user already has a pointer to it? My reasoning is that you could instead return an failed validation response object instead of mutating the data object. Some problems with mutating the data object are immediately clear; now you can't have a property in data called code, message, resStatus, or resMessage.

Singe Exit Point

I'm not sure if this came from sanitizing the code, but the returns in the validateResponse functions are a bit redundant. One programming platitude is to strive to give functions one exit point. Currently, your validateSecondTypeRequest function has three exit points:

Validation.prototype.validateSecondTypeRequest= (data) =&gt; {
  if (!isParameterOneLengthValid(data.parameterOne)) {
    data.code = 0;
    data.message = 'Length of the parameter one is invalid';
    return data; // these returns could be merged
  } else if (!isParameterOneValid(data.parameterOne)) {
    data.resStatus = 1;
    data.resMessage = 'Parameter one is invalid';
    return data; // these returns could be merged
  }

  return data; // these returns could be merged
};


The returns in the if else block are unnecessary and could be easily eliminated to:

Validation.prototype.validateSecondTypeRequest= (data) =&gt; {
  if (!isParameterOneLengthValid(data.parameterOne)) {
    data.code = 0;
    data.message = 'Length of the parameter one is invalid';
  } else if (!isParameterOneValid(data.parameterOne)) {
    data.resStatus = 1;
    data.resMessage = 'Parameter one is invalid';
  }

  return data;
};


Eliminating Duplicate Code

Write a function for each parameter:

function validateParameterOne(value) {
  let result;

  if (value.length &gt;= 5 &amp;&amp; value.length &lt;= 10) {
    result = {
      code: 0,
      message: 'Length of the parameter one is invalid'
    };
  };
  else if (!(/[^A-Za-z\-0-9\,\.]/.test(data))) {
    result = {
      resStatus: 1,
      resMessage: 'Parameter one is invalid'
    };
  }

  return result;
}


Not only do you get rid of the duplication, but now you have fewer functions; all your code for validating parameterOne is in contained in one spot. Also, now you can write your validateTypeRequest functions like so:

Validation.prototype.validateFirstTypeRequest= (data) =&gt; {
  let result;

  if (!result) {
    result = validateParameterOne(data.parameterOne);
  }

  if (!result) {
    result = validateParameterTwo(data.parameterTwo);
  }

  // If you HAVE to return and mutate data
  //if (result) {
  //  return Object.assign(data, result);
  //}

  return result;
}