feat: 增加ip、端口等校验规则

pull/16/head
ssongliu 2022-08-18 18:51:27 +08:00 committed by ssongliu
parent e68a62c925
commit 977625395b
3 changed files with 48 additions and 5 deletions

View File

@ -1,18 +1,40 @@
import i18n from '@/lang'; import i18n from '@/lang';
import { FormItemRule } from 'element-plus'; import { FormItemRule } from 'element-plus';
const checkIp = (rule: any, value: any, callback: any) => {
if (value === '' || typeof value === 'undefined' || value == null) {
callback(new Error(i18n.global.t('commons.rule.requiredInput')));
} else {
const reg =
/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
if (!reg.test(value) && value !== '') {
callback(new Error(i18n.global.t('commons.rule.ip')));
} else {
callback();
}
}
};
interface CommonRule { interface CommonRule {
required: FormItemRule; requiredInput: FormItemRule;
requiredSelect: FormItemRule;
name: FormItemRule; name: FormItemRule;
email: FormItemRule; email: FormItemRule;
ip: FormItemRule;
port: FormItemRule;
} }
export const Rules: CommonRule = { export const Rules: CommonRule = {
required: { requiredInput: {
required: true, required: true,
message: i18n.global.t('commons.rule.required'), message: i18n.global.t('commons.rule.requiredInput'),
trigger: 'blur', trigger: 'blur',
}, },
requiredSelect: {
required: true,
message: i18n.global.t('commons.rule.requiredSelect'),
trigger: 'change',
},
name: { name: {
type: 'regexp', type: 'regexp',
min: 1, min: 1,
@ -26,4 +48,17 @@ export const Rules: CommonRule = {
message: i18n.global.t('commons.rule.email'), message: i18n.global.t('commons.rule.email'),
trigger: 'blur', trigger: 'blur',
}, },
ip: {
validator: checkIp,
required: true,
trigger: 'blur',
},
port: {
required: true,
trigger: 'blur',
min: 1,
max: 65535,
type: 'number',
message: i18n.global.t('commons.rule.port'),
},
}; };

View File

@ -35,9 +35,12 @@ export default {
rule: { rule: {
username: 'Please enter a username', username: 'Please enter a username',
password: 'Please enter a password', password: 'Please enter a password',
required: 'Please enter the required fields', requiredInput: 'Please enter the required fields',
requiredSelect: 'Please select the required fields',
commonName: 'Support English, Chinese, numbers, .-_, length 1-30', commonName: 'Support English, Chinese, numbers, .-_, length 1-30',
email: 'Email format error', email: 'Email format error',
ip: 'Please enter the correct IP address',
port: 'Please enter the correct port',
}, },
res: { res: {
paramError: 'The request failed, please try again later!', paramError: 'The request failed, please try again later!',
@ -87,6 +90,7 @@ export default {
operations: { operations: {
detail: { detail: {
users: 'User', users: 'User',
hosts: 'Host',
auth: 'User', auth: 'User',
login: ' login', login: ' login',
logout: ' logout', logout: ' logout',

View File

@ -35,9 +35,12 @@ export default {
rule: { rule: {
username: '', username: '',
password: '', password: '',
required: '', requiredInput: '',
requiredSelect: '',
commonName: '.-_,1-30', commonName: '.-_,1-30',
email: '', email: '',
ip: ' IP ',
port: '',
}, },
res: { res: {
paramError: ',!', paramError: ',!',
@ -88,6 +91,7 @@ export default {
operations: { operations: {
detail: { detail: {
users: '', users: '',
hosts: '',
auth: '', auth: '',
post: '', post: '',
put: '', put: '',