pattern.js 846 B

1234567891011121314151617181920
  1. import { format } from "../util";
  2. var pattern = function pattern(rule, value, source, errors, options) {
  3. if (rule.pattern) {
  4. if (rule.pattern instanceof RegExp) {
  5. // if a RegExp instance is passed, reset `lastIndex` in case its `global`
  6. // flag is accidentally set to `true`, which in a validation scenario
  7. // is not necessary and the result might be misleading
  8. rule.pattern.lastIndex = 0;
  9. if (!rule.pattern.test(value)) {
  10. errors.push(format(options.messages.pattern.mismatch, rule.fullField, value, rule.pattern));
  11. }
  12. } else if (typeof rule.pattern === 'string') {
  13. var _pattern = new RegExp(rule.pattern);
  14. if (!_pattern.test(value)) {
  15. errors.push(format(options.messages.pattern.mismatch, rule.fullField, value, rule.pattern));
  16. }
  17. }
  18. }
  19. };
  20. export default pattern;