Guest User

Untitled

a guest
Jan 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. // NOTE: how to use
  2. // how to get a validator function that checks if a string has a minimum length of 4
  3. // let minimumLength = 4;
  4. // let hasMinimumLengthOfFour = hashmapOfValidatorFunctionsToBeCurried['min_length'](minimumLength);
  5. // hasMinimumLengthOfFour('un') -> return false
  6. // hasMinimumLengthOfFour('quatre') -> return true
  7.  
  8. // NOTE2: the keys has to be in sync with what the private api's delivers
  9. // in the validation_rules key
  10. const hashmapOfValidatorFunctionsToBeCurried = Object.freeze({
  11. min_length: (limit) => (string = '') => string.length >= limit,
  12. max_length: (limit) => (string = '') => string.length <= limit,
  13. required: (boolean) => (value) => boolean === false ? true : !!value,
  14. });
  15.  
  16.  
  17. /**
  18. * [_getValidatorFunctionForValidationRules this function is used to return a validator function that
  19. * will apply all the validation rules onto the desired value and return true
  20. * if all the validation rules are respected]
  21. * @param {[Object]} validationRules [object containing all the validation
  22. * rules and their limits (e.g. = {min_length: 10})]
  23. * @return {[Function]} [A validator function that accept the value to be checked
  24. * as its argument it will then run all of the validator functions onto the
  25. * desired value and returns true if all test have passed]
  26. */
  27.  
  28. function getValidatorFunctionForValidationRules(validationRules) {
  29. let validationRulesFunctions = {};
  30.  
  31. if (!validationRules) return null;
  32.  
  33. Object.keys(validationRules).map(rule => {
  34. let ruleValue = validationRules[rule];
  35. if (!hashmapOfValidatorFunctionsToBeCurried[rule]) return;
  36. let validationFunction = hashmapOfValidatorFunctionsToBeCurried[rule](ruleValue);
  37.  
  38. validationRulesFunctions[rule] = validationFunction;
  39. });
  40. let validationFunctionsCount = Object.keys(validationRulesFunctions).length;
  41.  
  42. if (validationFunctionsCount > 0) {
  43. return function(value) {
  44. let reducer = (is_valid, function_key) => {
  45. return is_valid && validationRulesFunctions[function_key](value);
  46. }
  47. // note: we set the start value at true in order to properly evaluate the
  48. // first rule. if we started with false and the first value was indeed valid then
  49. // it will return false for the first iteration which is bad and not true.
  50. // also note that the use of the && operator is mandatory since we want to verify
  51. // that **all rules** are respected.
  52. return Object.keys(validationRulesFunctions).reduce(reducer, true);
  53. };
  54. } else {
  55. return null;
  56. }
  57. }
  58.  
  59. export { getValidatorFunctionForValidationRules, hashmapOfValidatorFunctionsToBeCurried }
Add Comment
Please, Sign In to add comment