Param.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. "use strict";
  2. var util = require('util');
  3. var validator = require('validator');
  4. var AppErrors = require('./AppErrors.js');
  5. var OujRedis = require('./OujRedis.js');
  6. var configs = require('./../../conf/config.inc.js');
  7. var _ = require('underscore');
  8. class Param {
  9. constructor(objResponse) {
  10. this.objResponse = objResponse;
  11. }
  12. /**
  13. * 检查参数
  14. * @param rules 检查规则
  15. * {
  16. * 'appId' : 'int', //int类型
  17. * 'owners' : 'array', //array类型
  18. * 'instanceIds' : 'intArr', //array类型,元素为int类型
  19. * 'instanceTypes' : 'strArr', //array类型,元素为string类型
  20. * 'deviceId' : 'int/array', //int类型或者array类型,最后转化为元素为idArr类型
  21. * 'deviceClass' : 'string/array', //string类型或者array类型,最后转化为strArr类型
  22. * 'blocks' : {type : 'int', range : '(5, 10)'} //int类型, > 5 , < 10
  23. * 'blocks2' : {type : 'int', range : '[5, 10]'} //int类型, >= 5 , <= 10
  24. * 'percent' : {type : 'float', range : '[5.1, 10.9]'} //float类型,>= 5.1 , <= 10.9
  25. * 'appName' : {type : 'string'} //string类型
  26. * 'appName2' : {type : 'string', reg : '[^0-9A-Za-z]', 'len' : '[1, 10]', 'nullable' : true} //string类型,支持正则
  27. * }
  28. * @param args 参数合集
  29. * @param exitError 遇到错误是否直接exit
  30. * @static
  31. * @return boolean 是否检查通过
  32. */
  33. checkParam(rules, args, exitError) {
  34. exitError = exitError === false ? exitError : true;
  35. //_private.getRule(rules, args);
  36. for (var i in rules) {
  37. var result = _private.checkRule(rules[i], args, i);
  38. if (!result['result']) {
  39. if (exitError) {
  40. this.objResponse.error(CODE_PARAM_ERROR, null, result['msg']);
  41. // 仅仅是中断后续运行
  42. throw new AppErrors.Interrupt(result['msg']);
  43. } else {
  44. return result;
  45. }
  46. }
  47. }
  48. return _private.succ;
  49. }
  50. checkParam2(rules, args, exitError) {
  51. var flag = this.checkParam(rules, args, exitError);
  52. for (var i in args) {
  53. if (!_.has(rules, i)) {
  54. delete (args[i]);
  55. }
  56. }
  57. return flag;
  58. }
  59. }
  60. var _private = {
  61. succ: {
  62. result: true
  63. },
  64. makeRules: function (rules, key) {
  65. },
  66. getRule: function (rules, args) {
  67. if (args['__getRules']) {
  68. var params = args['__params'];
  69. params['rules'] = rules;
  70. for (let i in rules) {
  71. var value = params['params'][i];
  72. if (Array.isArray(rules[i])) {
  73. value['type'] = rules[i]['type'] || rules[i][0];
  74. value['rule'] = _private.genDoc(rules[i]);
  75. } else if (value) {
  76. value['type'] = rules[i];
  77. }
  78. params['params'][i] = value;
  79. }
  80. var config = configs.redisInfo.logstash_redis;
  81. var objRedis = OujRedis.init('logstash_redis');
  82. //if (config && objRedis) {
  83. // Response.exitMsg(objRedis);
  84. //}
  85. }
  86. },
  87. genDoc: function (rules) {
  88. var str = '';
  89. if (rules['nullable']) {
  90. str += '【可为null】';
  91. }
  92. if (rules['emptyable']) {
  93. str += '【可为空值】';
  94. }
  95. if (rules['len']) {
  96. str += "【长度范围:{$rule['len']}】";
  97. }
  98. if (rules['range']) {
  99. str += "【取值范围:{$rule['range']}】";
  100. }
  101. if (rules['reg']) {
  102. str += "【正则:{$rule['reg']}】";
  103. }
  104. if (rules['enum']) {
  105. str += '【值枚举:' + JSON.stringify(rules['enum']) + '】';
  106. }
  107. return str;
  108. },
  109. checkRule: function (rules, args, key) {
  110. var type = rules['type'] || rules,
  111. that = _private,
  112. result;
  113. if (type == "int/array") {
  114. result = that.checkIntArr(rules, args, key);
  115. } else if (type == "string/array") {
  116. result = that.checkStrArr(rules, args, key);
  117. } else if (type) {
  118. var funcName = "check" + type.charAt(0).toUpperCase() + type.slice(1);
  119. result = that[funcName].apply(that, [rules, args, key]);
  120. } else {
  121. return that.error(key + ': type must not be empty!');
  122. }
  123. return result;
  124. },
  125. error: function (msg) {
  126. return {result: false, msg: msg};
  127. },
  128. checkBase: function (rules, args, key) {
  129. var value = args[key],
  130. that = _private;
  131. //判断是否可为空
  132. if (rules['nullable'] && value == null) {
  133. return _.extend({
  134. nullable: true
  135. }, that.succ);
  136. }
  137. //判断是否可为0或空字符串
  138. if ((rules['nullable'] || rules['emptyable']) && !value && value != null) {
  139. return _.extend({
  140. emptyable: true
  141. }, that.succ);
  142. }
  143. //判断是否在enum中
  144. if (rules['enum'] && rules['enum'].indexOf(value) == -1) {
  145. return that.error(key + ':' + args[key] + ' is not in ' + rules['enum']);
  146. }
  147. //判断是否为空
  148. if (!value) {
  149. return that.error(key + ' is null or empty!');
  150. }
  151. return that.succ;
  152. },
  153. checkRange: function (rules, args, key) {
  154. var range = rules['range'],
  155. that = _private;
  156. if (range) {
  157. range = range.trim();
  158. var ranges = range.split(',');
  159. var errMsg = key + ' is not in range ' + range;
  160. var from = parseFloat(ranges[0].substring(1).trim());
  161. if (from !== '-' && from !== '~') {
  162. var flag = ranges[0].charAt(0);
  163. if (flag === '[' && args[key] < from) {
  164. return that.error(errMsg);
  165. } else if (flag === '(' && args[key] <= from) {
  166. return that.error(errMsg);
  167. }
  168. }
  169. var to = parseFloat(ranges[1].slice(0, -1));
  170. if (to != '+' && to != '~') {
  171. var flag = ranges[1].slice(-1);
  172. if (flag === ']' && args[key] > to) {
  173. return that.error(errMsg);
  174. } else if (flag === ')' && args[key] >= to) {
  175. return that.error(errMsg);
  176. }
  177. }
  178. }
  179. return that.succ;
  180. },
  181. checkLen: function (rules, args, key) {
  182. var len = rules['len'],
  183. that = _private;
  184. if (len) {
  185. len = len.trim();
  186. var ranges = len.split(',');
  187. var errMsg = key + ' is not valid. len must in' + len;
  188. var from = parseFloat(ranges[0].substring(1).trim());
  189. var strLength = args[key].length;
  190. if (from != '-' && from != '~') {
  191. var flag = ranges[0].charAt(0);
  192. if (flag === '[' && strLength < from) {
  193. return that.error(errMsg);
  194. } else if (flag === '(' && strLength <= from) {
  195. return that.error(errMsg);
  196. }
  197. }
  198. var to = parseFloat(ranges[1].slice(0, -1));
  199. if (to != '+' && to != '~') {
  200. var flag = ranges[1].slice(-1);
  201. if (flag === ']' && strLength > to) {
  202. return that.error(errMsg);
  203. } else if (flag === ')' && strLength >= to) {
  204. return that.error(errMsg);
  205. }
  206. }
  207. }
  208. return that.succ;
  209. },
  210. checkDefault: function (rules, args, key) {
  211. var that = _private;
  212. if (rules['emptyable']) {
  213. rules['emptyable'] = true;
  214. }
  215. var result = that.checkBase(rules, args, key);
  216. if (!result['result'] || result['nullable']) {
  217. return result;
  218. }
  219. result = that.checkRange(rules, args, key);
  220. if (!result['result']) {
  221. return result;
  222. }
  223. return that.succ;
  224. },
  225. checkInt: function (rules, args, key) {
  226. var that = _private;
  227. if (rules['emptyable']) {
  228. rules['emptyable'] = true;
  229. }
  230. var result = that.checkBase(rules, args, key);
  231. if (!result['result'] || result['nullable']) {
  232. return result;
  233. }
  234. if (!validator.isInt(args[key])) {
  235. return that.error(key + ': ' + args[key] + ' is not int!');
  236. }
  237. result = that.checkRange(rules, args, key);
  238. if (!result['result']) {
  239. return result;
  240. }
  241. return that.succ;
  242. },
  243. checkIp: function (rules, args, key) {
  244. var that = _private;
  245. var result = that.checkBase(rules, args, key);
  246. if (!result['result'] || result['nullable']) {
  247. return result;
  248. }
  249. if (!validator.isIP(args[key], 4)) {
  250. return that.error(key + ': ' + args[key] + ' is not valid ip format!');
  251. }
  252. return that.succ;
  253. },
  254. checkFloat: function (rules, args, key) {
  255. var that = _private;
  256. var result = that.checkBase(rules, args, key);
  257. if (!result['result'] || result['nullable']) {
  258. return result;
  259. }
  260. if (!validator.isFloat(args[key])) {
  261. return that.error(key + ': ' + args[key] + ' is not float!');
  262. }
  263. result = that.checkRange(rules, args, key);
  264. if (!result['result']) {
  265. return result;
  266. }
  267. return that.succ;
  268. },
  269. checkString: function (rules, args, key) {
  270. var that = _private;
  271. if (args[key] !== null && args[key]) {
  272. args[key] = args[key].toString().trim();
  273. }
  274. var result = that.checkBase(rules, args, key);
  275. if (!result['result'] || result['nullable']) {
  276. return result;
  277. }
  278. result = that.checkLen(rules, args, key);
  279. if (!result['result']) {
  280. return result;
  281. }
  282. if (rules['reg']) {
  283. var reg = new RegExp(rules['reg']);
  284. if (reg.test(args[key])) {
  285. return that.error(key + ' preg_match error! The reg rule is:' + rules['reg']);
  286. }
  287. }
  288. return that.succ;
  289. },
  290. checkArray: function (rules, args, key) {
  291. var that = _private;
  292. var result = that.checkBase(rules, args, key);
  293. if (!result['result'] || result['nullable']) {
  294. return result;
  295. }
  296. if (!Array.isArray(args[key])) {
  297. return that.error(key + ' is not array');
  298. }
  299. return that.succ;
  300. },
  301. checkJson: function (rules, args, key) {
  302. var that = _private;
  303. var result = that.checkBase(rules, args, key);
  304. if (!result['result'] || result['nullable']) {
  305. return result;
  306. }
  307. if (!validator.isJSON(args[key])) {
  308. return that.error(key + ' is not JSON');
  309. }
  310. return that.succ;
  311. },
  312. checkObject: function (rules, args, key) {
  313. var that = _private;
  314. var result = that.checkBase(rules, args, key);
  315. if (!result['result'] || result['nullable']) {
  316. return result;
  317. }
  318. if (!rules['items']) {
  319. return that.succ;
  320. }
  321. for (var i in rules['items']) {
  322. result = that.checkRule(rules['items'][i], args[key], i);
  323. if (!result['result']) {
  324. result['msg'] = {
  325. parent: key
  326. }
  327. return result;
  328. }
  329. }
  330. return that.succ;
  331. },
  332. checkIntArr: function (rules, args, key) {
  333. var that = _private;
  334. return that._checkRuleArr(rules, args, key, 'int');
  335. },
  336. checkStrArr: function (rules, args, key) {
  337. var that = _private;
  338. return that._checkRuleArr(rules, args, key, 'string');
  339. },
  340. checkIpArr: function (rules, args, key) {
  341. var that = _private;
  342. return that._checkRuleArr(rules, args, key, 'ip');
  343. },
  344. _checkRuleArr: function (rules, args, key, type) {
  345. var that = _private;
  346. var result = that.checkArray(rules, args, key);
  347. if (!result['result'] || result['nullable']) {
  348. return result;
  349. }
  350. for (var i in args[key]) {
  351. result = that.checkRule(type, args[key], i);
  352. if (!result['result']) {
  353. result['msg'] = {
  354. parent: key
  355. }
  356. return result;
  357. }
  358. }
  359. return that.succ;
  360. }
  361. };
  362. module.exports = Param;