pattern.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _rule = require('../rule/');
  4. var _rule2 = _interopRequireDefault(_rule);
  5. var _util = require('../util');
  6. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  7. /**
  8. * Validates a regular expression pattern.
  9. *
  10. * Performs validation when a rule only contains
  11. * a pattern property but is not declared as a string type.
  12. *
  13. * @param rule The validation rule.
  14. * @param value The value of the field on the source object.
  15. * @param callback The callback function.
  16. * @param source The source object being validated.
  17. * @param options The validation options.
  18. * @param options.messages The validation messages.
  19. */
  20. function pattern(rule, value, callback, source, options) {
  21. var errors = [];
  22. var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
  23. if (validate) {
  24. if ((0, _util.isEmptyValue)(value, 'string') && !rule.required) {
  25. return callback();
  26. }
  27. _rule2['default'].required(rule, value, source, errors, options);
  28. if (!(0, _util.isEmptyValue)(value, 'string')) {
  29. _rule2['default'].pattern(rule, value, source, errors, options);
  30. }
  31. }
  32. callback(errors);
  33. }
  34. exports['default'] = pattern;
  35. module.exports = exports['default'];