-
Notifications
You must be signed in to change notification settings - Fork 5
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
belindas-closet-nestjs_sprint8_remove-unrelated-fields #134
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is correct because when I take away the productSizeShoe field, it saves as an empty array:
It's a little difficult to test further since the UI add product functionality is not currently working. I believe the goal was to be able to add a product and not get default numerical values written to the database for productSize's that don't apply to certain productType's.
Correct me if I'm wrong @keiffer213 but this is for unrelated product fields that aren't displayed in the UI and are still setting default values? For example, you can have productType as Shoes, not enter a productSize but it will still show up as size 'S' in the database.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check the following suggestions, I think it will fix some of the issues you are encountering. One behavior I noticed is @ValidateIf will run first (decorators are checked sequentially from top to bottom) and if it fails, the rest of the decorators will not run and validate.
Your code should work as intended once you make the changes, Great Job!
@@ -12,18 +12,22 @@ export class CreateProductDto { | |||
@IsEnum(['MALE','FEMALE', 'NON_BINARY']) | |||
readonly productGender: []; | |||
|
|||
@ValidateIf((object, value) => object.productType === 'Shoes') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove value
for each time not in use in @ValidateIf decorator
@@ -12,18 +12,22 @@ export class CreateProductDto { | |||
@IsEnum(['MALE','FEMALE', 'NON_BINARY']) | |||
readonly productGender: []; | |||
|
|||
@ValidateIf((object, value) => object.productType === 'Shoes') | |||
@IsOptional() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove @IsOptional decorator
line 15 validates if object.productType matches with 'Shoes'. If it does match, then the next decorator is checked. @IsOptional() will create an entry in the database with an empty array if object.productSizeShoe is empty or is not present in the object payload. Removing @IsOptional decorator will allow @isEnum decorator to run so value of productSizeShoe's value is one of the enum values.
Example
@alasali1 please address the issue requested by Isaac |
attempt to remove unrelated field
For this I tried to validate all of the fields so that you can only include the fields that apply to the productType.
I am still able to post an object with unrelated fields on postman though. So this is not fixed though this is my attempt.
Would appreciate some feedback.