string.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import rules from '../rule/index.js';
  2. import { isEmptyValue } from '../util';
  3. /**
  4. * Performs validation for string types.
  5. *
  6. * @param rule The validation rule.
  7. * @param value The value of the field on the source object.
  8. * @param callback The callback function.
  9. * @param source The source object being validated.
  10. * @param options The validation options.
  11. * @param options.messages The validation messages.
  12. */
  13. function string(rule, value, callback, source, options) {
  14. const errors = [];
  15. const validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  16. if (validate) {
  17. if (isEmptyValue(value, 'string') && !rule.required) {
  18. return callback();
  19. }
  20. rules.required(rule, value, source, errors, options, 'string');
  21. if (!isEmptyValue(value, 'string')) {
  22. rules.type(rule, value, source, errors, options);
  23. rules.range(rule, value, source, errors, options);
  24. rules.pattern(rule, value, source, errors, options);
  25. if (rule.whitespace === true) {
  26. rules.whitespace(rule, value, source, errors, options);
  27. }
  28. }
  29. }
  30. callback(errors);
  31. }
  32. export default string;