Search code examples
mysqltypescriptnestjstypeorm

How to set boolean validation in typeorm and nest.js


I developed apps by nest.js and typeorm. The entity is validated like following.

I have some question about boolean validation.

event.dto.ts

export class EventRequest {
    @IsInt()  
    id: number;
    
    @IsInt()
    userId: number;
    
    @IsString()
    title: string;
      
    @IsDateString()
    date: Date;
    
    @IsBoolean()
    islastDate: boolean;

event.entity.ts

 @Entity('events')
  export class Event extends BaseEntity {
    @PrimaryGeneratedColumn('increment', { type: 'int' })
    id: number;
  
    @ManyToOne(type => User, user => user.events)
    @JoinColumn()
    readonly user?: User;
    userId: User;

    @Column('varchar')
    title: string;
  
    @Column('date')
    date: Date;
  
    @Column('bool')
    isLastdate: boolean;

I send following request to server

{
"id":0,
"userId":1,
"title":"mytest",
"date":"2011-10-05T14:48:00.000Z",
"isLastdate":0,
"beginTime":"2011-10-05T14:48:00.000Z",
"endTime":"2011-10-05T14:48:00.000Z",
"place":"Tokyo",
"labelCd":1,
"detail":"test"
}

The following error was returned. What should I set to boolean value ?

{
    "statusCode": 400,
    "message": [
        "islastDate must be a boolean value"
    ],
    "error": "Bad Request"
}

Here is my DB generated above entity.


mysql> desc events;
+------------+--------------+------+-----+----------------------+----------------+
| Field      | Type         | Null | Key | Default              | Extra          |
+------------+--------------+------+-----+----------------------+----------------+
| id         | int(11)      | NO   | PRI | NULL                 | auto_increment |
| title      | varchar(255) | NO   |     | NULL                 |                |
| date       | date         | NO   |     | NULL                 |                |
| place      | varchar(255) | NO   |     | NULL                 |                |
| detail     | varchar(255) | NO   |     | NULL                 |                |
| createdAt  | datetime(6)  | NO   |     | CURRENT_TIMESTAMP(6) |                |
| updatedAt  | datetime(6)  | NO   |     | CURRENT_TIMESTAMP(6) |                |
| deletedAt  | datetime(6)  | YES  |     | NULL                 |                |
| beginTime  | time         | NO   |     | NULL                 |                |
| endTime    | time         | NO   |     | NULL                 |                |
| labelCd    | int(11)      | NO   |     | NULL                 |                |
| userId     | int(11)      | YES  | MUL | NULL                 |                |
| isLastdate | tinyint(4)   | NO   |     | NULL                 |                |
+------------+--------------+------+-----+----------------------+----------------+

Are there something wrong with my understand ?

What is boolean value ?

Thanks


Solution

  • There is a typo in your request data, you have islastDate: boolean; in your DTO and isLastdate: boolean; in your entity file. Change one to match the other and you should be fine.