Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bws-139: + added validation for uuid #224

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
17 changes: 16 additions & 1 deletion backend/src/api/bs/bs.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
BSObjectGetRequestParamsDto,
} 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,
Expand Down Expand Up @@ -98,6 +101,18 @@ const initBsApi: FastifyPluginAsync<Options> = async (fastify, opts) => {
method: HttpMethod.DELETE,
url: `${BSApiPath.SPACES}${SpacesApiPath.$ID}`,
preHandler: checkHasPermissionsHook(Permission.MANAGE_BS),
schema: {
params: UUIDValidationSchema,
},
validatorCompiler({
schema,
}: FastifyRouteSchemaDef<typeof UUIDValidationSchema>) {
return (
data: BSSpaceDeleteParamsDto,
): ReturnType<typeof UUIDValidationSchema['validate']> => {
return schema.validate(data);
};
},
async handler(
req: FastifyRequest<{ Params: BSSpaceDeleteParamsDto }>,
rep: FastifyReply,
Expand Down
38 changes: 38 additions & 0 deletions backend/src/api/eam/eam.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } from '~/hooks/hooks';
import { EAMError } from '~/exceptions/exceptions';
Expand Down Expand Up @@ -111,6 +112,7 @@ const initEamApi: FastifyPluginAsync<Options> = async (fastify, opts) => {
url: `${EAMApiPath.GROUPS}${GroupsApiPath.$ID}`,
schema: {
body: groupUpdateValidationSchema,
params: UUIDValidationSchema,
},
validatorCompiler({
schema,
Expand Down Expand Up @@ -138,6 +140,18 @@ const initEamApi: FastifyPluginAsync<Options> = async (fastify, opts) => {
fastify.route({
method: HttpMethod.GET,
url: `${EAMApiPath.GROUPS}${GroupsApiPath.$ID}`,
schema: {
params: UUIDValidationSchema,
},
validatorCompiler({
schema,
}: FastifyRouteSchemaDef<typeof UUIDValidationSchema>) {
return (
data: EamGroupGetByIdRequestDto,
): ReturnType<typeof UUIDValidationSchema['validate']> => {
return schema.validate(data);
};
},
async handler(
req: FastifyRequest<{
Querystring: EamGroupGetByIdRequestDto;
Expand Down Expand Up @@ -191,6 +205,18 @@ const initEamApi: FastifyPluginAsync<Options> = async (fastify, opts) => {
fastify.route({
method: HttpMethod.DELETE,
url: `${EAMApiPath.GROUPS}${GroupsApiPath.$ID}`,
schema: {
params: UUIDValidationSchema,
},
validatorCompiler({
schema,
}: FastifyRouteSchemaDef<typeof UUIDValidationSchema>) {
return (
data: EAMGroupDeleteParamsDto,
): ReturnType<typeof UUIDValidationSchema['validate']> => {
return schema.validate(data);
};
},
async handler(
req: FastifyRequest<{ Params: EAMGroupDeleteParamsDto }>,
rep,
Expand All @@ -206,6 +232,18 @@ const initEamApi: FastifyPluginAsync<Options> = async (fastify, opts) => {
fastify.route({
method: HttpMethod.DELETE,
url: `${EAMApiPath.WORKERS}${WorkersApiPath.$ID}`,
schema: {
params: UUIDValidationSchema,
},
validatorCompiler({
schema,
}: FastifyRouteSchemaDef<typeof UUIDValidationSchema>) {
return (
data: EAMWorkerDeleteRequestDto,
): ReturnType<typeof UUIDValidationSchema['validate']> => {
return schema.validate(data);
};
},
async handler(
req: FastifyRequest<{ Params: EAMWorkerDeleteRequestDto }>,
rep,
Expand Down
26 changes: 26 additions & 0 deletions backend/src/api/sc/sc.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 } from '~/hooks/hooks';

Expand Down Expand Up @@ -56,6 +57,18 @@ const initScApi: FastifyPluginAsync<Options> = async (fastify, opts) => {
method: HttpMethod.GET,
url: `${SCApiPath.SSH_KEYS}${SshKeysApiPath.$ID}`,
preHandler: checkHasPermissionsHook(Permission.MANAGE_SC),
schema: {
params: UUIDValidationSchema,
},
validatorCompiler({
schema,
}: FastifyRouteSchemaDef<typeof UUIDValidationSchema>) {
return (
data: SCSshKeyGetByIdParamsDto,
): ReturnType<typeof UUIDValidationSchema['validate']> => {
return schema.validate(data);
};
},
async handler(
req: FastifyRequest<{
Params: SCSshKeyGetByIdParamsDto;
Expand Down Expand Up @@ -89,6 +102,18 @@ const initScApi: FastifyPluginAsync<Options> = async (fastify, opts) => {
method: HttpMethod.DELETE,
url: `${SCApiPath.INSTANCES}${InstancesApiPath.$ID}`,
preHandler: checkHasPermissionsHook(Permission.MANAGE_SC),
schema: {
params: UUIDValidationSchema,
},
validatorCompiler({
schema,
}: FastifyRouteSchemaDef<typeof UUIDValidationSchema>) {
return (
data: SCInstanceDeleteParamsDto,
): ReturnType<typeof UUIDValidationSchema['validate']> => {
return schema.validate(data);
};
},
async handler(
req: FastifyRequest<{
Params: SCInstanceDeleteParamsDto;
Expand All @@ -106,6 +131,7 @@ const initScApi: FastifyPluginAsync<Options> = async (fastify, opts) => {
preHandler: checkHasPermissionsHook(Permission.MANAGE_SC),
schema: {
body: scInstanceUpdateValidationSchema,
params: UUIDValidationSchema,
},
validatorCompiler({
schema,
Expand Down
53 changes: 52 additions & 1 deletion backend/src/api/slc/slc.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
slcFunction as slcFunctionServ,
token as tokenServ,
} 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,
Expand Down Expand Up @@ -74,6 +77,18 @@ const initSLCApi: FastifyPluginAsync<Options> = async (fastify, opts) => {
method: HttpMethod.POST,
url: `${SLCApiPath.SLC_FUNCTIONS}${SLCFunctionApiPath.$ID}`,
preHandler: checkHasPermissionsHook(Permission.MANAGE_SLC),
schema: {
params: UUIDValidationSchema,
},
validatorCompiler({
schema,
}: FastifyRouteSchemaDef<typeof UUIDValidationSchema>) {
return (
data: SLCFunctionLoadParamsDto,
): ReturnType<typeof UUIDValidationSchema['validate']> => {
return schema.validate(data);
};
},
async handler(
req: FastifyRequest<{
Params: SLCFunctionRunParamsDto;
Expand Down Expand Up @@ -124,6 +139,18 @@ const initSLCApi: FastifyPluginAsync<Options> = async (fastify, opts) => {
method: HttpMethod.GET,
url: `${SLCApiPath.SLC_FUNCTIONS}${SLCFunctionApiPath.$ID}`,
bohdanshcherba marked this conversation as resolved.
Show resolved Hide resolved
preHandler: checkHasPermissionsHook(Permission.MANAGE_SLC),
schema: {
params: UUIDValidationSchema,
},
validatorCompiler({
schema,
}: FastifyRouteSchemaDef<typeof UUIDValidationSchema>) {
return (
data: SLCFunctionLoadParamsDto,
): ReturnType<typeof UUIDValidationSchema['validate']> => {
return schema.validate(data);
};
},
async handler(
req: FastifyRequest<{
Params: SLCFunctionLoadParamsDto;
Expand All @@ -144,6 +171,18 @@ const initSLCApi: FastifyPluginAsync<Options> = async (fastify, opts) => {
method: HttpMethod.DELETE,
url: `${SLCApiPath.SLC_FUNCTIONS}${SLCFunctionApiPath.$ID}`,
preHandler: checkHasPermissionsHook(Permission.MANAGE_SLC),
schema: {
params: UUIDValidationSchema,
},
validatorCompiler({
schema,
}: FastifyRouteSchemaDef<typeof UUIDValidationSchema>) {
return (
data: SLCFunctionLoadParamsDto,
): ReturnType<typeof UUIDValidationSchema['validate']> => {
return schema.validate(data);
};
},
async handler(
req: FastifyRequest<{ Params: SLCFunctionDeleteParamsDto }>,
rep: FastifyReply,
Expand All @@ -170,6 +209,18 @@ const initSLCApi: FastifyPluginAsync<Options> = async (fastify, opts) => {
method: HttpMethod.PUT,
url: `${SLCApiPath.SLC_FUNCTIONS}${SLCFunctionApiPath.$ID}`,
preHandler: checkHasPermissionsHook(Permission.MANAGE_SLC),
schema: {
params: UUIDValidationSchema,
},
validatorCompiler({
schema,
}: FastifyRouteSchemaDef<typeof UUIDValidationSchema>) {
return (
data: SLCFunctionLoadParamsDto,
): ReturnType<typeof UUIDValidationSchema['validate']> => {
return schema.validate(data);
};
},
async handler(
req: FastifyRequest<{
Params: SLCFunctionUpdateParamsDto;
Expand Down
1 change: 1 addition & 0 deletions backend/src/validation-schemas/uuid/uuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { UUID } from 'bws-shared/validation/validation-schemas';
1 change: 1 addition & 0 deletions backend/src/validation-schemas/validation-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
2 changes: 2 additions & 0 deletions shared/src/common/enums/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export {
SCInstanceValidationMessage,
SLCFunctionValidationMessage,
SLCFunctionValidationRule,
UUIDValidationRule,
UUIDValidationMessage,
} from './validation/validation';
export { Permission } from './permissions/permissions';
export { UserRole } from './roles/roles';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const UUIDValidationMessage = {
UUID_PATTERN: 'uuid is invalid',
UUID_REQUIRE: 'uuid is required',
} as const;

export { UUIDValidationMessage };
Original file line number Diff line number Diff line change
@@ -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 };
2 changes: 2 additions & 0 deletions shared/src/common/enums/validation/uuid/uuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { UUIDValidationMessage } from './uuid-validation-massage';
export { UUIDValidationRule } from './uuid-validation-rule.enum';
1 change: 1 addition & 0 deletions shared/src/common/enums/validation/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export {
SLCFunctionValidationMessage,
SLCFunctionValidationRule,
} from './slc-function/slc-function';
export { UUIDValidationMessage, UUIDValidationRule } from './uuid/uuid';
19 changes: 19 additions & 0 deletions shared/src/validation/uuid/uuid.validation-shema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as Joi from 'joi';
import { getNameOf } from '~/helpers/helpers';
import { SLCFunctionLoadParamsDto } from '~/common/types/types';
import {
UUIDValidationRule,
UUIDValidationMessage,
} from '~/common/enums/enums';

const UUID = Joi.object({
[getNameOf<SLCFunctionLoadParamsDto>('id')]: Joi.string()
bohdanshcherba marked this conversation as resolved.
Show resolved Hide resolved
.required()
.regex(UUIDValidationRule.UUID_PATTERN)
.messages({
'string.empty': UUIDValidationMessage.UUID_REQUIRE,
'string.pattern.base': UUIDValidationMessage.UUID_PATTERN,
}),
});

export { UUID };
1 change: 1 addition & 0 deletions shared/src/validation/validation-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';