whefter / joi-class-decorators

MIT License
7 stars 1 forks source link

How can I validate the schema as array? #2

Open darvesh opened 6 days ago

darvesh commented 6 days ago

For example; I have this schema

@JoiSchemaOptions({})
class ThrillerDto {
  @JoiSchema(Joi.number().required())
  thrill!: number;
}

I want to receive an array in the body of ThrillerDto

@Injectable()
class Thriller{
  createBulk(
    @Body() body: ThrillerDto //array 
  ){
     //create thriller 
  }
}
darvesh commented 6 days ago

I have tried

@JoiSchemaOptions({})
@JoiSchemaCustomization((schema) => joi.array().items(schema))
class ThrillerDto {
  @JoiSchema(Joi.number().required())
  thrill!: number;
}

but it results in a type error

Type 'ArraySchema<any[]>' is missing the following properties from type 'ObjectSchema<any>': and, append, assert, instance, and 13 more.ts(2740)
defs.d.ts(9, 48): The expected type comes from the return type of this signature.
whefter commented 6 days ago

@JoiSchemaCustomization() seems the way to go here, have you adjusted the body definition?

@Injectable()
class Thriller{
  createBulk(
    @Body() body: ThrillerDto[] // <--- declared as array
  ){
     //create thriller 
  }
}
darvesh commented 5 days ago

Yeah, I have. I think the return type needs a fix in JoiSchemaCustomization