-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathensureValidUtils.js
153 lines (143 loc) · 5.74 KB
/
ensureValidUtils.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
var isFunction = require('lodash/isFunction');
var isNumber = require('lodash/isNumber');
function ensureValidOptions(options) {
if (!options) throw new Error('options are required by moesif-nodejs middleware');
if (!options.applicationId || typeof options.applicationId !== 'string') {
throw new Error(
'A moesif application id is required. Please obtain it through your settings at www.moesif.com'
);
}
if (options.applicationId.length < 50) {
throw new Error(
'A moesif application id is required. The format of the moesif application id provided does not look correct. Please obtain it through your settings at www.moesif.com'
);
}
if (options.identifyUser && !isFunction(options.identifyUser)) {
throw new Error('identifyUser should be a function');
}
if (options.identifyCompany && !isFunction(options.identifyCompany)) {
throw new Error('identifyCompany should be a function');
}
if (options.getMetadata && !isFunction(options.getMetadata)) {
throw new Error('getMetadata should be a function');
}
if (options.getSessionToken && !isFunction(options.getSessionToken)) {
throw new Error('getSessionToken should be a function');
}
if (options.getTags && !isFunction(options.getTags)) {
throw new Error('getTags should be a function');
}
if (options.getApiVersion && !isFunction(options.getApiVersion)) {
throw new Error('getApiVersion should be a function');
}
if (options.maskContent && !isFunction(options.maskContent)) {
throw new Error('maskContent should be a function');
}
if (options.skip && !isFunction(options.skip)) {
throw new Error('skip should be a function');
}
if (options.retry && (!isNumber(options.retry) || options.retry > 3 || options.retry < 0)) {
throw new Error('If retry is set, it must be a number between 0 to 3.');
}
if (options.batchSize && (!isNumber(options.batchSize) || options.batchSize <= 1)) {
throw new Error('batchSize must be a number greater than or equal to 1');
}
if (options.batchMaxTime && (!isNumber(options.batchMaxTime) || options.batchMaxTime <= 500)) {
throw new Error('batchMaxTime must be greater than 500 milliseonds');
}
if (options.requestMaxBodySize && (!isNumber(options.requestMaxBodySize) || options.requestMaxBodySize < 0)) {
throw new Error('requestMaxBodySize must be a number greater than 0');
}
if (options.responseMaxBodySize && (!isNumber(options.responseMaxBodySize) || options.responseMaxBodySize < 0)) {
throw new Error('responseMaxBodySize must be a number greater than 0');
}
}
function ensureValidLogData(logData) {
if (!logData.request) {
throw new Error(
'For Moesif events, request and response objects are required. Please check your maskContent function do not remove this'
);
} else {
if (!logData.request.time) {
throw new Error(
'For Moesif events, request time is required. Please check your maskContent function do not remove this'
);
}
if (!logData.request.verb) {
throw new Error(
'For Moesif events, request verb is required. Please check your maskContent function do not remove this'
);
}
if (!logData.request.uri) {
throw new Error(
'For Moesif events, request uri is required. Please check your maskContent function do not remove this'
);
}
}
if (!logData.response) {
throw new Error(
'For Moesif events, request and response objects are required. Please check your maskContent function do not remove this'
);
} else {
// if (!logData.response.body) {
// throw new Error('for log events, response body objects is required but can be empty object');
// }
if (!logData.request.time) {
throw new Error(
'For Moesif events, response time is required. The middleware should populate it automatically. Please check your maskContent function do not remove this'
);
}
}
}
function ensureValidUserModel(userModel) {
if (!userModel.userId) {
throw new Error('To update a user, a userId field is required');
}
}
function ensureValidUsersBatchModel(usersBatchModel) {
for (let userModel of usersBatchModel) {
if (!userModel.userId) {
throw new Error('To update a user, a userId field is required');
}
}
}
function ensureValidCompanyModel(companyModel) {
if (!companyModel.companyId) {
throw new Error('To update a company, a companyId field is required');
}
}
function ensureValidCompaniesBatchModel(companiesBatchModel) {
for (let companyModel of companiesBatchModel) {
if (!companyModel.companyId) {
throw new Error('To update a company, a companyId field is required');
}
}
}
function ensureValidActionModel(actionModel) {
if (!actionModel.actionName) {
throw new Error('To send an Action, the actionName field is required');
}
if (!(actionModel.request && actionModel.request.uri)) {
throw new Error('To send an Action, the request and request.uri fields are required');
}
}
function ensureValidActionsBatchModel(actionsBatchModel) {
for (let actionModel of actionsBatchModel) {
if (!actionModel.actionName) {
throw new Error('To send an Action, the actionName field is required');
}
if (!(actionModel.request && actionModel.request.uri)) {
throw new Error('To send an Action, the request and request.uri fields are required');
}
}
}
module.exports = {
ensureValidOptions: ensureValidOptions,
ensureValidLogData: ensureValidLogData,
ensureValidUserModel: ensureValidUserModel,
ensureValidUsersBatchModel: ensureValidUsersBatchModel,
ensureValidCompanyModel: ensureValidCompanyModel,
ensureValidCompaniesBatchModel: ensureValidCompaniesBatchModel,
ensureValidActionModel: ensureValidActionModel,
ensureValidActionsBatchModel: ensureValidActionsBatchModel
};