gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
example ParseUUIDPipe in nestjs
- User
example ParseUUIDPipe in nestjs
- Assistant
Here's an example implementation of a
ParseUUIDPipein NestJS:import { Injectable, PipeTransform, BadRequestException } from '@nestjs/common'; import { isUUID } from 'class-validator'; @Injectable() export class ParseUUIDPipe implements PipeTransform<string, string> { transform(value: string): string { if (!isUUID(value)) { throw new BadRequestException('Invalid UUID'); } return value; } }The
ParseUUIDPipeclass is responsible for transforming a value into a UUID string, or throwing an error if the value is not a valid UUID. Thetransformmethod is called by NestJS during request processing, and is passed the value being processed. TheisUUIDfunction from theclass-validatorlibrary is used to check whether the value is a valid UUID; if it is not, aBadRequestExceptionis thrown. Otherwise, the method returns the original value as a string.