jsgrid.validation.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. (function(jsGrid, $, undefined) {
  2. function Validation(config) {
  3. this._init(config);
  4. }
  5. Validation.prototype = {
  6. _init: function(config) {
  7. $.extend(true, this, config);
  8. },
  9. validate: function(args) {
  10. var errors = [];
  11. $.each(this._normalizeRules(args.rules), function(_, rule) {
  12. if(rule.validator(args.value, args.item, rule.param))
  13. return;
  14. var errorMessage = $.isFunction(rule.message) ? rule.message(args.value, args.item) : rule.message;
  15. errors.push(errorMessage);
  16. });
  17. return errors;
  18. },
  19. _normalizeRules: function(rules) {
  20. if(!$.isArray(rules))
  21. rules = [rules];
  22. return $.map(rules, $.proxy(function(rule) {
  23. return this._normalizeRule(rule);
  24. }, this));
  25. },
  26. _normalizeRule: function(rule) {
  27. if(typeof rule === "string")
  28. rule = { validator: rule };
  29. if($.isFunction(rule))
  30. rule = { validator: rule };
  31. if($.isPlainObject(rule))
  32. rule = $.extend({}, rule);
  33. else
  34. throw Error("wrong validation config specified");
  35. if($.isFunction(rule.validator))
  36. return rule;
  37. return this._applyNamedValidator(rule, rule.validator);
  38. },
  39. _applyNamedValidator: function(rule, validatorName) {
  40. delete rule.validator;
  41. var validator = validators[validatorName];
  42. if(!validator)
  43. throw Error("unknown validator \"" + validatorName + "\"");
  44. if($.isFunction(validator)) {
  45. validator = { validator: validator };
  46. }
  47. return $.extend({}, validator, rule);
  48. }
  49. };
  50. jsGrid.Validation = Validation;
  51. var validators = {
  52. required: {
  53. message: "Field is required",
  54. validator: function(value) {
  55. return value !== undefined && value !== null && value !== "";
  56. }
  57. },
  58. rangeLength: {
  59. message: "Field value length is out of the defined range",
  60. validator: function(value, _, param) {
  61. return value.length >= param[0] && value.length <= param[1];
  62. }
  63. },
  64. minLength: {
  65. message: "Field value is too long",
  66. validator: function(value, _, param) {
  67. return value.length >= param;
  68. }
  69. },
  70. maxLength: {
  71. message: "Field value is too short",
  72. validator: function(value, _, param) {
  73. return value.length <= param;
  74. }
  75. },
  76. pattern: {
  77. message: "Field value is not matching the defined pattern",
  78. validator: function(value, _, param) {
  79. if(typeof param === "string") {
  80. param = new RegExp("^(?:" + param + ")$");
  81. }
  82. return param.test(value);
  83. }
  84. },
  85. range: {
  86. message: "Field value is out of the defined range",
  87. validator: function(value, _, param) {
  88. return value >= param[0] && value <= param[1];
  89. }
  90. },
  91. min: {
  92. message: "Field value is too large",
  93. validator: function(value, _, param) {
  94. return value >= param;
  95. }
  96. },
  97. max: {
  98. message: "Field value is too small",
  99. validator: function(value, _, param) {
  100. return value <= param;
  101. }
  102. }
  103. };
  104. jsGrid.validators = validators;
  105. }(jsGrid, jQuery));