-
Notifications
You must be signed in to change notification settings - Fork 98
/
validation_utils.js
130 lines (104 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
129
130
/*jslint node: true */
"use strict";
var chash = require('./chash.js');
/**
* 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));
}
function isValidAddressAnyCase(address){
return isValidChash(address, 32);
}
function isValidAddress(address){
return (typeof address === "string" && address === address.toUpperCase() && isValidChash(address, 32));
}
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 isEmptyObjectOrArray(obj) {
if (typeof obj !== "object" || obj === null)
return false;
return (Array.isArray(obj) && obj.length === 0 || Object.keys(obj).length === 0);
}
function isValidBase64(b64, len){
return (typeof b64 === "string" && (!len || b64.length === len) && b64 === Buffer.from(b64, "base64").toString("base64"));
}
function isValidHexadecimal(hex, len){
try {
return (typeof hex === "string" && (!len || hex.length === len) && hex === Buffer.from(hex, "hex").toString("hex"));
}
catch (e) {
return false;
}
}
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));
}
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
exports.hasFieldsExcept = hasFieldsExcept;
exports.isNonemptyString = isNonemptyString;
exports.isStringOfLength = isStringOfLength;
exports.isInteger = isInteger;
exports.isNonnegativeInteger = isNonnegativeInteger;
exports.isPositiveInteger = isPositiveInteger;
exports.isEmptyObjectOrArray = isEmptyObjectOrArray;
exports.isNonemptyObject = isNonemptyObject;
exports.isNonemptyArray = isNonemptyArray;
exports.isArrayOfLength = isArrayOfLength;
exports.isValidAddressAnyCase = isValidAddressAnyCase;
exports.isValidAddress = isValidAddress;
exports.isValidDeviceAddress = isValidDeviceAddress;
exports.isValidBase64 = isValidBase64;
exports.isValidHexadecimal = isValidHexadecimal;
exports.isValidEmail = isValidEmail;
exports.hasOwnProperty = hasOwnProperty;