Skip to content

Commit ea9354c

Browse files
committed
Added validation to addJob() in jobs/queue.mjs to ensure that data is an object.
1 parent 900d23b commit ea9354c

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

ving/docs/change-log.md

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ outline: deep
33
---
44
# Change Log
55

6+
## December 2024
7+
8+
### 2024-12-17
9+
* Added validation to addJob() in jobs/queue.mjs to ensure that data is an object.
10+
611
## October 2024
712

813
### 2024-10-30

ving/jobs/handlers/Test.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import ving from '#ving/index.mjs';
66
* @returns {boolean} `true`
77
*/
88
export default async function (job) {
9-
ving.log('jobs').debug(`Test ran with data: ${JSON.stringify(job.data)}`);
9+
ving.log('jobs').debug(`Test ran with data: ${JSON.stringify(job.data)} which is a ${typeof job.data}`);
1010
return true;
1111
}

ving/jobs/queue.mjs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Queue } from 'bullmq';
22
import ving from '#ving/index.mjs';
33
import { useRedis } from '#ving/redis.mjs';
44
import { jobHandlers } from '#ving/jobs/map.mjs';
5+
import { isObject } from '#ving/utils/identify.mjs';
56

67
/**
78
* Get BullMQ queue object.
@@ -35,7 +36,7 @@ export const getHandlerNames = () => {
3536
* Enqueues a job in the jobs system.
3637
*
3738
* @param {string} type Must match the filename (without the `.mjs`) of a job handler.
38-
* @param {Object} data An object containing whatever data you wish to pass into the job.
39+
* @param {Object|Undefined} data An object containing whatever data you wish to pass into the job.
3940
* @param {Object} options An object with optional properties.
4041
* @param {string} options.queueName The name of the queue to add this job to. Defaults to `jobs`.
4142
* @param {number} options.delay The number of milliseconds to wait before executing this job. Defaults to running as soon as possible.
@@ -49,6 +50,9 @@ export const getHandlerNames = () => {
4950
export const addJob = async (type, data = {}, options = { queueName: 'jobs' }) => {
5051
if (!(getHandlerNames().includes(type)))
5152
throw ving.ouch(404, `Job handler ${type} is not available.`);
53+
if (!isObject(data)) {
54+
throw ving.ouch(442, `Data must be an object.`);
55+
}
5256
const queue = getQueue(options);
5357
const jobOptions = {
5458
removeOnComplete: {

0 commit comments

Comments
 (0)