I've defined a Survey
model:
@Table
export class Survey extends Model {
@PrimaryKey
@Default(DataType.UUIDV4)
@Column(DataType.UUID)
id: string;
@Column({ type: DataType.STRING, allowNull: false })
name: string;
@Column({ type: DataType.STRING, defaultValue: '', allowNull: false })
description: string;
@Column({ type: DataType.BOOLEAN, defaultValue: false, allowNull: false })
isActive: boolean;
}
When inserting a survey, only the name
field is mandatory, while the rest have default values.
Below is my CreateSurveyDto
:
import { IsString } from 'class-validator';
export class CreateSurveyDto {
@IsString()
name: string;
@IsString()
description?: string;
}
The issue arises in my service when passing the dto as an argument to the create function:
public async createSurvey(createSurveyDto: CreateSurveyDto): Promise<Survey> {
return await this.surveyModel.create(createSurveyDto);
}
This triggers an error message:
Argument of type 'CreateSurveyDto' is not assignable to parameter of type 'Optional<any, string>'.
Type 'CreateSurveyDto' is not assignable to type 'Omit<any, string>'.
Index signature for type 'number' is missing in type 'CreateSurveyDto'
What could be causing this issue? Should I include all fields in the model and make some optional, even though only the name
field is required in the dto for creating a survey?