-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidate.js
207 lines (188 loc) · 5.22 KB
/
validate.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"use strict";
/**
* Module to validate parameters.
*
* @module lib/validate
*/
/**
* Validate parameters constructor
*
* @constructor
*/
const ValidateKlass = function () {
};
ValidateKlass.prototype = {
/**
* Check if parameter is present
*
* @param {string} param - parameter value
*
* @public
*/
isPresent: function (param) {
return (typeof param !== 'undefined' && param !== null && String(param).trim() !== '');
},
/**
* Check if parameter is valid.
*
* @param {string} param - parameter value
* @public
*/
isValid: function (param) {
const oThis = this,
regex = /^[a-zA-Z0-9_\.\-]+$/;
return oThis.isPresent(param) && String(param).match(regex);
},
/**
* Get id from params.
*
* @param {object} params
*/
getId: function (params) {
const oThis = this;
if (oThis.isValid(params.id)) {
var id = params.id;
delete params.id;
return id;
} else {
throw new Error('id missing or invalid in request params');
}
},
/**
* Get user id from params.
*
* @param {object} params
*/
getUserId: function (params) {
const oThis = this;
if (oThis.isValid(params.user_id)) {
var userId = params.user_id;
delete params.user_id;
return userId
} else {
throw new Error('user_id missing or invalid in request params');
}
},
/**
* Get redeemable sku id from params.
*
* @param {object} params
* @param {string} params.redeemable_sku_id
*/
getRedeemableSkuId: function (params) {
const oThis = this;
if (oThis.isValid(params.redeemable_sku_id)) {
var redeemableSkuId = params.redeemable_sku_id;
delete params.redeemable_sku_id;
return redeemableSkuId
} else {
throw new Error('redeemable_sku_id missing or invalid in request params');
}
},
/**
* Get redemption uid from params.
*
* @param {object} params
* @param {string} params.redemption_id
*/
getRedemptionId: function (params) {
const oThis = this;
if (oThis.isValid(params.redemption_id)) {
let redemption_id = params.redemption_id;
delete params.redemption_id;
return redemption_id
} else {
throw new Error('redemption_id missing or invalid in request params.');
}
},
/**
* Get webhook id from params.
*
* @param {object} params
*/
getWebhookId: function (params) {
const oThis = this;
if (oThis.isValid(params.webhook_id)) {
let webhookId = params.webhook_id;
return webhookId
} else {
throw new Error('webhook_id missing or invalid in request params');
}
},
/**
* Get chain id from params
*
* @param {object} params
*/
getChainId: function (params) {
const oThis = this;
if (oThis.isValid(params.chain_id)) {
var chainId = params.chain_id;
delete params.chain_id;
return chainId
} else {
throw new Error('chain_id missing or invalid in request params');
}
},
/**
* Get device address from params
*
* @param {object} params
*/
getDeviceAddress: function (params) {
const oThis = this;
if (oThis.isValid(params.device_address)) {
var deviceAddress = params.device_address;
delete params.device_address;
return deviceAddress;
} else {
throw new Error('device_address missing or invalid in request params');
}
},
/**
* Get session address from params
*
* @param {object} params
*/
getSessionAddress: function (params) {
const oThis = this;
if (oThis.isValid(params.session_address)) {
var sessionAddress = params.session_address;
delete params.session_address;
return sessionAddress;
} else {
throw new Error('session_address missing or invalid in request params');
}
},
/**
* Get transaction id from params
*
* @param {object} params
*/
getTransactionId: function (params) {
const oThis = this;
if (oThis.isValid(params.transaction_id)) {
var TxAddress = params.transaction_id;
delete params.transaction_id;
return TxAddress;
} else {
throw new Error('transaction_id missing or invalid in request params');
}
},
/**
* Get recovery owner address from params
*
* @param {object} params
*/
getRecoveryOwnerAddress: function (params) {
const oThis = this;
if (oThis.isValid(params.recovery_owner_address)) {
var RecoveryAddress = params.recovery_owner_address;
delete params.recovery_owner_address;
return RecoveryAddress;
} else {
throw new Error('recovery_owner_address missing or invalid in request params');
}
}
};
module.exports = new ValidateKlass();