fix: Move MongoDB JSON validation to Monitor.validate()
Move databaseQuery JSON validation from mongodb.js check() method to Monitor.validate() to follow the same pattern as other monitor types (e.g., ping monitor validations). This ensures validation happens at the model level before the check is executed.
This commit is contained in:
parent
c3d7b6a989
commit
a2f21ecb7d
@ -1857,6 +1857,15 @@ class Monitor extends BeanModel {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.type === "mongodb" && this.databaseQuery) {
|
||||
// Validate that databaseQuery is valid JSON
|
||||
try {
|
||||
JSON.parse(this.databaseQuery);
|
||||
} catch (error) {
|
||||
throw new Error(`Invalid JSON in database query: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -12,11 +12,8 @@ class MongodbMonitorType extends MonitorType {
|
||||
async check(monitor, heartbeat, _server) {
|
||||
let command = { ping: 1 };
|
||||
if (monitor.databaseQuery) {
|
||||
try {
|
||||
command = JSON.parse(monitor.databaseQuery);
|
||||
} catch (error) {
|
||||
throw new Error(`Invalid JSON in database query: ${error.message}`);
|
||||
}
|
||||
// databaseQuery is validated in Monitor.validate(), so we can safely parse it here
|
||||
command = JSON.parse(monitor.databaseQuery);
|
||||
}
|
||||
|
||||
let result = await this.runMongodbCommand(monitor.databaseConnectionString, command);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user