From ddf21be38cc04860029d8a5e587ef0f0da0e960d Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 7 Mar 2024 12:58:06 -0500 Subject: [PATCH] fix: allow other params when using projectids --- src/validations/projects.validations.js | 40 ++++++++++---------- src/validations/units.validations.js | 50 ++++++++++++------------- 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/src/validations/projects.validations.js b/src/validations/projects.validations.js index 03bea033..ad770f03 100644 --- a/src/validations/projects.validations.js +++ b/src/validations/projects.validations.js @@ -63,27 +63,25 @@ export const baseSchema = { export const projectsGetQuerySchema = Joi.object({ page: Joi.number().optional(), limit: Joi.number().max(100).min(1).optional(), - 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({ projectIds: Joi.array().items(Joi.string()).single().min(1) }), - { - then: Joi.object().options({ presence: 'optional' }), // Make page and limit optional - otherwise: Joi.object({ - page: Joi.number().required(), - limit: Joi.number().required(), - }), - }, - ) - .and('page', 'limit'); + search: Joi.string().optional(), + columns: Joi.array().items(Joi.string()).single().optional(), + orgUid: Joi.string().optional(), + warehouseProjectId: Joi.string().optional(), + xls: Joi.boolean().optional(), + projectIds: Joi.array().items(Joi.string()).single().optional(), + order: Joi.string().regex(genericSortColumnRegex).optional(), + filter: Joi.string().regex(genericFilterRegex).optional(), + onlyMarketplaceProjects: Joi.boolean().optional(), +}).when(Joi.object({ warehouseProjectId: Joi.string().required() }).unknown(), { + then: Joi.object({ + page: Joi.number().optional(), + limit: Joi.number().optional(), + }), + otherwise: Joi.object({ + page: Joi.number().required(), + limit: Joi.number().required(), + }), +}); export const projectsPostSchema = Joi.object({ ...baseSchema, diff --git a/src/validations/units.validations.js b/src/validations/units.validations.js index 31833090..0f459971 100644 --- a/src/validations/units.validations.js +++ b/src/validations/units.validations.js @@ -54,35 +54,31 @@ export const unitsPostSchema = Joi.object({ }); export const unitsGetQuerySchema = Joi.object({ - page: Joi.number().min(1), - limit: Joi.number().max(100).min(1), - search: Joi.string(), - warehouseUnitId: Joi.string(), - columns: Joi.array().items(Joi.string()).single(), - orgUid: Joi.string(), + page: Joi.number().min(1).optional(), + limit: Joi.number().max(100).min(1).optional(), + search: Joi.string().optional(), + warehouseUnitId: Joi.string().optional(), + columns: Joi.array().items(Joi.string()).single().optional(), + orgUid: Joi.string().optional(), order: Joi.alternatives().try( - Joi.string().valid('SERIALNUMBER', 'ASC', 'DESC'), - Joi.string().regex(genericSortColumnRegex).min(1).max(100), + Joi.string().valid('SERIALNUMBER', 'ASC', 'DESC').optional(), + Joi.string().regex(genericSortColumnRegex).min(1).max(100).optional(), ), - xls: Joi.boolean(), - marketplaceIdentifiers: Joi.array().items(Joi.string()).single(), - hasMarketplaceIdentifier: Joi.boolean(), - includeProjectInfoInSearch: Joi.boolean(), - filter: Joi.string().regex(genericFilterRegex).min(1).max(100), -}) - .when( - Joi.object({ - warehouseUnitId: Joi.string().min(1), - }).or('warehouseUnitId'), - { - then: Joi.object(), - otherwise: Joi.object({ - page: Joi.number().min(1).required(), - limit: Joi.number().max(100).min(1).required(), - }), - }, - ) - .and('page', 'limit'); + xls: Joi.boolean().optional(), + marketplaceIdentifiers: Joi.array().items(Joi.string()).single().optional(), + hasMarketplaceIdentifier: Joi.boolean().optional(), + includeProjectInfoInSearch: Joi.boolean().optional(), + filter: Joi.string().regex(genericFilterRegex).min(1).max(100).optional(), +}).when(Joi.object({ warehouseUnitId: Joi.string().required() }).unknown(), { + then: Joi.object({ + page: Joi.number().optional(), + limit: Joi.number().optional(), + }), + otherwise: Joi.object({ + page: Joi.number().min(1).required(), + limit: Joi.number().max(100).min(1).required(), + }), +}); export const unitsUpdateSchema = Joi.object({ warehouseUnitId: Joi.string().required(),