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:
Angel98518 2026-01-20 07:09:09 +01:00
parent c3d7b6a989
commit a2f21ecb7d
2 changed files with 11 additions and 5 deletions

View File

@ -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}`);
}
}
}
/**

View File

@ -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);