validator-extras.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. const _ = require('lodash');
  3. const validator = _.cloneDeep(require('validator'));
  4. const moment = require('moment');
  5. const extensions = {
  6. extend(name, fn) {
  7. this[name] = fn;
  8. return this;
  9. },
  10. notEmpty(str) {
  11. return !str.match(/^[\s\t\r\n]*$/);
  12. },
  13. len(str, min, max) {
  14. return this.isLength(str, min, max);
  15. },
  16. isUrl(str) {
  17. return this.isURL(str);
  18. },
  19. isIPv6(str) {
  20. return this.isIP(str, 6);
  21. },
  22. isIPv4(str) {
  23. return this.isIP(str, 4);
  24. },
  25. notIn(str, values) {
  26. return !this.isIn(str, values);
  27. },
  28. regex(str, pattern, modifiers) {
  29. str += '';
  30. if (Object.prototype.toString.call(pattern).slice(8, -1) !== 'RegExp') {
  31. pattern = new RegExp(pattern, modifiers);
  32. }
  33. return str.match(pattern);
  34. },
  35. notRegex(str, pattern, modifiers) {
  36. return !this.regex(str, pattern, modifiers);
  37. },
  38. isDecimal(str) {
  39. return str !== '' && !!str.match(/^(?:-?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][+-]?(?:[0-9]+))?$/);
  40. },
  41. min(str, val) {
  42. const number = parseFloat(str);
  43. return isNaN(number) || number >= val;
  44. },
  45. max(str, val) {
  46. const number = parseFloat(str);
  47. return isNaN(number) || number <= val;
  48. },
  49. not(str, pattern, modifiers) {
  50. return this.notRegex(str, pattern, modifiers);
  51. },
  52. contains(str, elem) {
  53. return !!elem && str.includes(elem);
  54. },
  55. notContains(str, elem) {
  56. return !this.contains(str, elem);
  57. },
  58. is(str, pattern, modifiers) {
  59. return this.regex(str, pattern, modifiers);
  60. }
  61. };
  62. exports.extensions = extensions;
  63. // instance based validators
  64. validator.isImmutable = function(value, validatorArgs, field, modelInstance) {
  65. return modelInstance.isNewRecord || modelInstance.dataValues[field] === modelInstance._previousDataValues[field];
  66. };
  67. // extra validators
  68. validator.notNull = function(val) {
  69. return val !== null && val !== undefined;
  70. };
  71. // https://github.com/chriso/validator.js/blob/6.2.0/validator.js
  72. _.forEach(extensions, (extend, key) => {
  73. validator[key] = extend;
  74. });
  75. // map isNull to isEmpty
  76. // https://github.com/chriso/validator.js/commit/e33d38a26ee2f9666b319adb67c7fc0d3dea7125
  77. validator.isNull = validator.isEmpty;
  78. // isDate removed in 7.0.0
  79. // https://github.com/chriso/validator.js/commit/095509fc707a4dc0e99f85131df1176ad6389fc9
  80. validator.isDate = function(dateString) {
  81. // avoid http://momentjs.com/guides/#/warnings/js-date/
  82. // by doing a preliminary check on `dateString`
  83. const parsed = Date.parse(dateString);
  84. if (isNaN(parsed)) {
  85. // fail if we can't parse it
  86. return false;
  87. }
  88. // otherwise convert to ISO 8601 as moment prefers
  89. // http://momentjs.com/docs/#/parsing/string/
  90. const date = new Date(parsed);
  91. return moment(date.toISOString()).isValid();
  92. };
  93. exports.validator = validator;