Validator.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // https://github.com/chriso/validator.js
  2. import validator from 'validator';
  3. /**
  4. * Helper methods to validate form inputs
  5. * using controlled components
  6. */
  7. const FormValidator = {
  8. /**
  9. * Validate input element
  10. * @param element Dome element of the input
  11. * Uses the following attributes
  12. * data-validate: array in json format with validation methods
  13. * data-param: used to provide arguments for certain methods.
  14. */
  15. validate(element) {
  16. const isCheckbox = element.type === 'checkbox';
  17. const value = isCheckbox ? element.checked : element.value;
  18. const name = element.name;
  19. if (!name) throw new Error('Input name must not be empty.');
  20. // use getAttribute to support IE10+
  21. const param = element.getAttribute('data-param');
  22. const validations = JSON.parse(element.getAttribute('data-validate'));
  23. let result = []
  24. if(validations && validations.length) {
  25. /* Result of each validation must be true if the input is invalid
  26. and false if valid. */
  27. validations.forEach(m => {
  28. switch (m) {
  29. case 'required':
  30. result[m] = isCheckbox ? value === false : validator.isEmpty(value)
  31. break;
  32. case 'email':
  33. result[m] = !validator.isEmail(value)
  34. break;
  35. case 'number':
  36. result[m] = !validator.isNumeric(value)
  37. break;
  38. case 'integer':
  39. result[m] = !validator.isInt(value)
  40. break;
  41. case 'alphanum':
  42. result[m] = !validator.isAlphanumeric(value)
  43. break;
  44. case 'url':
  45. result[m] = !validator.isURL(value)
  46. break;
  47. case 'equalto':
  48. // here we expect a valid ID as param
  49. const value2 = document.getElementById(param).value;
  50. result[m] = !validator.equals(value, value2)
  51. break;
  52. case 'minlen':
  53. result[m] = !validator.isLength(value, { min: param })
  54. break;
  55. case 'maxlen':
  56. result[m] = !validator.isLength(value, { max: param })
  57. break;
  58. case 'len':
  59. const [min, max] = JSON.parse(param)
  60. result[m] = !validator.isLength(value, { min, max })
  61. break;
  62. case 'min':
  63. result[m] = !validator.isInt(value, { min: validator.toInt(param) })
  64. break;
  65. case 'max':
  66. result[m] = !validator.isInt(value, { max: validator.toInt(param) })
  67. break;
  68. case 'list':
  69. const list = JSON.parse(param)
  70. result[m] = !validator.isIn(value, list)
  71. break;
  72. default:
  73. throw new Error('Unrecognized validator.');
  74. }
  75. })
  76. }
  77. return result;
  78. },
  79. /**
  80. * Bulk validation of input elements.
  81. * Used with form elements collection.
  82. * @param {Array} inputs Array for DOM element
  83. * @return {Object} Contains array of error and a flag to
  84. * indicate if there was a validation error
  85. */
  86. bulkValidate(inputs) {
  87. let errors = {},
  88. hasError = false;
  89. inputs.forEach(input => {
  90. let result = this.validate(input)
  91. errors = { ...errors, [input.name]: result }
  92. if (!hasError) hasError = Object.keys(result).some(val => result[val])
  93. })
  94. return {
  95. errors,
  96. hasError
  97. }
  98. }
  99. }
  100. export default FormValidator;