diff --git a/backend/src/api/bs/bs.api.ts b/backend/src/api/bs/bs.api.ts index 1e51466a1..913f5afe6 100644 --- a/backend/src/api/bs/bs.api.ts +++ b/backend/src/api/bs/bs.api.ts @@ -24,7 +24,10 @@ import { TokenPayload, } from '~/common/types/types'; import { FastifyRouteSchemaDef } from 'fastify/types/schema'; -import { bsSpaceCreate as bsSpaceCreateValidationSchema } from '~/validation-schemas/validation-schemas'; +import { + bsSpaceCreate as bsSpaceCreateValidationSchema, + UUID as UUIDValidationSchema, +} from '~/validation-schemas/validation-schemas'; import { upload as uploadHook, checkHasPermissions as checkHasPermissionsHook, @@ -104,6 +107,19 @@ const initBsApi: FastifyPluginAsync = async (fastify, opts) => { checkHasPermissionsHook(Permission.MANAGE_BS), checkHasRoleHook(UserRole.WORKER), ], + schema: { + params: UUIDValidationSchema, + }, + validatorCompiler({ + schema, + }: FastifyRouteSchemaDef) { + return ( + data: BSSpaceDeleteParamsDto, + ): ReturnType => { + return schema.validate(data); + }; + }, + async handler( req: FastifyRequest<{ Params: BSSpaceDeleteParamsDto }>, rep: FastifyReply, diff --git a/backend/src/api/eam/eam.api.ts b/backend/src/api/eam/eam.api.ts index a7c175dd7..ba6b59d23 100644 --- a/backend/src/api/eam/eam.api.ts +++ b/backend/src/api/eam/eam.api.ts @@ -29,6 +29,7 @@ import { eamGroupCreate as groupCreateValidationSchema, eamWorkerCreateBackend as workerValidationSchema, eamGroupUpdate as groupUpdateValidationSchema, + UUID as UUIDValidationSchema, } from '~/validation-schemas/validation-schemas'; import { checkHasPermissions as checkHasPermissionsHook, @@ -118,6 +119,7 @@ const initEamApi: FastifyPluginAsync = async (fastify, opts) => { url: `${EAMApiPath.GROUPS}${GroupsApiPath.$ID}`, schema: { body: groupUpdateValidationSchema, + params: UUIDValidationSchema, }, preHandler: checkHasPermissionsHook(Permission.MANAGE_EAM), validatorCompiler({ @@ -147,6 +149,18 @@ const initEamApi: FastifyPluginAsync = async (fastify, opts) => { method: HttpMethod.GET, url: `${EAMApiPath.GROUPS}${GroupsApiPath.$ID}`, preHandler: checkHasPermissionsHook(Permission.MANAGE_EAM), + schema: { + params: UUIDValidationSchema, + }, + validatorCompiler({ + schema, + }: FastifyRouteSchemaDef) { + return ( + data: EamGroupGetByIdRequestDto, + ): ReturnType => { + return schema.validate(data); + }; + }, async handler( req: FastifyRequest<{ Params: EamGroupGetByIdRequestDto; @@ -201,6 +215,18 @@ const initEamApi: FastifyPluginAsync = async (fastify, opts) => { method: HttpMethod.DELETE, url: `${EAMApiPath.GROUPS}${GroupsApiPath.$ID}`, preHandler: checkHasPermissionsHook(Permission.MANAGE_EAM), + schema: { + params: UUIDValidationSchema, + }, + validatorCompiler({ + schema, + }: FastifyRouteSchemaDef) { + return ( + data: EAMGroupDeleteParamsDto, + ): ReturnType => { + return schema.validate(data); + }; + }, async handler( req: FastifyRequest<{ Params: EAMGroupDeleteParamsDto }>, rep: FastifyReply, @@ -220,6 +246,19 @@ const initEamApi: FastifyPluginAsync = async (fastify, opts) => { checkHasPermissionsHook(Permission.MANAGE_EAM), checkHasRoleHook(UserRole.MASTER), ], + schema: { + params: UUIDValidationSchema, + }, + validatorCompiler({ + schema, + }: FastifyRouteSchemaDef) { + return ( + data: EAMWorkerDeleteRequestDto, + ): ReturnType => { + return schema.validate(data); + }; + }, + async handler( req: FastifyRequest<{ Params: EAMWorkerDeleteRequestDto }>, rep: FastifyReply, diff --git a/backend/src/api/sc/sc.api.ts b/backend/src/api/sc/sc.api.ts index 3a5f0ad92..9620c6a5b 100644 --- a/backend/src/api/sc/sc.api.ts +++ b/backend/src/api/sc/sc.api.ts @@ -25,6 +25,7 @@ import { FastifyRouteSchemaDef } from 'fastify/types/schema'; import { scInstanceCreate as scInstanceCreateValidationSchema, scInstanceUpdate as scInstanceUpdateValidationSchema, + UUID as UUIDValidationSchema, } from '~/validation-schemas/validation-schemas'; import { checkHasPermissions as checkHasPermissionsHook, @@ -63,6 +64,19 @@ const initScApi: FastifyPluginAsync = async (fastify, opts) => { checkHasPermissionsHook(Permission.MANAGE_SC), checkHasRoleHook(UserRole.WORKER), ], + schema: { + params: UUIDValidationSchema, + }, + validatorCompiler({ + schema, + }: FastifyRouteSchemaDef) { + return ( + data: SCSshKeyGetByIdParamsDto, + ): ReturnType => { + return schema.validate(data); + }; + }, + async handler( req: FastifyRequest<{ Params: SCSshKeyGetByIdParamsDto; @@ -99,6 +113,18 @@ const initScApi: FastifyPluginAsync = async (fastify, opts) => { checkHasPermissionsHook(Permission.MANAGE_SC), checkHasRoleHook(UserRole.WORKER), ], + schema: { + params: UUIDValidationSchema, + }, + validatorCompiler({ + schema, + }: FastifyRouteSchemaDef) { + return ( + data: SCInstanceDeleteParamsDto, + ): ReturnType => { + return schema.validate(data); + }; + }, async handler( req: FastifyRequest<{ Params: SCInstanceDeleteParamsDto; @@ -119,6 +145,7 @@ const initScApi: FastifyPluginAsync = async (fastify, opts) => { ], schema: { body: scInstanceUpdateValidationSchema, + params: UUIDValidationSchema, }, validatorCompiler({ schema, diff --git a/backend/src/api/slc/slc.api.ts b/backend/src/api/slc/slc.api.ts index 053f8a51d..f08737f9e 100644 --- a/backend/src/api/slc/slc.api.ts +++ b/backend/src/api/slc/slc.api.ts @@ -1,7 +1,10 @@ import { FastifyPluginAsync, FastifyReply, FastifyRequest } from 'fastify'; import { FastifyRouteSchemaDef } from 'fastify/types/schema'; import { slcFunction as slcFunctionServ } from '~/services/services'; -import { slcFunctionCreate as slcFunctionCreateValidationSchema } from '~/validation-schemas/validation-schemas'; +import { + slcFunctionCreate as slcFunctionCreateValidationSchema, + UUID as UUIDValidationSchema, +} from '~/validation-schemas/validation-schemas'; import { HttpCode, HttpMethod, @@ -75,6 +78,19 @@ const initSLCApi: FastifyPluginAsync = async (fastify, opts) => { checkHasPermissionsHook(Permission.MANAGE_SLC), checkHasRoleHook(UserRole.WORKER), ], + schema: { + params: UUIDValidationSchema, + }, + validatorCompiler({ + schema, + }: FastifyRouteSchemaDef) { + return ( + data: SLCFunctionLoadParamsDto, + ): ReturnType => { + return schema.validate(data); + }; + }, + async handler( req: FastifyRequest<{ Params: SLCFunctionRunParamsDto; @@ -114,6 +130,18 @@ const initSLCApi: FastifyPluginAsync = async (fastify, opts) => { method: HttpMethod.GET, url: `${SLCApiPath.SLC_FUNCTIONS}${SLCFunctionApiPath.$ID}`, preHandler: checkHasPermissionsHook(Permission.MANAGE_SLC), + schema: { + params: UUIDValidationSchema, + }, + validatorCompiler({ + schema, + }: FastifyRouteSchemaDef) { + return ( + data: SLCFunctionLoadParamsDto, + ): ReturnType => { + return schema.validate(data); + }; + }, async handler( req: FastifyRequest<{ Params: SLCFunctionLoadParamsDto; @@ -137,6 +165,18 @@ const initSLCApi: FastifyPluginAsync = async (fastify, opts) => { checkHasPermissionsHook(Permission.MANAGE_SLC), checkHasRoleHook(UserRole.WORKER), ], + schema: { + params: UUIDValidationSchema, + }, + validatorCompiler({ + schema, + }: FastifyRouteSchemaDef) { + return ( + data: SLCFunctionLoadParamsDto, + ): ReturnType => { + return schema.validate(data); + }; + }, async handler( req: FastifyRequest<{ Params: SLCFunctionDeleteParamsDto }>, rep: FastifyReply, @@ -154,6 +194,18 @@ const initSLCApi: FastifyPluginAsync = async (fastify, opts) => { checkHasPermissionsHook(Permission.MANAGE_SLC), checkHasRoleHook(UserRole.WORKER), ], + schema: { + params: UUIDValidationSchema, + }, + validatorCompiler({ + schema, + }: FastifyRouteSchemaDef) { + return ( + data: SLCFunctionLoadParamsDto, + ): ReturnType => { + return schema.validate(data); + }; + }, async handler( req: FastifyRequest<{ Params: SLCFunctionUpdateParamsDto; diff --git a/backend/src/validation-schemas/uuid/uuid.ts b/backend/src/validation-schemas/uuid/uuid.ts new file mode 100644 index 000000000..b5b17fd42 --- /dev/null +++ b/backend/src/validation-schemas/uuid/uuid.ts @@ -0,0 +1 @@ +export { UUID } from 'bws-shared/validation/validation-schemas'; diff --git a/backend/src/validation-schemas/validation-schemas.ts b/backend/src/validation-schemas/validation-schemas.ts index 42d6e9a56..11a889aa3 100644 --- a/backend/src/validation-schemas/validation-schemas.ts +++ b/backend/src/validation-schemas/validation-schemas.ts @@ -12,3 +12,4 @@ export { bsSpaceCreate } from './bs-space/bs-space'; export { scInstanceCreate, scInstanceUpdate } from './sc-instance/sc-instance'; export { slcFunctionCreate } from './slc-function/slc-function'; export { eamTenantUpdate } from './eam-tenant-update/eam-tenant-update'; +export { UUID } from './uuid/uuid'; diff --git a/shared/src/common/enums/enums.ts b/shared/src/common/enums/enums.ts index fa4795041..57a7ea288 100644 --- a/shared/src/common/enums/enums.ts +++ b/shared/src/common/enums/enums.ts @@ -32,6 +32,8 @@ export { SCInstanceValidationMessage, SLCFunctionValidationMessage, SLCFunctionValidationRule, + UUIDValidationRule, + UUIDValidationMessage, } from './validation/validation'; export { Permission } from './permissions/permissions'; export { UserRole } from './roles/roles'; diff --git a/shared/src/common/enums/validation/uuid/uuid-validation-massage.ts b/shared/src/common/enums/validation/uuid/uuid-validation-massage.ts new file mode 100644 index 000000000..c8c8972b6 --- /dev/null +++ b/shared/src/common/enums/validation/uuid/uuid-validation-massage.ts @@ -0,0 +1,6 @@ +const UUIDValidationMessage = { + UUID_PATTERN: 'uuid is invalid', + UUID_REQUIRE: 'uuid is required', +} as const; + +export { UUIDValidationMessage }; diff --git a/shared/src/common/enums/validation/uuid/uuid-validation-rule.enum.ts b/shared/src/common/enums/validation/uuid/uuid-validation-rule.enum.ts new file mode 100644 index 000000000..f2188df5c --- /dev/null +++ b/shared/src/common/enums/validation/uuid/uuid-validation-rule.enum.ts @@ -0,0 +1,6 @@ +const UUIDValidationRule = { + UUID_PATTERN: + /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, +} as const; + +export { UUIDValidationRule }; diff --git a/shared/src/common/enums/validation/uuid/uuid.ts b/shared/src/common/enums/validation/uuid/uuid.ts new file mode 100644 index 000000000..9ddadc117 --- /dev/null +++ b/shared/src/common/enums/validation/uuid/uuid.ts @@ -0,0 +1,2 @@ +export { UUIDValidationMessage } from './uuid-validation-massage'; +export { UUIDValidationRule } from './uuid-validation-rule.enum'; diff --git a/shared/src/common/enums/validation/validation.ts b/shared/src/common/enums/validation/validation.ts index f66548351..bf63ebe4a 100644 --- a/shared/src/common/enums/validation/validation.ts +++ b/shared/src/common/enums/validation/validation.ts @@ -26,3 +26,4 @@ export { SLCFunctionValidationMessage, SLCFunctionValidationRule, } from './slc-function/slc-function'; +export { UUIDValidationMessage, UUIDValidationRule } from './uuid/uuid'; diff --git a/shared/src/validation/uuid/uuid.validation-shema.ts b/shared/src/validation/uuid/uuid.validation-shema.ts new file mode 100644 index 000000000..7b19f7838 --- /dev/null +++ b/shared/src/validation/uuid/uuid.validation-shema.ts @@ -0,0 +1,18 @@ +import * as Joi from 'joi'; + +import { + UUIDValidationRule, + UUIDValidationMessage, +} from '~/common/enums/enums'; + +const UUID = Joi.object({ + ['id']: Joi.string() + .required() + .regex(UUIDValidationRule.UUID_PATTERN) + .messages({ + 'string.empty': UUIDValidationMessage.UUID_REQUIRE, + 'string.pattern.base': UUIDValidationMessage.UUID_PATTERN, + }), +}); + +export { UUID }; diff --git a/shared/src/validation/validation-schemas.ts b/shared/src/validation/validation-schemas.ts index 63111729b..d0957d0d0 100644 --- a/shared/src/validation/validation-schemas.ts +++ b/shared/src/validation/validation-schemas.ts @@ -13,3 +13,4 @@ export { bsSpaceCreate } from './bs-space/bs-space'; export { scInstanceCreate, scInstanceUpdate } from './sc-instance/sc-instance'; export { slcFunctionCreate } from './slc-function/slc-function'; export { eamTenantUpdate } from './eam-tenant-update/eam-tenant-update.validation-schema'; +export { UUID } from './uuid/uuid.validation-shema';