Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"dev": "node src/server.js",
"start": "node src/server.js",
"test": "node --test src/tests"
"test": "node --test src/tests/job-budget-range-validation.test.js"
},
"dependencies": {
"cors": "^2.8.5",
Expand Down
49 changes: 49 additions & 0 deletions apps/api/src/tests/job-budget-range-validation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import test from "node:test";
import assert from "node:assert/strict";
import { createJobSchema, updateJobSchema } from "../validators/job.js";

const validJob = {
title: "Budget range guard",
description: "Ensure the validation rejects inverted budget ranges.",
budgetMin: 50,
budgetMax: 100,
categoryId: "cat-1",
skills: ["zod"]
};

test("createJobSchema rejects inverted budget ranges", () => {
const result = createJobSchema.safeParse({
...validJob,
budgetMin: 120,
budgetMax: 80
});

assert.equal(result.success, false);
assert.equal(result.error.issues.length, 1);
assert.deepEqual(result.error.issues[0], {
code: "custom",
path: ["budgetMax"],
message: "budgetMax must be greater than or equal to budgetMin"
});
});

test("updateJobSchema rejects inverted budget ranges when both bounds are present", () => {
const result = updateJobSchema.safeParse({
budgetMin: 120,
budgetMax: 80
});

assert.equal(result.success, false);
assert.equal(result.error.issues.length, 1);
assert.deepEqual(result.error.issues[0], {
code: "custom",
path: ["budgetMax"],
message: "budgetMax must be greater than or equal to budgetMin"
});
});

test("createJobSchema accepts a valid budget range", () => {
const result = createJobSchema.safeParse(validJob);

assert.equal(result.success, true);
});
27 changes: 25 additions & 2 deletions apps/api/src/validators/job.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { z } from "zod";

export const createJobSchema = z.object({
const budgetOrderMessage = "budgetMax must be greater than or equal to budgetMin";

const jobShapeSchema = z.object({
title: z.string().min(4),
description: z.string().min(10),
budgetMin: z.number().nonnegative(),
Expand All @@ -9,4 +11,25 @@ export const createJobSchema = z.object({
skills: z.array(z.string().min(1)).default([])
});

export const updateJobSchema = createJobSchema.partial();
function addBudgetOrderIssue(payload, ctx) {
if (payload.budgetMax < payload.budgetMin) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["budgetMax"],
message: budgetOrderMessage
});
}
}

export const createJobSchema = jobShapeSchema.superRefine((payload, ctx) => {
addBudgetOrderIssue(payload, ctx);
});

export const updateJobSchema = jobShapeSchema.partial().superRefine((payload, ctx) => {
if (
typeof payload.budgetMin === "number" &&
typeof payload.budgetMax === "number"
) {
addBudgetOrderIssue(payload, ctx);
}
});