From 74111324db5cb55292dfbd9024686fdc5abd7932 Mon Sep 17 00:00:00 2001 From: raczu Date: Mon, 10 Jun 2024 00:36:13 +0200 Subject: [PATCH] feat: add regex validation for dtos --- .../common/__tests__/models/EventDto.test.ts | 2 +- .../common/__tests__/models/UserDto.test.ts | 2 +- .../packages/common/models/AddressDto.ts | 25 +++++++++++++++---- .../packages/common/models/CommentDto.ts | 2 +- .../packages/common/models/EventDto.ts | 15 ++++++++--- .../packages/common/models/InterestDto.ts | 5 +++- .../packages/common/models/ParameterDto.ts | 10 ++++++-- .../packages/common/models/TagDto.ts | 5 +++- .../packages/common/models/UserDto.ts | 22 ++++++++++++---- 9 files changed, 68 insertions(+), 20 deletions(-) diff --git a/Client/reasn-client/packages/common/__tests__/models/EventDto.test.ts b/Client/reasn-client/packages/common/__tests__/models/EventDto.test.ts index 27755f37..177960cd 100644 --- a/Client/reasn-client/packages/common/__tests__/models/EventDto.test.ts +++ b/Client/reasn-client/packages/common/__tests__/models/EventDto.test.ts @@ -14,7 +14,7 @@ describe("EventDto", () => { const updatedAt = new Date("2022-01-01"); const slug = "test-event"; const status = EventStatus.APPROVED; - const tags: TagDto[] = [{ Name: "Tag 1" }, { Name: "Tag 2" }]; + const tags: TagDto[] = [{ Name: "first tag" }, { Name: "second tag" }]; describe("fromJson", () => { it("should create an instance of EventDto from JSON string", () => { diff --git a/Client/reasn-client/packages/common/__tests__/models/UserDto.test.ts b/Client/reasn-client/packages/common/__tests__/models/UserDto.test.ts index 3e15c22f..e0ed639d 100644 --- a/Client/reasn-client/packages/common/__tests__/models/UserDto.test.ts +++ b/Client/reasn-client/packages/common/__tests__/models/UserDto.test.ts @@ -8,7 +8,7 @@ describe("UserDto", () => { const name = "John"; const surname = "Doe"; const email = "john.doe@example.com"; - const phone = "+1234567890"; + const phone = "+48 1234567890"; const role = UserRole.USER; const addressId = 2; const interests: UserInterestDto[] = [ diff --git a/Client/reasn-client/packages/common/models/AddressDto.ts b/Client/reasn-client/packages/common/models/AddressDto.ts index 06b45a9d..0827e825 100644 --- a/Client/reasn-client/packages/common/models/AddressDto.ts +++ b/Client/reasn-client/packages/common/models/AddressDto.ts @@ -2,11 +2,26 @@ import ModelMappingError from "@reasn/common/errors/ModelMappingError"; import { z } from "zod"; export const AddressDtoSchema = z.object({ - Country: z.string(), - City: z.string(), - Street: z.string(), - State: z.string(), - ZipCode: z.string().nullable(), + Country: z + .string() + .max(64) + .regex(/^\p{Lu}[\p{L}\s'-]*(? value === null || /^[\p{L}\d\s-]{3,}$/u.test(value)), }); export type AddressDto = z.infer; diff --git a/Client/reasn-client/packages/common/models/CommentDto.ts b/Client/reasn-client/packages/common/models/CommentDto.ts index ebd3780f..4e1f8a87 100644 --- a/Client/reasn-client/packages/common/models/CommentDto.ts +++ b/Client/reasn-client/packages/common/models/CommentDto.ts @@ -3,7 +3,7 @@ import { z } from "zod"; export const CommentDtoSchema = z.object({ EventId: z.number(), - Content: z.string(), + Content: z.string().max(1024), CreatedAt: z .string() .datetime({ offset: true }) diff --git a/Client/reasn-client/packages/common/models/EventDto.ts b/Client/reasn-client/packages/common/models/EventDto.ts index d16176a8..8c360bdd 100644 --- a/Client/reasn-client/packages/common/models/EventDto.ts +++ b/Client/reasn-client/packages/common/models/EventDto.ts @@ -4,9 +4,9 @@ import { EventStatus } from "@reasn/common/enums/modelsEnums"; import { z } from "zod"; export const EventDtoSchema = z.object({ - Name: z.string(), + Name: z.string().max(64), AddressId: z.number(), - Description: z.string(), + Description: z.string().max(4048), OrganizerId: z.number(), StartAt: z .string() @@ -28,8 +28,17 @@ export const EventDtoSchema = z.object({ .datetime({ offset: true }) .or(z.date()) .transform((arg) => new Date(arg)), - Slug: z.string().nullable(), + Slug: z + .string() + .nullable() + .refine( + (value) => + value === null || + (value.length <= 128 && /^[\p{L}\d]+[\p{L}\d-]*$/u.test(value)), + ), + Status: z.nativeEnum(EventStatus), + Tags: z.array(TagDtoSchema), }); diff --git a/Client/reasn-client/packages/common/models/InterestDto.ts b/Client/reasn-client/packages/common/models/InterestDto.ts index ca9b2f41..9576f0ac 100644 --- a/Client/reasn-client/packages/common/models/InterestDto.ts +++ b/Client/reasn-client/packages/common/models/InterestDto.ts @@ -2,7 +2,10 @@ import ModelMappingError from "@reasn/common/errors/ModelMappingError"; import { z } from "zod"; export const InterestDtoSchema = z.object({ - Name: z.string(), + Name: z + .string() + .max(32) + .regex(/^\p{Lu}\p{Ll}+(?:\s\p{L}+)*$/u), }); export type InterestDto = z.infer; diff --git a/Client/reasn-client/packages/common/models/ParameterDto.ts b/Client/reasn-client/packages/common/models/ParameterDto.ts index 0a48d989..f35b0b7b 100644 --- a/Client/reasn-client/packages/common/models/ParameterDto.ts +++ b/Client/reasn-client/packages/common/models/ParameterDto.ts @@ -2,8 +2,14 @@ import ModelMappingError from "@reasn/common/errors/ModelMappingError"; import { z } from "zod"; export const ParameterDtoSchema = z.object({ - Key: z.string(), - Value: z.string(), + Key: z + .string() + .max(32) + .regex(/^\p{L}+(?:\s\p{L}+)*$/u), + Value: z + .string() + .max(64) + .regex(/^[\p{L}\d]+(?:\s[\p{L}\d]+)*$/u), }); export type ParameterDto = z.infer; diff --git a/Client/reasn-client/packages/common/models/TagDto.ts b/Client/reasn-client/packages/common/models/TagDto.ts index e9a074b6..3b129d84 100644 --- a/Client/reasn-client/packages/common/models/TagDto.ts +++ b/Client/reasn-client/packages/common/models/TagDto.ts @@ -2,7 +2,10 @@ import ModelMappingError from "@reasn/common/errors/ModelMappingError"; import { z } from "zod"; export const TagDtoSchema = z.object({ - Name: z.string(), + Name: z + .string() + .max(64) + .regex(/^\p{L}+(?:\s\p{L}+)*$/u), }); export type TagDto = z.infer; diff --git a/Client/reasn-client/packages/common/models/UserDto.ts b/Client/reasn-client/packages/common/models/UserDto.ts index cfd1536b..e6f57ed0 100644 --- a/Client/reasn-client/packages/common/models/UserDto.ts +++ b/Client/reasn-client/packages/common/models/UserDto.ts @@ -4,11 +4,23 @@ import { UserRole } from "@reasn/common/enums/modelsEnums"; import { z } from "zod"; export const UserDtoSchema = z.object({ - Username: z.string(), - Name: z.string(), - Surname: z.string(), - Email: z.string(), - Phone: z.string().nullable(), + Username: z + .string() + .max(64) + .regex(/^[\p{L}\d._%+-]{4,}$/u), + Name: z + .string() + .max(64) + .regex(/^\p{Lu}[\p{Ll}\s'-]+$/u), + Surname: z + .string() + .max(64) + .regex(/^\p{L}+(?:[\s'-]\p{L}+)*$/u), + Email: z.string().email(), + Phone: z + .string() + .nullable() + .refine((value) => value === null || /^\+\d{1,3}\s\d{1,15}$/.test(value)), Role: z.nativeEnum(UserRole), AddressId: z.number(), Intrests: z.array(UserInterestDtoSchema),