-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvalidation_utils.js
More file actions
81 lines (61 loc) · 2.24 KB
/
validation_utils.js
File metadata and controls
81 lines (61 loc) · 2.24 KB
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
/*jslint node: true */
"use strict";
var chash = require('./chash.js');
function hasFieldsExcept(obj, arrFields){
for (var field in obj)
if (arrFields.indexOf(field) === -1)
return true;
return false;
}
function isInteger(int){
return (typeof int === 'number' && int.toString().indexOf('.') === -1 && !isNaN(int));
}
function isPositiveInteger(int){
return (typeof int === 'number' && int > 0 && int.toString().indexOf('.') === -1 && !isNaN(int));
}
function isNonnegativeInteger(int){
return (typeof int === 'number' && int >= 0 && int.toString().indexOf('.') === -1 && !isNaN(int));
}
function isNonemptyString(str){
return (typeof str === "string" && str.length > 0);
}
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 isValidBase64(b64, len){
return (typeof b64 === "string" && b64.length === len && b64 === (new Buffer(b64, "base64")).toString("base64"));
}
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;