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

Feature/event validation #19

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Feature/event validation #19

wants to merge 10 commits into from

Conversation

DankaMarci
Copy link
Collaborator

It's nearly done, but I just can't seem to figure it out how to make the prisma compatible with the dtos.

@balintking balintking linked an issue Jun 1, 2024 that may be closed by this pull request
4 tasks
@csiszaralex
Copy link
Member

The build is not works, please check it

@balintking
Copy link
Collaborator

Yeah we talked bout it, but didn't have time to to check the problem in detail. There's something with the CreateEventDto.

@csiszaralex
Copy link
Member

There are multiple problems:

  • You not installed the @nestjs/swagger, class-transformer and class-validator, but you use it
  • You receive CreateEventDto in event.service.ts create method, but prisma wait another type, please check it
  • The problem is same with update and UpdateEventDto

@csiszaralex
Copy link
Member

I resolved the first problem 😉

@IsOptional()
priority: Priority;

@IsEnum(Priority)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is incorrect

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should be @IsEnum(Status)

Comment on lines 44 to 63
@IsInt()
@IsNotEmpty()
categoryId: number;

@IsNotEmpty()
category: Category;

@IsInt()
@IsOptional()
ownerUserId: number;

@IsOptional()
ownerUser: User;

@IsInt()
@IsOptional()
ownerGroupId: number;

@IsOptional()
ownerGroup: Group;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not create a field for relations in the entity schema. This class should represent one record in the database. Prisma hides this from you, but there are no ownerGroup, ownerUser or category fields on the Event table (only categoryId, ownerUserId and ownerGroupId), so you should remove those from this class. When an event is created, the user, group and category likely already exists, and prisma will be able to link the new event record to those records with the id (foreign key) fields

Copy link
Member

@csiszaralex csiszaralex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I marked some problems, please fix them and it will be perfect 😃

"@prisma/client": "^5.13.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
<<<<<<< HEAD
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please resolve the merge conflict (just delete all between <<< and >>> 76...43).

@IsOptional()
priority: Priority;

@IsEnum(Priority)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should be @IsEnum(Status)

Copy link
Collaborator

@balintking balintking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My observations..

@IsOptional()
categoryId: number;

category: Category;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need category

apps/backend/src/event/entities/event.entity.ts Outdated Show resolved Hide resolved
}
export class Event {
@IsInt()
@IsOptional()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id should not be optional as I see


@IsString()
@IsOptional()
color: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when optional we could declare as color?: string, so that it is nullable


import { Event } from '../entities/event.entity';

export class CreateEventDto extends OmitType(Event, ['id', 'category', 'ownerGroupId', 'ownerUserId']) {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say we need everything upon creation, but maybe I'm wrong.

Copy link
Collaborator

@balintking balintking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, sorry for the little late. Reviewed your code, please resolve those changes quickly and we can merge this branch. I tried to explain what you might misunderstood about the optional fields, but if it's still not clear, hit me up.
Yeah, and one more thing: why is the class category even in the event entity file?

name: string;

@IsString()
@IsOptional()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IsOptional() should stay as well, sorry if I wasn't clear

location: string;

@IsDate()
@IsOptional()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start date is not optional

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mixed this up with the default values. But this is the entity, this should perfectly mirror what's in the database schema. The default values come handy creating an object and not telling those values. Thats what create dto is for.

endTime: Date;

@IsEnum(Priority)
@IsOptional()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

priority is also not optional according to the schema

priority: Priority;

@IsEnum(Status)
@IsOptional()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not optional

categoryId: number;

@IsInt()
ownerUserId: number;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional

ownerUserId: number;

@IsInt()
ownerGroupId: number;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional


import { Event } from '../entities/event.entity';

export class CreateEventDto extends OmitType(Event, ['ownerGroupId', 'ownerUserId']) {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omit only id, because that's the only information the database comes up with alone. The owner we should specify.

@DankaMarci
Copy link
Collaborator Author

I altered the event_has_only_one_owner constraint in the prisma migrations. As I see an event's ownership is optional in the prisma schema therefore (if I'm right) it should be optional in the migration as well.

I also made the events' category field optional. I don't think it's a must for an event to have a category.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Class Validation to Event Entity
4 participants