Param.js 12 KB

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