-
Notifications
You must be signed in to change notification settings - Fork 216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/json validation #1700
Feature/json validation #1700
Changes from all commits
6000096
b7a8596
81a0ec8
8b49603
b263e27
05e448c
9fe2de8
4d12608
c3d92ec
6837adb
1f0876d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import jmespath from 'jmespath'; | ||
const SERVICE_NAME = "NetworkService"; | ||
const UPROCK_ENDPOINT = "https://api.uprock.com/checkmate/push"; | ||
|
||
|
@@ -122,20 +123,19 @@ class NetworkService { | |
*/ | ||
async requestHttp(job) { | ||
try { | ||
const url = job.data.url; | ||
const { url, secret, _id, name, teamId, type, jsonPath, matchMethod, expectedValue } = job.data; | ||
const config = {}; | ||
|
||
job.data.secret !== undefined && | ||
(config.headers = { Authorization: `Bearer ${job.data.secret}` }); | ||
secret !== undefined && (config.headers = { Authorization: `Bearer ${secret}` }); | ||
|
||
const { response, responseTime, error } = await this.timeRequest(() => | ||
this.axios.get(url, config) | ||
); | ||
|
||
const httpResponse = { | ||
monitorId: job.data._id, | ||
teamId: job.data.teamId, | ||
type: job.data.type, | ||
monitorId: _id, | ||
teamId, | ||
type, | ||
responseTime, | ||
payload: response?.data, | ||
}; | ||
|
@@ -144,12 +144,62 @@ class NetworkService { | |
const code = error.response?.status || this.NETWORK_ERROR; | ||
httpResponse.code = code; | ||
httpResponse.status = false; | ||
httpResponse.message = this.http.STATUS_CODES[code] || "Network Error"; | ||
httpResponse.message = this.http.STATUS_CODES[code] || this.stringService.httpNetworkError; | ||
return httpResponse; | ||
} | ||
httpResponse.status = true; | ||
|
||
httpResponse.code = response.status; | ||
httpResponse.message = this.http.STATUS_CODES[response.status]; | ||
|
||
if (!expectedValue) { | ||
// not configure expected value, return | ||
httpResponse.status = true; | ||
httpResponse.message = this.http.STATUS_CODES[response.status]; | ||
return httpResponse; | ||
} | ||
|
||
// validate if response data match expected value | ||
let result = response?.data; | ||
|
||
this.logger.info({ | ||
service: this.SERVICE_NAME, | ||
method: "requestHttp", | ||
message: `Job: [${name}](${_id}) match result with expected value`, | ||
details: { expectedValue, result, jsonPath, matchMethod } | ||
}); | ||
|
||
if (jsonPath) { | ||
const contentType = response.headers['content-type']; | ||
|
||
const isJson = contentType?.includes('application/json'); | ||
if (!isJson) { | ||
httpResponse.status = false; | ||
httpResponse.message = this.stringService.httpNotJson; | ||
return httpResponse; | ||
} | ||
|
||
try { | ||
result = jmespath.search(result, jsonPath); | ||
} catch (error) { | ||
httpResponse.status = false; | ||
httpResponse.message = this.stringService.httpJsonPathError; | ||
return httpResponse; | ||
} | ||
} | ||
|
||
if (result === null || result === undefined) { | ||
httpResponse.status = false; | ||
httpResponse.message = this.stringService.httpEmptyResult; | ||
return httpResponse; | ||
} | ||
|
||
let match; | ||
result = typeof result === "object" ? JSON.stringify(result) : result.toString(); | ||
if (matchMethod === "include") match = result.includes(expectedValue); | ||
else if (matchMethod === "regex") match = new RegExp(expectedValue).test(result); | ||
else match = result === expectedValue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good, this is much easier to read 👍 |
||
|
||
httpResponse.status = match; | ||
httpResponse.message = match ? this.stringService.httpMatchSuccess : this.stringService.httpMatchFail; | ||
return httpResponse; | ||
} catch (error) { | ||
error.service = this.SERVICE_NAME; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,6 +194,9 @@ const createMonitorBodyValidation = joi.object({ | |
}), | ||
notifications: joi.array().items(joi.object()), | ||
secret: joi.string(), | ||
jsonPath: joi.string(), | ||
expectedValue: joi.string(), | ||
matchMethod: joi.string(), | ||
Comment on lines
+197
to
+199
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add enumeration validation for To ensure only valid match methods are accepted, add enumeration constraints to the Apply this diff: matchMethod: joi.string(),
+ matchMethod: joi.string().valid('equal', 'include', 'regex'),
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is worth doing |
||
}); | ||
|
||
const editMonitorBodyValidation = joi.object({ | ||
|
@@ -202,6 +205,9 @@ const editMonitorBodyValidation = joi.object({ | |
interval: joi.number(), | ||
notifications: joi.array().items(joi.object()), | ||
secret: joi.string(), | ||
jsonPath: joi.string(), | ||
expectedValue: joi.string(), | ||
matchMethod: joi.string(), | ||
Comment on lines
+208
to
+210
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add enumeration validation for Ensure consistency by adding the same enumeration constraints to Apply this diff: matchMethod: joi.string(),
+ matchMethod: joi.string().valid('equal', 'include', 'regex'),
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, worth addding this to the validation |
||
}); | ||
|
||
const pauseMonitorParamValidation = joi.object({ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance secret handling security.
The current secret handling might expose sensitive data in logs. Let's keep that secret sauce under wraps!
📝 Committable suggestion