Created
May 15, 2024 19:27
-
-
Save kitihounel/8b0b0b677e65144b740cc91b51d1e015 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common' | |
@Injectable() | |
export class ParsePositiveIntPipe implements PipeTransform<any, number | undefined | null> { | |
transform(value) { | |
if (value === null || value === undefined) { | |
return value | |
} | |
const n = parseInt(value) | |
if (isNaN(n) || n <= 0) { | |
throw new BadRequestException() | |
} | |
return n | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment