string.js 801 B

12345678910111213141516171819202122
  1. import rules from "../rule";
  2. import { isEmptyValue } from "../util";
  3. var string = function string(rule, value, callback, source, options) {
  4. var errors = [];
  5. var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
  6. if (validate) {
  7. if (isEmptyValue(value, 'string') && !rule.required) {
  8. return callback();
  9. }
  10. rules.required(rule, value, source, errors, options, 'string');
  11. if (!isEmptyValue(value, 'string')) {
  12. rules.type(rule, value, source, errors, options);
  13. rules.range(rule, value, source, errors, options);
  14. rules.pattern(rule, value, source, errors, options);
  15. if (rule.whitespace === true) {
  16. rules.whitespace(rule, value, source, errors, options);
  17. }
  18. }
  19. }
  20. callback(errors);
  21. };
  22. export default string;