#Use ParseArrayPipe for query params and validate the items as UUIDs

1 messages · Page 1 of 1 (latest)

ocean birch
#

Hello. I'm trying to load a list of UUIDs from the query params and I can't seem to find a solution to validate them as UUIDs. I can do this using class-validator directly in the controller method, but I was wondering if there is some way of using Nest to do this (like a DTO or pipe)

@Get()
  @HttpCode(200)
  async findAll(
    @Query('ids', new ParseArrayPipe({ items: String, separator: ',' }))
    ids: string[],
  ): Promise<Client[]> {
    return this.clientService.findByIds(ids);
  }

Being query params, I don't see how I can use a DTO for the items property because the request would be something like GET /?ids=30c06627-ecb7-4bb4-b579-014ebc02fafa,30c06627-ecb7-4bb4-b579-014ebc02fafa

atomic isle
#

There is one for UUID out of the box

#

Maybe you can adapt it for an array

atomic isle
ocean birch
ocean birch
#

Looks like if you do it like this /?id=30c06627-ecb7-4bb4-b579-014ebc02fafa&id=30c06627-ecb7-4bb4-b579-014ebc02fafa, NestJS automatically loads it up as an Array 🙂 This makes it super easy to use a DTO for validation because I can drop the ParseArrayPipe

atomic isle
#

Yeah, it would have been my suggestion