zgg-ui
Version:
2.0.1 正式版本 基于 Vue 3、UniApp 和 TypeScript 开发的多端适配移动端 UI 组件库,旨在提供丰富的组件集合,帮助开发者快速构建多端响应式的移动应用界面
39 lines (36 loc) • 1.07 kB
text/typescript
// @ts-nocheck
import type { ExecuteRule } from '../interface'
import { format } from '../util'
const pattern: ExecuteRule = (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(
format(
options.messages.pattern.mismatch,
rule.fullField,
value,
rule.pattern
)
)
}
} else if (typeof rule.pattern === 'string') {
const _pattern = new RegExp(rule.pattern)
if (!_pattern.test(value)) {
errors.push(
format(
options.messages.pattern.mismatch,
rule.fullField,
value,
rule.pattern
)
)
}
}
}
}
export default pattern