validate.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. var pattern = {
  2. email: /^\S+?@\S+?\.\S+?$/,
  3. idcard: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
  4. url: new RegExp(
  5. "^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$",
  6. 'i')
  7. };
  8. const FORMAT_MAPPING = {
  9. "int": 'integer',
  10. "bool": 'boolean',
  11. "double": 'number',
  12. "long": 'number',
  13. "password": 'string'
  14. // "fileurls": 'array'
  15. }
  16. function formatMessage(args, resources = '') {
  17. var defaultMessage = ['label']
  18. defaultMessage.forEach((item) => {
  19. if (args[item] === undefined) {
  20. args[item] = ''
  21. }
  22. })
  23. let str = resources
  24. for (let key in args) {
  25. let reg = new RegExp('{' + key + '}')
  26. str = str.replace(reg, args[key])
  27. }
  28. return str
  29. }
  30. function isEmptyValue(value, type) {
  31. if (value === undefined || value === null) {
  32. return true;
  33. }
  34. if (typeof value === 'string' && !value) {
  35. return true;
  36. }
  37. if (Array.isArray(value) && !value.length) {
  38. return true;
  39. }
  40. if (type === 'object' && !Object.keys(value).length) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. const types = {
  46. integer(value) {
  47. return types.number(value) && parseInt(value, 10) === value;
  48. },
  49. string(value) {
  50. return typeof value === 'string';
  51. },
  52. number(value) {
  53. if (isNaN(value)) {
  54. return false;
  55. }
  56. return typeof value === 'number';
  57. },
  58. "boolean": function(value) {
  59. return typeof value === 'boolean';
  60. },
  61. "float": function(value) {
  62. return types.number(value) && !types.integer(value);
  63. },
  64. array(value) {
  65. return Array.isArray(value);
  66. },
  67. object(value) {
  68. return typeof value === 'object' && !types.array(value);
  69. },
  70. date(value) {
  71. return value instanceof Date;
  72. },
  73. timestamp(value) {
  74. if (!this.integer(value) || Math.abs(value).toString().length > 16) {
  75. return false
  76. }
  77. return true;
  78. },
  79. file(value) {
  80. return typeof value.url === 'string';
  81. },
  82. email(value) {
  83. return typeof value === 'string' && !!value.match(pattern.email) && value.length < 255;
  84. },
  85. url(value) {
  86. return typeof value === 'string' && !!value.match(pattern.url);
  87. },
  88. pattern(reg, value) {
  89. try {
  90. return new RegExp(reg).test(value);
  91. } catch (e) {
  92. return false;
  93. }
  94. },
  95. method(value) {
  96. return typeof value === 'function';
  97. },
  98. idcard(value) {
  99. return typeof value === 'string' && !!value.match(pattern.idcard);
  100. },
  101. 'url-https'(value) {
  102. return this.url(value) && value.startsWith('https://');
  103. },
  104. 'url-scheme'(value) {
  105. return value.startsWith('://');
  106. },
  107. 'url-web'(value) {
  108. return false;
  109. }
  110. }
  111. class RuleValidator {
  112. constructor(message) {
  113. this._message = message
  114. }
  115. async validateRule(fieldKey, fieldValue, value, data, allData) {
  116. var result = null
  117. let rules = fieldValue.rules
  118. let hasRequired = rules.findIndex((item) => {
  119. return item.required
  120. })
  121. if (hasRequired < 0) {
  122. if (value === null || value === undefined) {
  123. return result
  124. }
  125. if (typeof value === 'string' && !value.length) {
  126. return result
  127. }
  128. }
  129. var message = this._message
  130. if (rules === undefined) {
  131. return message['default']
  132. }
  133. for (var i = 0; i < rules.length; i++) {
  134. let rule = rules[i]
  135. let vt = this._getValidateType(rule)
  136. Object.assign(rule, {
  137. label: fieldValue.label || `["${fieldKey}"]`
  138. })
  139. if (RuleValidatorHelper[vt]) {
  140. result = RuleValidatorHelper[vt](rule, value, message)
  141. if (result != null) {
  142. break
  143. }
  144. }
  145. if (rule.validateExpr) {
  146. let now = Date.now()
  147. let resultExpr = rule.validateExpr(value, allData, now)
  148. if (resultExpr === false) {
  149. result = this._getMessage(rule, rule.errorMessage || this._message['default'])
  150. break
  151. }
  152. }
  153. if (rule.validateFunction) {
  154. result = await this.validateFunction(rule, value, data, allData, vt)
  155. if (result !== null) {
  156. break
  157. }
  158. }
  159. }
  160. if (result !== null) {
  161. result = message.TAG + result
  162. }
  163. return result
  164. }
  165. async validateFunction(rule, value, data, allData, vt) {
  166. let result = null
  167. try {
  168. let callbackMessage = null
  169. const res = await rule.validateFunction(rule, value, allData || data, (message) => {
  170. callbackMessage = message
  171. })
  172. if (callbackMessage || (typeof res === 'string' && res) || res === false) {
  173. result = this._getMessage(rule, callbackMessage || res, vt)
  174. }
  175. } catch (e) {
  176. result = this._getMessage(rule, e.message, vt)
  177. }
  178. return result
  179. }
  180. _getMessage(rule, message, vt) {
  181. return formatMessage(rule, message || rule.errorMessage || this._message[vt] || message['default'])
  182. }
  183. _getValidateType(rule) {
  184. var result = ''
  185. if (rule.required) {
  186. result = 'required'
  187. } else if (rule.format) {
  188. result = 'format'
  189. } else if (rule.arrayType) {
  190. result = 'arrayTypeFormat'
  191. } else if (rule.range) {
  192. result = 'range'
  193. } else if (rule.maximum !== undefined || rule.minimum !== undefined) {
  194. result = 'rangeNumber'
  195. } else if (rule.maxLength !== undefined || rule.minLength !== undefined) {
  196. result = 'rangeLength'
  197. } else if (rule.pattern) {
  198. result = 'pattern'
  199. } else if (rule.validateFunction) {
  200. result = 'validateFunction'
  201. }
  202. return result
  203. }
  204. }
  205. const RuleValidatorHelper = {
  206. required(rule, value, message) {
  207. if (rule.required && isEmptyValue(value, rule.format || typeof value)) {
  208. return formatMessage(rule, rule.errorMessage || message.required);
  209. }
  210. return null
  211. },
  212. range(rule, value, message) {
  213. const {
  214. range,
  215. errorMessage
  216. } = rule;
  217. let list = new Array(range.length);
  218. for (let i = 0; i < range.length; i++) {
  219. const item = range[i];
  220. if (types.object(item) && item.value !== undefined) {
  221. list[i] = item.value;
  222. } else {
  223. list[i] = item;
  224. }
  225. }
  226. let result = false
  227. if (Array.isArray(value)) {
  228. result = (new Set(value.concat(list)).size === list.length);
  229. } else {
  230. if (list.indexOf(value) > -1) {
  231. result = true;
  232. }
  233. }
  234. if (!result) {
  235. return formatMessage(rule, errorMessage || message['enum']);
  236. }
  237. return null
  238. },
  239. rangeNumber(rule, value, message) {
  240. if (!types.number(value)) {
  241. return formatMessage(rule, rule.errorMessage || message.pattern.mismatch);
  242. }
  243. let {
  244. minimum,
  245. maximum,
  246. exclusiveMinimum,
  247. exclusiveMaximum
  248. } = rule;
  249. let min = exclusiveMinimum ? value <= minimum : value < minimum;
  250. let max = exclusiveMaximum ? value >= maximum : value > maximum;
  251. if (minimum !== undefined && min) {
  252. return formatMessage(rule, rule.errorMessage || message['number'][exclusiveMinimum ?
  253. 'exclusiveMinimum' : 'minimum'
  254. ])
  255. } else if (maximum !== undefined && max) {
  256. return formatMessage(rule, rule.errorMessage || message['number'][exclusiveMaximum ?
  257. 'exclusiveMaximum' : 'maximum'
  258. ])
  259. } else if (minimum !== undefined && maximum !== undefined && (min || max)) {
  260. return formatMessage(rule, rule.errorMessage || message['number'].range)
  261. }
  262. return null
  263. },
  264. rangeLength(rule, value, message) {
  265. if (!types.string(value) && !types.array(value)) {
  266. return formatMessage(rule, rule.errorMessage || message.pattern.mismatch);
  267. }
  268. let min = rule.minLength;
  269. let max = rule.maxLength;
  270. let val = value.length;
  271. if (min !== undefined && val < min) {
  272. return formatMessage(rule, rule.errorMessage || message['length'].minLength)
  273. } else if (max !== undefined && val > max) {
  274. return formatMessage(rule, rule.errorMessage || message['length'].maxLength)
  275. } else if (min !== undefined && max !== undefined && (val < min || val > max)) {
  276. return formatMessage(rule, rule.errorMessage || message['length'].range)
  277. }
  278. return null
  279. },
  280. pattern(rule, value, message) {
  281. if (!types['pattern'](rule.pattern, value)) {
  282. return formatMessage(rule, rule.errorMessage || message.pattern.mismatch);
  283. }
  284. return null
  285. },
  286. format(rule, value, message) {
  287. var customTypes = Object.keys(types);
  288. var format = FORMAT_MAPPING[rule.format] ? FORMAT_MAPPING[rule.format] : (rule.format || rule.arrayType);
  289. if (customTypes.indexOf(format) > -1) {
  290. if (!types[format](value)) {
  291. return formatMessage(rule, rule.errorMessage || message.typeError);
  292. }
  293. }
  294. return null
  295. },
  296. arrayTypeFormat(rule, value, message) {
  297. if (!Array.isArray(value)) {
  298. return formatMessage(rule, rule.errorMessage || message.typeError);
  299. }
  300. for (let i = 0; i < value.length; i++) {
  301. const element = value[i];
  302. let formatResult = this.format(rule, element, message)
  303. if (formatResult !== null) {
  304. return formatResult
  305. }
  306. }
  307. return null
  308. }
  309. }
  310. class SchemaValidator extends RuleValidator {
  311. constructor(schema, options) {
  312. super(SchemaValidator.message);
  313. this._schema = schema
  314. this._options = options || null
  315. }
  316. updateSchema(schema) {
  317. this._schema = schema
  318. }
  319. async validate(data, allData) {
  320. let result = this._checkFieldInSchema(data)
  321. if (!result) {
  322. result = await this.invokeValidate(data, false, allData)
  323. }
  324. return result.length ? result[0] : null
  325. }
  326. async validateAll(data, allData) {
  327. let result = this._checkFieldInSchema(data)
  328. if (!result) {
  329. result = await this.invokeValidate(data, true, allData)
  330. }
  331. return result
  332. }
  333. async validateUpdate(data, allData) {
  334. let result = this._checkFieldInSchema(data)
  335. if (!result) {
  336. result = await this.invokeValidateUpdate(data, false, allData)
  337. }
  338. return result.length ? result[0] : null
  339. }
  340. async invokeValidate(data, all, allData) {
  341. let result = []
  342. let schema = this._schema
  343. for (let key in schema) {
  344. let value = schema[key]
  345. let errorMessage = await this.validateRule(key, value, data[key], data, allData)
  346. if (errorMessage != null) {
  347. result.push({
  348. key,
  349. errorMessage
  350. })
  351. if (!all) break
  352. }
  353. }
  354. return result
  355. }
  356. async invokeValidateUpdate(data, all, allData) {
  357. let result = []
  358. for (let key in data) {
  359. let errorMessage = await this.validateRule(key, this._schema[key], data[key], data, allData)
  360. if (errorMessage != null) {
  361. result.push({
  362. key,
  363. errorMessage
  364. })
  365. if (!all) break
  366. }
  367. }
  368. return result
  369. }
  370. _checkFieldInSchema(data) {
  371. var keys = Object.keys(data)
  372. var keys2 = Object.keys(this._schema)
  373. if (new Set(keys.concat(keys2)).size === keys2.length) {
  374. return ''
  375. }
  376. var noExistFields = keys.filter((key) => {
  377. return keys2.indexOf(key) < 0;
  378. })
  379. var errorMessage = formatMessage({
  380. field: JSON.stringify(noExistFields)
  381. }, SchemaValidator.message.TAG + SchemaValidator.message['defaultInvalid'])
  382. return [{
  383. key: 'invalid',
  384. errorMessage
  385. }]
  386. }
  387. }
  388. function Message() {
  389. return {
  390. TAG: "",
  391. default: '验证错误',
  392. defaultInvalid: '提交的字段{field}在数据库中并不存在',
  393. validateFunction: '验证无效',
  394. required: '{label}必填',
  395. 'enum': '{label}超出范围',
  396. timestamp: '{label}格式无效',
  397. whitespace: '{label}不能为空',
  398. typeError: '{label}类型无效',
  399. date: {
  400. format: '{label}日期{value}格式无效',
  401. parse: '{label}日期无法解析,{value}无效',
  402. invalid: '{label}日期{value}无效'
  403. },
  404. length: {
  405. minLength: '{label}长度不能少于{minLength}',
  406. maxLength: '{label}长度不能超过{maxLength}',
  407. range: '{label}必须介于{minLength}和{maxLength}之间'
  408. },
  409. number: {
  410. minimum: '{label}不能小于{minimum}',
  411. maximum: '{label}不能大于{maximum}',
  412. exclusiveMinimum: '{label}不能小于等于{minimum}',
  413. exclusiveMaximum: '{label}不能大于等于{maximum}',
  414. range: '{label}必须介于{minimum}and{maximum}之间'
  415. },
  416. pattern: {
  417. mismatch: '{label}格式不匹配'
  418. }
  419. };
  420. }
  421. SchemaValidator.message = new Message();
  422. export default SchemaValidator