Skip to content

Commit

Permalink
refactor: 完善前后端校验
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Sep 18, 2023
1 parent 3fd0c08 commit 90d825a
Show file tree
Hide file tree
Showing 23 changed files with 117 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@
public class RegexConsts implements RegexPool {

/**
* 用户名正则(长度为 4 到 16 位,可以包含字母、数字,下划线,以字母开头)
* 用户名正则(长度为 4 到 64 位,可以包含字母、数字,下划线,以字母开头)
*/
public static final String USERNAME = "^[a-zA-Z][a-zA-Z0-9_]{3,15}$";
public static final String USERNAME = "^[a-zA-Z][a-zA-Z0-9_]{3,64}$";

/**
* 密码正则(长度为 6 到 32 位,可以包含字母、数字、下划线,特殊字符,同时包含字母和数字)
*/
public static final String PASSWORD = "^(?=.*\\d)(?=.*[a-z]).{6,32}$";

/**
* 通用编码正则(长度为 2 到 16 位,可以包含字母、数字,下划线,以字母开头)
* 通用编码正则(长度为 2 到 30 位,可以包含字母、数字,下划线,以字母开头)
*/
public static final String GENERAL_CODE = "^[a-zA-Z][a-zA-Z0-9_]{1,15}$";
public static final String GENERAL_CODE = "^[a-zA-Z][a-zA-Z0-9_]{1,29}$";

/**
* 通用名称正则(长度为 120 位,可以包含中文、字母、数字、下划线,短横线)
* 通用名称正则(长度为 230 位,可以包含中文、字母、数字、下划线,短横线)
*/
public static final String GENERAL_NAME = "^[\\u4e00-\\u9fa5a-zA-Z0-9_-]{1,20}$";
public static final String GENERAL_NAME = "^[\\u4e00-\\u9fa5a-zA-Z0-9_-]{2,30}$";

/**
* 包名正则(可以包含大小写字母、数字、下划线,每一级包名不能以数字开头)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class AnnouncementRequest extends BaseRequest {
*/
@Schema(description = "标题", example = "这是公告标题")
@NotBlank(message = "标题不能为空")
@Length(max = 255, message = "标题长度不能超过 {max} 个字符")
@Length(max = 150, message = "标题长度不能超过 {max} 个字符")
private String title;

/**
Expand All @@ -60,6 +60,7 @@ public class AnnouncementRequest extends BaseRequest {
*/
@Schema(description = "类型(取值于字典 announcement_type)", example = "1")
@NotBlank(message = "类型不能为空")
@Length(max = 30, message = "类型长度不能超过 {max} 个字符")
private String type;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package top.charles7c.cnadmin.system.model.request;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
Expand Down Expand Up @@ -55,14 +56,14 @@ public class DeptRequest extends BaseRequest {
*/
@Schema(description = "部门名称", example = "测试部")
@NotBlank(message = "部门名称不能为空")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "部门名称长度为 120 位,可以包含中文、字母、数字、下划线,短横线")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "部门名称长度为 230 位,可以包含中文、字母、数字、下划线,短横线")
private String name;

/**
* 部门排序
*/
@Schema(description = "部门排序", example = "1")
@NotNull(message = "部门排序不能为空")
@Min(value = 1, message = "部门排序最小值为 {value}")
private Integer sort;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,29 @@ public class DictItemRequest extends BaseRequest {
*/
@Schema(description = "字典标签", example = "通知")
@NotBlank(message = "字典标签不能为空")
@Length(max = 30, message = "字典标签长度不能超过 {max} 个字符")
private String label;

/**
* 字典值
*/
@Schema(description = "字典值", example = "1")
@NotBlank(message = "字典值不能为空")
@Length(max = 30, message = "字典值长度不能超过 {max} 个字符")
private String value;

/**
* 背景颜色
*/
@Schema(description = "背景颜色", example = "blue")
@Length(max = 30, message = "背景颜色长度不能超过 {max} 个字符")
private String color;

/**
* 排序
*/
@Schema(description = "排序", example = "1")
@Min(value = 1, message = "排序最小值为 {value}")
private Integer sort;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.hibernate.validator.constraints.Length;

import top.charles7c.cnadmin.common.base.BaseRequest;
import top.charles7c.cnadmin.common.constant.RegexConsts;

/**
* 创建或修改字典信息
Expand All @@ -43,13 +44,15 @@ public class DictRequest extends BaseRequest {
*/
@Schema(description = "字典名称", example = "公告类型")
@NotBlank(message = "字典名称不能为空")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "字典名称长度为 2 到 30 位,可以包含中文、字母、数字、下划线,短横线")
private String name;

/**
* 字典编码
*/
@Schema(description = "字典编码", example = "announcement_type")
@NotBlank(message = "字典编码不能为空")
@Pattern(regexp = RegexConsts.GENERAL_CODE, message = "字典编码长度为 2 到 30 位,可以包含字母、数字,下划线,以字母开头")
private String code;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package top.charles7c.cnadmin.system.model.request;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
Expand All @@ -24,6 +25,8 @@

import io.swagger.v3.oas.annotations.media.Schema;

import org.hibernate.validator.constraints.Length;

import top.charles7c.cnadmin.common.base.BaseRequest;
import top.charles7c.cnadmin.common.constant.RegexConsts;
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
Expand Down Expand Up @@ -52,45 +55,51 @@ public class MenuRequest extends BaseRequest {
* 菜单图标
*/
@Schema(description = "菜单图标", example = "user")
@Length(max = 50, message = "菜单图标长度不能超过 {max} 个字符")
private String icon;

/**
* 菜单标题
*/
@Schema(description = "菜单标题", example = "用户管理")
@NotBlank(message = "菜单标题不能为空")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "菜单标题长度为 120 位,可以包含中文、字母、数字、下划线,短横线")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "菜单标题长度为 230 位,可以包含中文、字母、数字、下划线,短横线")
private String title;

/**
* 菜单排序
*/
@Schema(description = "菜单排序", example = "1")
@NotNull(message = "菜单排序不能为空")
@Min(value = 1, message = "菜单排序最小值为 {value}")
private Integer sort;

/**
* 权限标识
*/
@Schema(description = "权限标识", example = "system:user:list")
@Length(max = 100, message = "权限标识长度不能超过 {max} 个字符")
private String permission;

/**
* 路由地址
*/
@Schema(description = "路由地址", example = "/system/user")
@Length(max = 255, message = "路由地址长度不能超过 {max} 个字符")
private String path;

/**
* 组件名称
*/
@Schema(description = "组件名称", example = "User")
@Length(max = 50, message = "组件名称长度不能超过 {max} 个字符")
private String name;

/**
* 组件路径
*/
@Schema(description = "组件路径", example = "/system/user/index")
@Length(max = 255, message = "组件路径长度不能超过 {max} 个字符")
private String component;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import lombok.Data;
Expand Down Expand Up @@ -51,22 +51,22 @@ public class RoleRequest extends BaseRequest {
*/
@Schema(description = "角色名称", example = "测试人员")
@NotBlank(message = "角色名称不能为空")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "角色名称长度为 120 位,可以包含中文、字母、数字、下划线,短横线")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "角色名称长度为 230 位,可以包含中文、字母、数字、下划线,短横线")
private String name;

/**
* 角色编码
*/
@Schema(description = "角色编码", example = "test")
@NotBlank(message = "角色编码不能为空")
@Pattern(regexp = RegexConsts.GENERAL_CODE, message = "角色编码长度为 2 到 16 位,可以包含字母、数字,下划线,以字母开头")
@Pattern(regexp = RegexConsts.GENERAL_CODE, message = "角色编码长度为 2 到 30 位,可以包含字母、数字,下划线,以字母开头")
private String code;

/**
* 角色排序
*/
@Schema(description = "角色排序", example = "1")
@NotNull(message = "角色排序不能为空")
@Min(value = 1, message = "角色排序最小值为 {value}")
private Integer sort;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class UpdateBasicInfoRequest implements Serializable {
*/
@Schema(description = "昵称", example = "张三")
@NotBlank(message = "昵称不能为空")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "昵称长度为 120 位,可以包含中文、字母、数字、下划线,短横线")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "昵称长度为 230 位,可以包含中文、字母、数字、下划线,短横线")
private String nickname;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,23 @@ public class UserRequest extends BaseRequest {
*/
@Schema(description = "用户名", example = "zhangsan")
@NotBlank(message = "用户名不能为空")
@Pattern(regexp = RegexConsts.USERNAME, message = "用户名长度为 4 到 16 位,可以包含字母、数字,下划线,以字母开头")
@Pattern(regexp = RegexConsts.USERNAME, message = "用户名长度为 4 到 64 位,可以包含字母、数字,下划线,以字母开头")
private String username;

/**
* 昵称
*/
@Schema(description = "昵称", example = "张三")
@NotBlank(message = "昵称不能为空")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "昵称长度为 120 位,可以包含中文、字母、数字、下划线,短横线")
@Pattern(regexp = "^[\\u4e00-\\u9fa5a-zA-Z0-9_-]{4,30}$", message = "昵称长度为 430 位,可以包含中文、字母、数字、下划线,短横线")
private String nickname;

/**
* 邮箱
*/
@Schema(description = "邮箱", example = "[email protected]")
@Pattern(regexp = RegexConsts.EMAIL, message = "邮箱格式错误")
@Length(max = 255, message = "邮箱长度不能超过 {max} 个字符")
private String email;

/**
Expand Down
5 changes: 3 additions & 2 deletions continew-admin-ui/src/views/login/components/login-form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<a-input
v-model="form.username"
:placeholder="$t('login.form.placeholder.username')"
max-length="50"
:max-length="64"
>
<template #prefix><icon-user /></template>
</a-input>
Expand All @@ -24,7 +24,7 @@
<a-input-password
v-model="form.password"
:placeholder="$t('login.form.placeholder.password')"
max-length="32"
:max-length="32"
allow-clear
>
<template #prefix><icon-lock /></template>
Expand All @@ -34,6 +34,7 @@
<a-input
v-model="form.captcha"
:placeholder="$t('login.form.placeholder.captcha')"
:max-length="4"
allow-clear
style="width: 63%"
>
Expand Down
2 changes: 1 addition & 1 deletion continew-admin-ui/src/views/system/announcement/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
<a-input
v-model="form.title"
placeholder="请输入标题"
max-length="255"
:max-length="150"
style="width: 100%"
/>
</a-form-item>
Expand Down
10 changes: 8 additions & 2 deletions continew-admin-ui/src/views/system/dept/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,14 @@
// 表单验证规则
rules: {
parentId: [{ required: true, message: '请选择上级部门' }],
name: [{ required: true, message: '请输入部门名称' }],
sort: [{ required: true, message: '请输入部门排序' }],
name: [
{ required: true, message: '请输入部门名称' },
{
match: /^[\u4e00-\u9fa5a-zA-Z0-9_-]{2,30}$/,
message:
'长度为 2 到 30 位,可以包含中文、字母、数字、下划线,短横线',
},
],
},
});
const { queryParams, form, rules } = toRefs(data);
Expand Down
18 changes: 16 additions & 2 deletions continew-admin-ui/src/views/system/dict/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,21 @@
form: {} as DataRecord,
// 表单验证规则
rules: {
name: [{ required: true, message: '字典名称不能为空' }],
code: [{ required: true, message: '字典编码不能为空' }],
name: [
{ required: true, message: '请输入字典名称' },
{
match: /^[\\u4e00-\\u9fa5a-zA-Z0-9_-]{2,30}$/,
message:
'长度为 2 到 30 位,可以包含中文、字母、数字、下划线,短横线',
},
],
code: [
{ required: true, message: '请输入字典编码' },
{
match: /^[a-zA-Z][a-zA-Z0-9_]{1,29}$/,
message: '长度为 2 到 30 位,可以包含字母、数字,下划线,以字母开头',
},
],
},
});
const { queryParams, form, rules } = toRefs(data);
Expand All @@ -298,6 +311,7 @@
* @param params 查询参数
*/
const getList = (params: ListParam = { ...queryParams.value }) => {
dictId.value = null;
loading.value = true;
list(params)
.then((res) => {
Expand Down
17 changes: 13 additions & 4 deletions continew-admin-ui/src/views/system/dict/item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,25 @@
>
<a-form ref="formRef" :model="form" :rules="rules" size="large">
<a-form-item label="字典标签" field="label">
<a-input v-model="form.label" placeholder="请输入字典标签" />
<a-input
v-model="form.label"
placeholder="请输入字典标签"
:max-length="30"
/>
</a-form-item>
<a-form-item label="字典值" field="value">
<a-input v-model="form.value" placeholder="请输入字典值" />
<a-input
v-model="form.value"
placeholder="请输入字典值"
:max-length="30"
/>
</a-form-item>
<a-form-item label="背景颜色" field="color">
<a-auto-complete
v-model="form.color"
:data="colors"
placeholder="请选择或输入背景颜色"
:max-length="30"
allow-clear
>
<template #option="{ data }">
Expand Down Expand Up @@ -198,8 +207,8 @@
form: {} as DataRecord,
// 表单验证规则
rules: {
label: [{ required: true, message: '字典标签不能为空' }],
value: [{ required: true, message: '字典值不能为空' }],
label: [{ required: true, message: '请输入字典标签' }],
value: [{ required: true, message: '请输入字典值' }],
},
});
const { queryParams, form, rules } = toRefs(data);
Expand Down
Loading

0 comments on commit 90d825a

Please sign in to comment.