mm_validator 0.0.0-stage → 1.0.1

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 (52) hide show
  1. package/index.js +9 -0
  2. package/lib/tools/abac/index.js +80 -0
  3. package/lib/tools/basic/index.js +73 -0
  4. package/lib/tools/code/index.js +462 -0
  5. package/lib/tools/enum/index.js +22 -0
  6. package/lib/tools/index.js +16 -0
  7. package/lib/tools/json/index.js +181 -0
  8. package/lib/tools/jwt/index.js +109 -0
  9. package/lib/tools/length/index.js +61 -0
  10. package/lib/tools/oauth/index.js +170 -0
  11. package/lib/tools/params/index.js +99 -0
  12. package/lib/tools/pattern/index.js +58 -0
  13. package/lib/tools/range/index.js +112 -0
  14. package/lib/tools/rbac/index.js +92 -0
  15. package/lib/tools/required/index.js +37 -0
  16. package/lib/tools/sanitize/index.js +124 -0
  17. package/lib/tools/string/index.js +75 -0
  18. package/lib/tools/type/index.js +21 -0
  19. package/package.json +36 -5
  20. package/tool/abac/index.js +7 -0
  21. package/tool/abac/tool.json +76 -0
  22. package/tool/basic/index.js +7 -0
  23. package/tool/basic/tool.json +69 -0
  24. package/tool/code/index.js +7 -0
  25. package/tool/code/tool.json +69 -0
  26. package/tool/enum/index.js +7 -0
  27. package/tool/enum/tool.json +51 -0
  28. package/tool/json/index.js +7 -0
  29. package/tool/json/tool.json +65 -0
  30. package/tool/jwt/index.js +7 -0
  31. package/tool/jwt/tool.json +91 -0
  32. package/tool/length/index.js +7 -0
  33. package/tool/length/tool.json +53 -0
  34. package/tool/oauth/index.js +7 -0
  35. package/tool/oauth/tool.json +86 -0
  36. package/tool/params/index.js +7 -0
  37. package/tool/params/tool.json +92 -0
  38. package/tool/pattern/index.js +7 -0
  39. package/tool/pattern/tool.json +52 -0
  40. package/tool/range/index.js +7 -0
  41. package/tool/range/tool.json +79 -0
  42. package/tool/rbac/index.js +7 -0
  43. package/tool/rbac/tool.json +74 -0
  44. package/tool/required/index.js +7 -0
  45. package/tool/required/tool.json +46 -0
  46. package/tool/sanitize/index.js +7 -0
  47. package/tool/sanitize/tool.json +83 -0
  48. package/tool/string/index.js +7 -0
  49. package/tool/string/tool.json +62 -0
  50. package/tool/type/index.js +7 -0
  51. package/tool/type/tool.json +54 -0
  52. package/README.md +0 -3
@@ -0,0 +1,112 @@
1
+ /**
2
+ * 范围验证器
3
+ * 验证数值是否在指定范围内,支持多种比较模式:
4
+ * between / not_between / > / >= / < / <= / = / != / in / not_in
5
+ * 支持单规则和多规则(rules 数组)两种模式
6
+ */
7
+ async function main(params, ctx, options) {
8
+ var errors = [];
9
+ var rule_list;
10
+
11
+ if (params.rules && Array.isArray(params.rules)) {
12
+ rule_list = params.rules;
13
+ } else {
14
+ rule_list = [params];
15
+ }
16
+
17
+ for (var i = 0; i < rule_list.length; i++) {
18
+ var rule = rule_list[i];
19
+ _validateRule(rule, errors);
20
+ }
21
+
22
+ var valid = errors.length === 0;
23
+ var tip = valid ? '参数校验通过' : errors.length + '个参数校验失败: ' + errors.join('; ');
24
+ ctx.output = { valid: valid, tip: tip, errors: errors };
25
+ return ctx.output;
26
+ }
27
+
28
+ function _validateRule(rule, errors) {
29
+ var value = rule.value;
30
+ var min = rule.min;
31
+ var max = rule.max;
32
+ var mode = rule.mode || 'between';
33
+ var inclusive_min = rule.inclusive_min !== false;
34
+ var inclusive_max = rule.inclusive_max !== false;
35
+ var label = rule.field || rule.label || '值';
36
+
37
+ switch (mode) {
38
+ case 'between':
39
+ if (min !== undefined && value !== undefined) {
40
+ if (inclusive_min ? value < min : value <= min) {
41
+ errors.push(label + ' 不能小于' + (inclusive_min ? '' : '等于') + ' ' + min);
42
+ }
43
+ }
44
+ if (max !== undefined && value !== undefined) {
45
+ if (inclusive_max ? value > max : value >= max) {
46
+ errors.push(label + ' 不能大于' + (inclusive_max ? '' : '等于') + ' ' + max);
47
+ }
48
+ }
49
+ break;
50
+ case 'not_between':
51
+ if (min !== undefined && max !== undefined && value !== undefined) {
52
+ if (value >= min && value <= max) {
53
+ errors.push(label + ' 不能在 ' + min + ' ~ ' + max + ' 之间');
54
+ }
55
+ }
56
+ break;
57
+ case '>':
58
+ if (min !== undefined && value !== undefined && value <= min) {
59
+ errors.push(label + ' 必须大于 ' + min);
60
+ }
61
+ break;
62
+ case '>=':
63
+ if (min !== undefined && value !== undefined && value < min) {
64
+ errors.push(label + ' 必须大于等于 ' + min);
65
+ }
66
+ break;
67
+ case '<':
68
+ if (max !== undefined && value !== undefined && value >= max) {
69
+ errors.push(label + ' 必须小于 ' + max);
70
+ }
71
+ break;
72
+ case '<=':
73
+ if (max !== undefined && value !== undefined && value > max) {
74
+ errors.push(label + ' 必须小于等于 ' + max);
75
+ }
76
+ break;
77
+ case '=':
78
+ if (min !== undefined && value !== undefined && value !== min) {
79
+ errors.push(label + ' 必须等于 ' + min);
80
+ }
81
+ break;
82
+ case '!=':
83
+ if (min !== undefined && value !== undefined && value === min) {
84
+ errors.push(label + ' 不能等于 ' + min);
85
+ }
86
+ break;
87
+ case 'in':
88
+ if (rule.values !== undefined && Array.isArray(rule.values)) {
89
+ if (rule.values.indexOf(value) === -1) {
90
+ errors.push(label + ' 必须在 [' + rule.values.join(', ') + '] 之中');
91
+ }
92
+ }
93
+ break;
94
+ case 'not_in':
95
+ if (rule.values !== undefined && Array.isArray(rule.values)) {
96
+ if (rule.values.indexOf(value) !== -1) {
97
+ errors.push(label + ' 不能在 [' + rule.values.join(', ') + '] 之中');
98
+ }
99
+ }
100
+ break;
101
+ default:
102
+ if (min !== undefined && value !== undefined && value < min) {
103
+ errors.push(label + ' 不能小于 ' + min);
104
+ }
105
+ if (max !== undefined && value !== undefined && value > max) {
106
+ errors.push(label + ' 不能大于 ' + max);
107
+ }
108
+ break;
109
+ }
110
+ }
111
+
112
+ module.exports = { main };
@@ -0,0 +1,92 @@
1
+ /**
2
+ * RBAC权限验证器
3
+ * 基于角色的访问控制:检查用户角色是否拥有指定权限
4
+ * 角色-权限映射配置在 this.config.options.role_permissions 中
5
+ */
6
+
7
+ async function main(params, ctx, options) {
8
+ var action = params.action || 'check';
9
+ var user_id = params.user_id || '';
10
+ var role = params.role || '';
11
+ var permission = params.permission || '';
12
+
13
+ // 默认角色-权限映射(可从 options 覆盖)
14
+ var opts = (this && typeof this.getOptions === 'function') ? this.getOptions() : {};
15
+ var role_permissions = opts.role_permissions || {
16
+ admin: ['*'],
17
+ user: ['read', 'write'],
18
+ guest: ['read']
19
+ };
20
+
21
+ switch (action) {
22
+ case 'check': {
23
+ if (!role || !permission) {
24
+ ctx.output = {
25
+ valid: false,
26
+ tip: '1个参数校验失败: 缺少角色或权限参数',
27
+ errors: ['缺少角色或权限参数'],
28
+ roles: {}
29
+ };
30
+ return ctx.output;
31
+ }
32
+
33
+ var perms = role_permissions[role] || [];
34
+ var allowed = perms.indexOf('*') !== -1 || perms.indexOf(permission) !== -1;
35
+
36
+ var tip = allowed
37
+ ? '参数校验通过'
38
+ : '1个参数校验失败: 角色 ' + role + ' 没有 ' + permission + ' 权限';
39
+ ctx.output = {
40
+ valid: allowed,
41
+ tip: tip,
42
+ errors: allowed ? [] : ['角色 ' + role + ' 没有 ' + permission + ' 权限'],
43
+ roles: { role: role, permission: permission, allowed: allowed }
44
+ };
45
+ return ctx.output;
46
+ }
47
+ case 'add_role': {
48
+ if (!role) {
49
+ ctx.output = {
50
+ valid: false,
51
+ tip: '1个参数校验失败: 缺少角色名',
52
+ errors: ['缺少角色名'],
53
+ roles: {}
54
+ };
55
+ return ctx.output;
56
+ }
57
+ if (!role_permissions[role]) {
58
+ role_permissions[role] = [];
59
+ }
60
+ if (params.permissions) {
61
+ role_permissions[role] = role_permissions[role].concat(params.permissions);
62
+ }
63
+ ctx.output = {
64
+ valid: true,
65
+ tip: '参数校验通过',
66
+ errors: [],
67
+ roles: { role: role, permissions: role_permissions[role] }
68
+ };
69
+ return ctx.output;
70
+ }
71
+ case 'list_roles': {
72
+ ctx.output = {
73
+ valid: true,
74
+ tip: '参数校验通过',
75
+ errors: [],
76
+ roles: { roles: Object.keys(role_permissions), mapping: role_permissions }
77
+ };
78
+ return ctx.output;
79
+ }
80
+ default: {
81
+ ctx.output = {
82
+ valid: false,
83
+ tip: '1个参数校验失败: 不支持的操作类型: ' + action,
84
+ errors: ['不支持的操作类型: ' + action],
85
+ roles: {}
86
+ };
87
+ return ctx.output;
88
+ }
89
+ }
90
+ }
91
+
92
+ module.exports = { main };
@@ -0,0 +1,37 @@
1
+ /**
2
+ * 必填验证器
3
+ * 验证字段值不为 null/undefined/空字符串
4
+ * 支持单字段 (field+value) 和多字段 (fields 对象) 两种模式
5
+ */
6
+
7
+ async function main(params, ctx, options) {
8
+ var errors = [];
9
+ var required_fields = [];
10
+
11
+ // 多字段模式: { fields: { username: '...', password: '...' } }
12
+ if (params.fields && typeof params.fields === 'object') {
13
+ var field_names = Object.keys(params.fields);
14
+ required_fields = field_names;
15
+ for (var i = 0; i < field_names.length; i++) {
16
+ var name = field_names[i];
17
+ var val = params.fields[name];
18
+ if (val === null || val === undefined || val === '') {
19
+ errors.push(name + ' 为必填项');
20
+ }
21
+ }
22
+ } else {
23
+ // 单字段模式(向后兼容): { field: '...', value: '...' }
24
+ required_fields = [params.field];
25
+ var value = params.value;
26
+ if (value === null || value === undefined || value === '') {
27
+ errors.push(params.field + ' 为必填项');
28
+ }
29
+ }
30
+
31
+ var valid = errors.length === 0;
32
+ var tip = valid ? '参数校验通过' : errors.length + '个参数校验失败: ' + errors.join('; ');
33
+ ctx.output = { valid: valid, tip: tip, errors: errors, required_fields: required_fields };
34
+ return ctx.output;
35
+ }
36
+
37
+ module.exports = { main };
@@ -0,0 +1,124 @@
1
+ /**
2
+ * 输入消毒器
3
+ * 对用户输入进行安全清洗,检测 SQL 注入、脚本注入和路径遍历攻击
4
+ */
5
+ const SQL_PATTERN =
6
+ /(\b(SELECT|INSERT|UPDATE|DELETE|DROP|ALTER|TRUNCATE|UNION|EXEC|EXECUTE)\b|--|;)/i;
7
+ const SCRIPT_PATTERN = /(<script|eval\s*\(|Function\s*\(|setTimeout\s*\(|setInterval\s*\()/i;
8
+ const PATH_PATTERN = /\.\.\/|\.\.\\/;
9
+
10
+ /**
11
+ * 消毒用户输入
12
+ * @param {object} ctx 上下文对象
13
+ * @param {object} params 步骤参数
14
+ * - input {*} 待清洗的输入值
15
+ * - max_depth {number} 递归最大深度
16
+ * - max_string_len {number} 字符串最大长度
17
+ * - max_keys {number} 对象最大键数
18
+ * @returns {object} { msg, valid, cleaned, threats }
19
+ */
20
+ async function main(params, ctx, options) {
21
+ var input = params.input;
22
+ if (input === undefined || input === null) {
23
+ ctx.output = { msg: '消毒完成', valid: true, cleaned: input, threats: [] };
24
+ return ctx.output;
25
+ }
26
+
27
+ var opts = _getOptions.call(this, params);
28
+ var threats = [];
29
+
30
+ var cleaned = _sanitize(input, opts, threats);
31
+ ctx.output = {
32
+ msg: '消毒完成',
33
+ valid: threats.length === 0,
34
+ cleaned: cleaned,
35
+ threats: threats
36
+ };
37
+ return ctx.output;
38
+ }
39
+
40
+ /**
41
+ * 合并 options 和 params 获取配置
42
+ * @param {object} params 步骤参数
43
+ * @returns {object} 合并后的配置
44
+ */
45
+ function _getOptions(params) {
46
+ var cfg = (this && typeof this.getOptions === 'function') ? this.getOptions() : {};
47
+ return {
48
+ max_depth: params.max_depth || cfg.max_depth || 10,
49
+ max_string_len: params.max_string_len || cfg.max_string_len || 1048576,
50
+ max_keys: params.max_keys || cfg.max_keys || 1000
51
+ };
52
+ }
53
+
54
+ /**
55
+ * 递归清洗值
56
+ * @param {*} val 待清洗值
57
+ * @param {object} opts 配置项
58
+ * @param {Array} threats 威胁收集数组
59
+ * @param {number} depth 当前深度
60
+ * @returns {*} 清洗后的值
61
+ */
62
+ function _sanitize(val, opts, threats, depth) {
63
+ if (depth === undefined) depth = 0;
64
+ if (depth > opts.max_depth) return null;
65
+
66
+ if (val === null || val === undefined) return val;
67
+ if (typeof val === 'number' || typeof val === 'boolean') return val;
68
+
69
+ if (typeof val === 'string') {
70
+ return _sanitizeStr(val, opts, threats);
71
+ }
72
+
73
+ if (Array.isArray(val)) {
74
+ var arr = [];
75
+ for (var i = 0; i < val.length; i++) {
76
+ arr.push(_sanitize(val[i], opts, threats, depth + 1));
77
+ }
78
+ return arr;
79
+ }
80
+
81
+ if (typeof val === 'object') {
82
+ var result = {};
83
+ var keys = Object.keys(val);
84
+ if (keys.length > opts.max_keys) {
85
+ keys = keys.slice(0, opts.max_keys);
86
+ }
87
+ for (var k = 0; k < keys.length; k++) {
88
+ var key = keys[k];
89
+ result[key] = _sanitize(val[key], opts, threats, depth + 1);
90
+ }
91
+ return result;
92
+ }
93
+
94
+ return val;
95
+ }
96
+
97
+ /**
98
+ * 清洗字符串
99
+ * @param {string} str 输入字符串
100
+ * @param {object} opts 配置项
101
+ * @param {Array} threats 威胁收集数组
102
+ * @returns {string} 清洗后的字符串
103
+ */
104
+ function _sanitizeStr(str, opts, threats) {
105
+ if (typeof str !== 'string') return str;
106
+
107
+ if (str.length > opts.max_string_len) {
108
+ str = str.slice(0, opts.max_string_len);
109
+ }
110
+
111
+ if (SQL_PATTERN.test(str)) {
112
+ threats.push('sql_injection');
113
+ }
114
+ if (SCRIPT_PATTERN.test(str)) {
115
+ threats.push('script_injection');
116
+ }
117
+ if (PATH_PATTERN.test(str)) {
118
+ threats.push('path_traversal');
119
+ }
120
+
121
+ return str;
122
+ }
123
+
124
+ module.exports = { main };
@@ -0,0 +1,75 @@
1
+ /**
2
+ * 字符串验证器
3
+ * 验证字符串是否满足指定条件:
4
+ * contains / starts_with / ends_with
5
+ * 支持单规则和多规则(rules 数组)两种模式
6
+ */
7
+ async function main(params, ctx, options) {
8
+ var errors = [];
9
+ var rule_list;
10
+
11
+ if (params.rules && Array.isArray(params.rules)) {
12
+ rule_list = params.rules;
13
+ } else {
14
+ rule_list = [params];
15
+ }
16
+
17
+ for (var i = 0; i < rule_list.length; i++) {
18
+ var rule = rule_list[i];
19
+ _validateRule(rule, errors);
20
+ }
21
+
22
+ var valid = errors.length === 0;
23
+ var tip = valid ? '参数校验通过' : errors.length + '个参数校验失败: ' + errors.join('; ');
24
+ ctx.output = { valid: valid, tip: tip, errors: errors };
25
+ return ctx.output;
26
+ }
27
+
28
+ function _validateRule(rule, errors) {
29
+ var value = rule.value;
30
+ var pattern = rule.pattern;
31
+ var mode = rule.mode || 'contains';
32
+ var case_sensitive = rule.case_sensitive !== false;
33
+ var label = rule.field || rule.label || '值';
34
+
35
+ if (value === undefined || value === null) {
36
+ return;
37
+ }
38
+
39
+ var str = String(value);
40
+ var pat = String(pattern || '');
41
+
42
+ if (!case_sensitive) {
43
+ str = str.toLowerCase();
44
+ pat = pat.toLowerCase();
45
+ }
46
+
47
+ switch (mode) {
48
+ case 'contains':
49
+ if (pat && str.indexOf(pat) === -1) {
50
+ errors.push(label + ' 必须包含' + (case_sensitive ? '' : '(忽略大小写)') + ' ' + pattern);
51
+ }
52
+ break;
53
+ case 'starts_with':
54
+ if (pat && !str.startsWith(pat)) {
55
+ errors.push(
56
+ label + ' 必须以' + (case_sensitive ? '' : '(忽略大小写)') + ' ' + pattern + ' 开头'
57
+ );
58
+ }
59
+ break;
60
+ case 'ends_with':
61
+ if (pat && !str.endsWith(pat)) {
62
+ errors.push(
63
+ label + ' 必须以' + (case_sensitive ? '' : '(忽略大小写)') + ' ' + pattern + ' 结尾'
64
+ );
65
+ }
66
+ break;
67
+ default:
68
+ if (pat && str.indexOf(pat) === -1) {
69
+ errors.push(label + ' 必须包含 ' + pattern);
70
+ }
71
+ break;
72
+ }
73
+ }
74
+
75
+ module.exports = { main };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * 类型验证器
3
+ * 验证字段值是否为指定类型
4
+ */
5
+
6
+ async function main(params, ctx, options) {
7
+ var errors = [];
8
+ var value = params.value;
9
+ var expected_type = params.expected_type || 'string';
10
+ var actual_type = Array.isArray(value) ? 'array' : typeof value;
11
+
12
+ if (actual_type !== expected_type) {
13
+ errors.push(params.field + ' 类型应为 ' + expected_type + ',实际为 ' + actual_type);
14
+ }
15
+ var valid = errors.length === 0;
16
+ var tip = valid ? '参数校验通过' : errors.length + '个参数校验失败: ' + errors.join('; ');
17
+ ctx.output = { valid: valid, tip: tip, errors: errors, expected_type: expected_type };
18
+ return ctx.output;
19
+ }
20
+
21
+ module.exports = { main };
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
1
  {
2
- "name": "mm_validator",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "mm_validator",
3
+ "version": "1.0.1",
4
+ "description": "mm_workflow 验证器工具包:ABAC、基础、验证码、枚举、JSON、JWT、长度、OAuth、参数、模式、范围、RBAC、必填、清洗、字符串、类型 等 16 个验证工具模块",
5
+ "type": "commonjs",
6
+ "main": "index.js",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://gitee.com/qiuwenwu91/mm_validator.git"
10
+ },
11
+ "keywords": [
12
+ "mm_mod",
13
+ "operator",
14
+ "validator",
15
+ "超级美眉",
16
+ "工作流工具"
17
+ ],
18
+ "layer": "shared",
19
+ "group": "operator",
20
+ "operator": "validator",
21
+ "author": "邱文武",
22
+ "license": "ISC",
23
+ "files": [
24
+ "index.js",
25
+ "lib",
26
+ "tool"
27
+ ],
28
+ "engines": {
29
+ "node": ">=16"
30
+ },
31
+ "scripts": {
32
+ "test": "node -e \"const t=require('./index.js').loadTools();console.log(Object.keys(t).length+' tools loaded')\""
33
+ },
34
+ "dependencies": {
35
+ "mm_tool_host": "^1.0.0"
36
+ }
37
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * abac —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/abac/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_validator').tools.abac 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/abac');
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "abac",
3
+ "handler": "validator",
4
+ "title": "ABAC权限验证器",
5
+ "description": "基于属性的访问控制验证,依据主体、资源、环境属性评估策略是否放行。适用场景:多条件组合的细粒度权限校验、动态授权决策时。工作流提示:action 选 evaluate 评估,subject/resource/environment 传属性、operation 传操作名,结果看 valid 与 policies。",
6
+ "type": "core",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "action": {
11
+ "type": "string",
12
+ "description": "操作类型: evaluate/add_policy/remove_policy",
13
+ "enum": [
14
+ "evaluate",
15
+ "add_policy",
16
+ "remove_policy"
17
+ ]
18
+ },
19
+ "subject": {
20
+ "type": "object",
21
+ "description": "主体属性(用户属性)"
22
+ },
23
+ "resource": {
24
+ "type": "object",
25
+ "description": "资源属性"
26
+ },
27
+ "environment": {
28
+ "type": "object",
29
+ "description": "环境属性"
30
+ },
31
+ "operation": {
32
+ "type": "string",
33
+ "description": "操作名称"
34
+ }
35
+ },
36
+ "required": [
37
+ "action"
38
+ ]
39
+ },
40
+ "returns": {
41
+ "type": "object",
42
+ "properties": {
43
+ "valid": {
44
+ "type": "boolean",
45
+ "description": "验证是否通过"
46
+ },
47
+ "tip": {
48
+ "type": "string",
49
+ "description": "提示信息,通过时为'参数校验通过',失败时包含错误数量和详情"
50
+ },
51
+ "errors": {
52
+ "type": "array",
53
+ "description": "错误信息列表"
54
+ },
55
+ "policies": {
56
+ "description": "evaluate 时返回决策列表,add_policy 时返回全部策略列表"
57
+ },
58
+ "error": {
59
+ "type": "object",
60
+ "description": "错误信息,仅调用出错时返回",
61
+ "properties": {
62
+ "code": {
63
+ "type": "string",
64
+ "description": "错误码,如 VAL_ABAC"
65
+ },
66
+ "message": {
67
+ "type": "string",
68
+ "description": "错误描述"
69
+ }
70
+ }
71
+ }
72
+ }
73
+ },
74
+ "options": {},
75
+ "group": "permission"
76
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * basic —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/basic/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_validator').tools.basic 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/basic');
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "basic",
3
+ "handler": "validator",
4
+ "title": "Basic验证器",
5
+ "description": "验证 HTTP Basic 认证,支持编码、解码与校验用户名密码。适用场景:接口登录鉴权、生成 Basic 头或反向解析时。工作流提示:action 选 encode/verify 时填 username、password,选 decode/verify 时填 encoded,结果看 valid 与 errors。",
6
+ "type": "core",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "action": {
11
+ "type": "string",
12
+ "description": "操作类型: encode/decode/verify",
13
+ "enum": [
14
+ "encode",
15
+ "decode",
16
+ "verify"
17
+ ]
18
+ },
19
+ "username": {
20
+ "type": "string",
21
+ "description": "用户名(encode/verify时必填)"
22
+ },
23
+ "password": {
24
+ "type": "string",
25
+ "description": "密码(encode/verify时必填)"
26
+ },
27
+ "encoded": {
28
+ "type": "string",
29
+ "description": "Base64编码的认证字符串(decode/verify时必填)"
30
+ }
31
+ },
32
+ "required": [
33
+ "action"
34
+ ]
35
+ },
36
+ "returns": {
37
+ "type": "object",
38
+ "properties": {
39
+ "valid": {
40
+ "type": "boolean",
41
+ "description": "验证是否通过"
42
+ },
43
+ "tip": {
44
+ "type": "string",
45
+ "description": "提示信息,通过时为'参数校验通过',失败时包含错误数量和详情"
46
+ },
47
+ "errors": {
48
+ "type": "array",
49
+ "description": "错误信息列表"
50
+ },
51
+ "error": {
52
+ "type": "object",
53
+ "description": "错误信息,仅调用出错时返回",
54
+ "properties": {
55
+ "code": {
56
+ "type": "string",
57
+ "description": "错误码,如 VAL_BASIC"
58
+ },
59
+ "message": {
60
+ "type": "string",
61
+ "description": "错误描述"
62
+ }
63
+ }
64
+ }
65
+ }
66
+ },
67
+ "options": {},
68
+ "group": "auth"
69
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * code —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/code/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_validator').tools.code 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/code');