Whenever I send a JavaScript Date() object through a put request to my Nest backend, I expect the date format to remain unchanged. However, it gets passed as a string, making it quite challenging to manage. I attempted using DTOs, but encountered an issue where the data is nested within an array. Can the DTO be applied to interfaces as well?
Here's what I currently have:
OpeningHoursInterface.ts
export interface OpeningHoursInterface {
dayOfWeek: number,
open: Date,
close: Date,
isOpen: boolean
}
DTO.ts
import { IsArray, IsObject, IsString, ValidateNested } from "class-validator";
import { CnmInterface } from "../interfaces/cnm.interface";
import { OpeningHoursInterface } from "../interfaces/openingHours.interface";
export class UpdateCnmDto {
@IsString()
_id: string;
@IsObject()
cnm: CnmInterface;
@IsArray()
@ValidateNested({ each: true })
openingHours: [OpeningHoursInterface]
}
controller.ts
@UseGuards(JwtAuthGuard)
@Put('')
@UsePipes(new ValidationPipe({ transform: true }))
async updateCnm(@Body() updateCnmDto: UpdateCnmDto, @GetUser() user: UserInterface) {
console.log("update cnm in controler", updateCnmDto)
return await this.storesService.updateCnm(updateCnmDto, user)
}
UPDATE 2021-03-28:
Thank you for your response!
I am encountering the following error in my console:
error TS1128: Declaration or statement expected.
Within VS Code, I see the ts(2693) error which mentions that "OpeningHoursInterface" is connected to a type but being used as a value.
I attempted to modify my tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
}
}
Your assistance is greatly appreciated.