pattern.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. import rules from '../rule/index.js';
  2. import { isEmptyValue } from '../util';
  3. /**
  4. * Validates a regular expression pattern.
  5. *
  6. * Performs validation when a rule only contains
  7. * a pattern property but is not declared as a string type.
  8. *
  9. * @param rule The validation rule.
  10. * @param value The value of the field on the source object.
  11. * @param callback The callback function.
  12. * @param source The source object being validated.
  13. * @param options The validation options.
  14. * @param options.messages The validation messages.
  15. */
  16. function pattern(rule, value, callback, source, options) {
  17. const errors = [];
  18. const validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  19. if (validate) {
  20. if (isEmptyValue(value, 'string') && !rule.required) {
  21. return callback();
  22. }
  23. rules.required(rule, value, source, errors, options);
  24. if (!isEmptyValue(value, 'string')) {
  25. rules.pattern(rule, value, source, errors, options);
  26. }
  27. }
  28. callback(errors);
  29. }
  30. export default pattern;