From 86e073a37a2db70208f8d30f8637d5c4a88dd9c4 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 11 Dec 2023 09:52:15 -0500 Subject: [PATCH] feat: require pagination --- src/validations/projects.validations.js | 42 ++++++++++++-------- src/validations/units.validations.js | 51 +++++++++++++++---------- 2 files changed, 56 insertions(+), 37 deletions(-) diff --git a/src/validations/projects.validations.js b/src/validations/projects.validations.js index d894fef2..9d835c80 100644 --- a/src/validations/projects.validations.js +++ b/src/validations/projects.validations.js @@ -60,22 +60,32 @@ export const baseSchema = { timeStaged: Joi.date().timestamp().allow(null).optional(), }; -export const projectsGetQuerySchema = Joi.object() - .keys({ - page: Joi.number(), - limit: Joi.number(), - search: Joi.string(), - columns: Joi.array().items(Joi.string()).single(), - orgUid: Joi.string(), - warehouseProjectId: Joi.string(), - xls: Joi.boolean(), - projectIds: Joi.array().items(Joi.string()).single(), - order: Joi.string().regex(genericSortColumnRegex), - filter: Joi.string().regex(genericFilterRegex), - onlyMarketplaceProjects: Joi.boolean(), - }) - .with('page', 'limit') - .with('limit', 'page'); +export const projectsGetQuerySchema = Joi.object({ + page: Joi.number(), + limit: Joi.number(), + search: Joi.string(), + columns: Joi.array().items(Joi.string()).single(), + orgUid: Joi.string(), + warehouseProjectId: Joi.string(), + xls: Joi.boolean(), + projectIds: Joi.array().items(Joi.string()).single(), + order: Joi.string().regex(genericSortColumnRegex), + filter: Joi.string().regex(genericFilterRegex), + onlyMarketplaceProjects: Joi.boolean(), +}) + .when( + Joi.object({ + warehouseProjectId: Joi.string().min(1), + }).or('warehouseProjectId'), + { + then: Joi.object(), + otherwise: Joi.object({ + page: Joi.number().required(), + limit: Joi.number().required(), + }), + }, + ) + .and('page', 'limit'); export const projectsPostSchema = Joi.object({ ...baseSchema, diff --git a/src/validations/units.validations.js b/src/validations/units.validations.js index f78c0c6f..eae4c519 100644 --- a/src/validations/units.validations.js +++ b/src/validations/units.validations.js @@ -53,27 +53,36 @@ export const unitsPostSchema = Joi.object({ ...unitsBaseSchema, }); -export const unitsGetQuerySchema = Joi.object() - .keys({ - page: Joi.number(), - limit: Joi.number(), - search: Joi.string(), - warehouseUnitId: Joi.string(), - columns: Joi.array().items(Joi.string()).single(), - orgUid: Joi.string(), - order: Joi.alternatives().try( - // backwards compatibility for old order usage - Joi.string().valid('SERIALNUMBER', 'ASC', 'DESC'), - // new order usage - Joi.string().regex(genericSortColumnRegex), - ), - xls: Joi.boolean(), - marketplaceIdentifiers: Joi.array().items(Joi.string()).single(), - hasMarketplaceIdentifier: Joi.boolean(), - includeProjectInfoInSearch: Joi.boolean(), - filter: Joi.string().regex(genericFilterRegex), - }) - .with('page', 'limit'); +export const unitsGetQuerySchema = Joi.object({ + page: Joi.number(), + limit: Joi.number(), + search: Joi.string(), + warehouseUnitId: Joi.string(), + columns: Joi.array().items(Joi.string()).single(), + orgUid: Joi.string(), + order: Joi.alternatives().try( + Joi.string().valid('SERIALNUMBER', 'ASC', 'DESC'), + Joi.string().regex(genericSortColumnRegex), + ), + xls: Joi.boolean(), + marketplaceIdentifiers: Joi.array().items(Joi.string()).single(), + hasMarketplaceIdentifier: Joi.boolean(), + includeProjectInfoInSearch: Joi.boolean(), + filter: Joi.string().regex(genericFilterRegex), +}) + .when( + Joi.object({ + warehouseUnitId: Joi.string().min(1), + }).or('warehouseUnitId'), + { + then: Joi.object(), + otherwise: Joi.object({ + page: Joi.number().required(), + limit: Joi.number().required(), + }), + }, + ) + .and('page', 'limit'); export const unitsUpdateSchema = Joi.object({ warehouseUnitId: Joi.string().required(),