-
Notifications
You must be signed in to change notification settings - Fork 2
/
validation_utils.js
128 lines (107 loc) · 3.51 KB
/
validation_utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*jslint node: true */
"use strict";
var chash = require('./chash.js');
var web3 = require('web3/lib/utils/utils')
/**
* True if there is at least one field in obj that is not in arrFields.
*/
function hasFieldsExcept(obj, arrFields){
for (var field in obj)
if (arrFields.indexOf(field) === -1)
return true;
return false;
}
/**
* ES6 Number.isInteger Ponyfill.
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
*/
function isInteger(value){
return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
};
/**
* True if int is an integer strictly greater than zero.
*/
function isPositiveInteger(int){
return (isInteger(int) && int > 0);
}
/**
* True if int is an integer greater than or equal to zero.
*/
function isNonnegativeInteger(int){
return (isInteger(int) && int >= 0);
}
/**
* True if str is a string and not the empty string.
*/
function isNonemptyString(str){
return (typeof str === "string" && str.length > 0);
}
/**
* True if str is a string and has length len. False if len not provided.
*/
function isStringOfLength(str, len){
return (typeof str === "string" && str.length === len);
}
function isValidChash(str, len){
//return (isStringOfLength(str, len) && chash.isChashValid(str));
return isStringOfLength(str, len);
}
function isValidAddressAnyCase(address){
return isValidChash(address, 32);
}
/**
* 增加BTC地址验证
* @param address
* @param type
* @returns {*}
*/
function isValidAddress(address,type){
let Bitcore = require('bitcore-lib');
switch (type) {
case 'INVE':
return (typeof address === "string" && address === address.toUpperCase() && isValidChash(address, 32));
break;
case 'BTC':
return Bitcore.Address.isValid(address,'livenet');
break;
case 'ETH':
return web3.isAddress(address);
break;
case 'SNC':
return (typeof address === "string" && address.length == 76);
break;
}
}
function isValidDeviceAddress(address){
return ( isStringOfLength(address, 33) && address[0] === '0' && isValidAddress(address.substr(1)) );
}
function isNonemptyArray(arr){
return (Array.isArray(arr) && arr.length > 0);
}
function isArrayOfLength(arr, len){
return (Array.isArray(arr) && arr.length === len);
}
function isNonemptyObject(obj){
return (obj && typeof obj === "object" && !Array.isArray(obj) && Object.keys(obj).length > 0);
}
function isValidBase64(b64, len){
return (typeof b64 === "string" && b64.length === len && b64 === (new Buffer(b64, "base64")).toString("base64"));
}
function isValidEmail(str) {
return (typeof str === "string" && /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str));
}
exports.hasFieldsExcept = hasFieldsExcept;
exports.isNonemptyString = isNonemptyString;
exports.isStringOfLength = isStringOfLength;
exports.isInteger = isInteger;
exports.isNonnegativeInteger = isNonnegativeInteger;
exports.isPositiveInteger = isPositiveInteger;
exports.isNonemptyObject = isNonemptyObject;
exports.isNonemptyArray = isNonemptyArray;
exports.isArrayOfLength = isArrayOfLength;
exports.isValidAddressAnyCase = isValidAddressAnyCase;
exports.isValidAddress = isValidAddress;
exports.isValidDeviceAddress = isValidDeviceAddress;
exports.isValidBase64 = isValidBase64;
exports.isValidEmail = isValidEmail;