Search code examples
javascripttypescriptnest

Nest creating dto with class validator is not working


I have a simlple request that i am trying to build using class validator in nestjs but it is failing with error ` one of the parameter is missing in the request payload" any help here what is missing in creating new schema ?

main.model.ts

 export class IrxOrder {
    @IsString()
    readonly lineOfBusiness: string;
    @IsString()
    readonly memberIdentifier: string
}

export class ProcessRxOrderInfo {
    @IsString()
   emailAddress: string;
    @IsString()
    readonly sendCustomerEmail: string;
    @IsObject()
    @ValidateNested() @Type(() => IrxOrder)
    readonly rxOrder: IrxOrder[];
}



export default class CreateSubmitRequest {
    @IsObject()
    @ValidateNested() @Type(() => ProcessRxOrderInfo)
    readonly processRxOrderInfo: ProcessRxOrderInfo

}

Request:

{
  "processRxOrderInfo": {
    "emailAddress": "test@gmail.com",
    "sendCustomerEmail": "Y",
    "rxOrder": [
      {
        "lineOfBusiness": "mail",
        "memberIdentifier": "testJ"
      }
    ]
  }
}

Solution

  • In the class ProcessRxOrderInfo

    @IsObject()
    @ValidateNested() 
    @Type(() => IrxOrder)
    readonly rxOrder: IrxOrder[];
    

    The validator @IsObject would result in the error your mentioned since it's actually supposed to be an array of objects rather than single object. I copied your code and simply removing it got the job done. You may choose to add @IsArray() validator here