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

[RSN-56] Add regex validation for dtos #54

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("UserDto", () => {
const name = "John";
const surname = "Doe";
const email = "[email protected]";
const phone = "+1234567890";
const phone = "+48 1234567890";
const role = UserRole.USER;
const addressId = 2;
const interests: UserInterestDto[] = [
Expand Down
25 changes: 20 additions & 5 deletions Client/reasn-client/packages/common/models/AddressDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'-]*(?<![\s-])$/u),
City: z
.string()
.max(64)
.regex(/^\p{Lu}[\p{Ll}'.]+(?:[\s-][\p{L}'.]+)*$/u),
Street: z
.string()
.max(64)
.regex(/^[\p{L}\d\s\-/.,#']+(?<![-\s#,])$/u),
State: z
.string()
.max(64)
.regex(/^\p{Lu}\p{Ll}+(?:(\s|-)\p{L}+)*$/u),
ZipCode: z
.string()
.nullable()
.refine((value) => value === null || /^[\p{L}\d\s-]{3,}$/u.test(value)),
});

export type AddressDto = z.infer<typeof AddressDtoSchema>;
Expand Down
2 changes: 1 addition & 1 deletion Client/reasn-client/packages/common/models/CommentDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
15 changes: 12 additions & 3 deletions Client/reasn-client/packages/common/models/EventDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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),
});

Expand Down
5 changes: 4 additions & 1 deletion Client/reasn-client/packages/common/models/InterestDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof InterestDtoSchema>;
Expand Down
10 changes: 8 additions & 2 deletions Client/reasn-client/packages/common/models/ParameterDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof ParameterDtoSchema>;
Expand Down
5 changes: 4 additions & 1 deletion Client/reasn-client/packages/common/models/TagDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof TagDtoSchema>;
Expand Down
22 changes: 17 additions & 5 deletions Client/reasn-client/packages/common/models/UserDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading