string.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _rule = require('../rule/');
  6. var _rule2 = _interopRequireDefault(_rule);
  7. var _util = require('../util');
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  9. /**
  10. * Performs validation for string types.
  11. *
  12. * @param rule The validation rule.
  13. * @param value The value of the field on the source object.
  14. * @param callback The callback function.
  15. * @param source The source object being validated.
  16. * @param options The validation options.
  17. * @param options.messages The validation messages.
  18. */
  19. function string(rule, value, callback, source, options) {
  20. var errors = [];
  21. var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
  22. if (validate) {
  23. if ((0, _util.isEmptyValue)(value, 'string') && !rule.required) {
  24. return callback();
  25. }
  26. _rule2['default'].required(rule, value, source, errors, options, 'string');
  27. if (!(0, _util.isEmptyValue)(value, 'string')) {
  28. _rule2['default'].type(rule, value, source, errors, options);
  29. _rule2['default'].range(rule, value, source, errors, options);
  30. _rule2['default'].pattern(rule, value, source, errors, options);
  31. if (rule.whitespace === true) {
  32. _rule2['default'].whitespace(rule, value, source, errors, options);
  33. }
  34. }
  35. }
  36. callback(errors);
  37. }
  38. exports['default'] = string;
  39. module.exports = exports['default'];