Skip to content

Instantly share code, notes, and snippets.

@kitihounel
Created May 15, 2024 19:30
Show Gist options
  • Save kitihounel/caae2490c93260b4aeb25d5683a5f6ac to your computer and use it in GitHub Desktop.
Save kitihounel/caae2490c93260b4aeb25d5683a5f6ac to your computer and use it in GitHub Desktop.
import { ArgumentMetadata, BadRequestException, Injectable, PipeTransform } from '@nestjs/common'
import { isUUID } from 'class-validator'
@Injectable()
export class ParseUuidListPipe implements PipeTransform {
transform(value: any, metadata: ArgumentMetadata): string[] {
if (typeof value !== 'string') {
throw new BadRequestException()
}
const parts = value.split(',')
for (const id of parts) {
if (!isUUID(id, 4)) {
throw new BadRequestException()
}
}
return parts
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment