PromoCursed/node_modules/@rc-component/async-validator/lib/rule/pattern.js
2024-08-20 23:25:37 +04:00

26 lines
990 B
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _util = require("../util");
var pattern = function pattern(rule, value, source, errors, options) {
if (rule.pattern) {
if (rule.pattern instanceof RegExp) {
// if a RegExp instance is passed, reset `lastIndex` in case its `global`
// flag is accidentally set to `true`, which in a validation scenario
// is not necessary and the result might be misleading
rule.pattern.lastIndex = 0;
if (!rule.pattern.test(value)) {
errors.push((0, _util.format)(options.messages.pattern.mismatch, rule.fullField, value, rule.pattern));
}
} else if (typeof rule.pattern === 'string') {
var _pattern = new RegExp(rule.pattern);
if (!_pattern.test(value)) {
errors.push((0, _util.format)(options.messages.pattern.mismatch, rule.fullField, value, rule.pattern));
}
}
}
};
var _default = exports.default = pattern;