Skip to content

Commit 3ed39ea

Browse files
committed
1 parent bdf273b commit 3ed39ea

File tree

16 files changed

+599
-16
lines changed

16 files changed

+599
-16
lines changed

services/client/controllers/api/routes/picklist.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@ import logger from '../../../../lib/logger.js';
66

77
const router = Router();
88

9-
router.get('/', async (req, res) => {
10-
res.json({ message: 'Picklist API Root' });
9+
router.get('/', validate(schema.picklistQuery, {reqParts: ['query']}), async (req, res) => {
10+
try {
11+
logger.info('Picklist query validated', {corkTraceId: req.corkTraceId});
12+
const r = await models.picklist.query(req.payload);
13+
if (r.error) {
14+
throw r.error;
15+
}
16+
logger.info('Picklist query successful', {corkTraceId: req.corkTraceId, resultCount: r.res.total_count});
17+
res.status(200).json(r.res);
18+
} catch (e) {
19+
return handleError(res, req, e);
20+
}
1121
});
1222

1323
// create picklist

services/client/controllers/api/utils/validation/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import handleError from "../handleError.js";
2-
import { picklistCreateSchema } from './schemas/picklist.js';
2+
import { picklistCreateSchema, picklistQuerySchema } from './schemas/picklist.js';
33

44
/**
55
* @description Middleware to validate request data against a Zod schema.
@@ -45,6 +45,7 @@ function formatErrorResponse(zodError) {
4545

4646
const schema = {
4747
picklistCreate: picklistCreateSchema,
48+
picklistQuery: picklistQuerySchema
4849
};
4950

5051
export { validate, schema };

services/client/controllers/api/utils/validation/schemas/picklist.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as z from "zod";
2-
import { requiredString, urlFriendlyString } from "./utils.js";
2+
import { requiredString, urlFriendlyString, pageParam, perPageParam, booleanParam } from "./utils.js";
33
import models from '../../../../../../lib/models/index.js';
44

55
const picklistBaseSchema = z.object({
@@ -19,12 +19,27 @@ const picklistCreateSchema = picklistBaseSchema.extend({
1919
if (existing.res) {
2020
ctx.addIssue({
2121
code: z.ZodIssueCode.custom,
22-
message: 'Picklist with this name already exists',
22+
message: 'A picklist with this name already exists',
2323
path: []
2424
});
2525
}
2626
}),
2727
label: requiredString().pipe(z.string().max(250))
2828
});
2929

30-
export { picklistBaseSchema, picklistCreateSchema };
30+
const picklistQuerySchema = z.object({
31+
page: pageParam,
32+
per_page: perPageParam(15),
33+
active: z.preprocess(
34+
v => {
35+
if (v === 'true') return true;
36+
if (v === 'false') return false;
37+
return v;
38+
},
39+
z.union([z.literal(true), z.literal(false)]).optional()
40+
),
41+
archived_only: booleanParam,
42+
active_only: booleanParam
43+
});
44+
45+
export { picklistBaseSchema, picklistCreateSchema, picklistQuerySchema };

services/client/controllers/api/utils/validation/schemas/utils.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,35 @@ const urlFriendlyString = z.string().regex(/^[a-z0-9_-]+$/, {
4040
message: "Must be URL-friendly: lowercase letters, numbers, hyphens, and underscores only."
4141
});
4242

43-
export { requiredString, requiredNumber, urlFriendlyString };
43+
const pageParam = z.preprocess(
44+
v => {
45+
if (v == null || v === '') return 1;
46+
const n = Number(v);
47+
return Number.isFinite(n) ? n : v;
48+
},
49+
z.number().int().positive("Page must be a positive integer")
50+
);
51+
52+
const perPageParam = (defaultValue = 15, maxValue = 50) =>
53+
z.preprocess(
54+
v => {
55+
if (v == null || v === '') return defaultValue;
56+
const n = Number(v);
57+
return Number.isFinite(n) ? n : v;
58+
},
59+
z.number()
60+
.int()
61+
.positive("per_page must be a positive integer")
62+
.max(maxValue, `per_page must be ≤ ${maxValue}`)
63+
);
64+
65+
const booleanParam = z.preprocess(
66+
v => {
67+
if (v === 'true') return true;
68+
if (v === 'false') return false;
69+
return v;
70+
},
71+
z.boolean().optional()
72+
);
73+
74+
export { requiredString, requiredNumber, urlFriendlyString, pageParam, perPageParam, booleanParam };

0 commit comments

Comments
 (0)