mm_check 1.4.8 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +220 -2
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,2 +1,220 @@
1
- # mm_check
2
- 这是超级美眉check参数验证模块,用于验证参数是否正确
1
+ # mm_check
2
+
3
+ 这是超级美眉check参数验证模块,用于验证参数是否正确。本模块提供了一个强大的参数验证类,可以对各种类型的参数进行验证,包括字符串、数字、布尔值、数组和对象等。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install mm_check
9
+ ```
10
+
11
+ ## 基本使用
12
+
13
+ ```javascript
14
+ const Check = require('mm_check');
15
+
16
+ // 创建验证实例
17
+ const check = new Check({
18
+ name: "username", // 参数名
19
+ title: "用户名", // 参数介绍名
20
+ type: "string", // 参数类型
21
+ string: {
22
+ notEmpty: true, // 非空验证
23
+ min: 3, // 最小长度
24
+ max: 20 // 最大长度
25
+ }
26
+ });
27
+
28
+ // 验证参数
29
+ const result = check.run("test_user");
30
+ console.log(result); // 如果验证通过返回null,否则返回错误信息
31
+ ```
32
+
33
+ ## 参数配置说明
34
+
35
+ ### 基础配置
36
+
37
+ - `name`: 参数名称
38
+ - `title`: 参数介绍名
39
+ - `type`: 参数类型,支持 string、number、bool、dateTime、object、array
40
+ - `splitter`: 分隔符号,用于查询时判断多个传参,默认为 "|"
41
+ - `default`: 默认值
42
+
43
+ ### 字符串验证配置 (string)
44
+
45
+ ```javascript
46
+ string: {
47
+ notEmpty: false, // 是否非空
48
+ min: 0, // 最小长度
49
+ max: 0, // 最大长度
50
+ range: [], // 长度范围,例:[6, 20]
51
+ regex: "", // 正则表达式
52
+ identical: "", // 验证与某个参数值是否相同
53
+ different: "", // 验证与某个参数值是否不同
54
+ extension: "", // 后缀名,多个用"|"分隔
55
+ format: "" // 格式验证:email、url、date、dateISO、number、digits、phone
56
+ }
57
+ ```
58
+
59
+ ### 数值验证配置 (number)
60
+
61
+ ```javascript
62
+ number: {
63
+ min: 0, // 最小值
64
+ max: 0, // 最大值
65
+ range: [] // 数值范围,例:[0, 100]
66
+ }
67
+ ```
68
+
69
+ ### 数组验证配置 (array)
70
+
71
+ ```javascript
72
+ array: {
73
+ min: 0, // 最小成员数
74
+ max: 0, // 最大成员数
75
+ range: [], // 成员数范围
76
+ type: "", // 成员类型:string、number、object
77
+ object: [] // 成员为对象时的验证规则
78
+ }
79
+ ```
80
+
81
+ ## 使用示例
82
+
83
+ ### 1. 字符串验证
84
+
85
+ ```javascript
86
+ const userCheck = new Check({
87
+ name: "username",
88
+ title: "用户名",
89
+ type: "string",
90
+ string: {
91
+ notEmpty: true,
92
+ min: 3,
93
+ max: 20,
94
+ regex: "^[a-zA-Z0-9_]+$" // 只允许字母、数字和下划线
95
+ }
96
+ });
97
+
98
+ console.log(userCheck.run("")); // 用户名(username) 不能为空
99
+ console.log(userCheck.run("ab")); // 用户名(username) 最少需要输入 3 个字符
100
+ console.log(userCheck.run("user@123")); // 用户名(username) 格式不正确
101
+ ```
102
+
103
+ ### 2. 数值验证
104
+
105
+ ```javascript
106
+ const ageCheck = new Check({
107
+ name: "age",
108
+ title: "年龄",
109
+ type: "number",
110
+ number: {
111
+ min: 0,
112
+ max: 120
113
+ }
114
+ });
115
+
116
+ console.log(ageCheck.run(150)); // 年龄(age) 请输入不大于 120 的数值
117
+ console.log(ageCheck.run(-1)); // 年龄(age) 请输入不小于 0 的数值
118
+ ```
119
+
120
+ ### 3. 数组验证
121
+
122
+ ```javascript
123
+ const tagsCheck = new Check({
124
+ name: "tags",
125
+ title: "标签",
126
+ type: "array",
127
+ array: {
128
+ min: 1,
129
+ max: 5,
130
+ type: "string"
131
+ }
132
+ });
133
+
134
+ console.log(tagsCheck.run([])); // 标签(tags) 数组的成员数不小于 1 个
135
+ console.log(tagsCheck.run([1, 2, 3])); // 数组成员数据类型不正确, 应为string型
136
+ ```
137
+
138
+ ### 4. 对象验证
139
+
140
+ ```javascript
141
+ const userInfoCheck = new Check({
142
+ name: "userInfo",
143
+ title: "用户信息",
144
+ type: "object",
145
+ object: [
146
+ {
147
+ name: "name",
148
+ title: "姓名",
149
+ type: "string",
150
+ string: {
151
+ notEmpty: true
152
+ }
153
+ },
154
+ {
155
+ name: "age",
156
+ title: "年龄",
157
+ type: "number",
158
+ number: {
159
+ min: 0,
160
+ max: 120
161
+ }
162
+ }
163
+ ]
164
+ });
165
+
166
+ console.log(userInfoCheck.run({
167
+ name: "",
168
+ age: 150
169
+ })); // 属性 -> 姓名(name) 不能为空
170
+ ```
171
+
172
+ ## 错误提示配置
173
+
174
+ 可以通过 `lang` 方法自定义错误提示信息:
175
+
176
+ ```javascript
177
+ check.lang({
178
+ notEmpty: "字段不能为空",
179
+ minLength: "长度不能小于 {0} 个字符",
180
+ maxLength: "长度不能超过 {0} 个字符",
181
+ // ... 其他提示信息
182
+ });
183
+ ```
184
+
185
+ ## API 参考
186
+
187
+ ### Check 类
188
+
189
+ #### 构造函数
190
+
191
+ - `constructor(param)`: 创建一个新的验证实例
192
+ - `param`: 验证配置对象
193
+
194
+ #### 实例方法
195
+
196
+ - `run(value, [config])`: 验证值是否正确
197
+ - `value`: 要验证的值
198
+ - `config`: 可选的验证配置,默认使用构造函数的配置
199
+ - 返回:验证通过返回null,否则返回错误信息
200
+
201
+ - `lang(obj)`: 获取或设置错误提示
202
+ - `obj`: 设置的提示对象,为空则获取当前提示
203
+ - 返回:错误提示集合
204
+
205
+ - `msg(key, v1, v2)`: 获取错误提示
206
+ - `key`: 语言包键
207
+ - `v1`: 替换的词1
208
+ - `v2`: 替换的词2
209
+ - 返回:错误提示语句
210
+
211
+ ## 注意事项
212
+
213
+ 1. 使用前需要先安装 `mm_expand` 模块,因为本模块依赖于它。
214
+ 2. 验证失败时返回的错误信息会自动包含参数名和标题。
215
+ 3. 支持链式验证,可以同时设置多个验证规则。
216
+ 4. 对于数组和对象的验证,支持递归验证其成员或属性。
217
+
218
+ ## 许可证
219
+
220
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_check",
3
- "version": "1.4.8",
3
+ "version": "1.5.0",
4
4
  "description": "这是超级美眉check参数验证模块,用于验证参数是否正确",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,6 +26,6 @@
26
26
  },
27
27
  "homepage": "https://github.com/qiuwenwu/mm_check#readme",
28
28
  "dependencies": {
29
- "mm_expand": "^1.7.1"
29
+ "mm_expand": "^1.8.3"
30
30
  }
31
31
  }