Skip to content

Instantly share code, notes, and snippets.

@kitihounel
Created May 15, 2024 19:27
Show Gist options
  • Save kitihounel/8b0b0b677e65144b740cc91b51d1e015 to your computer and use it in GitHub Desktop.
Save kitihounel/8b0b0b677e65144b740cc91b51d1e015 to your computer and use it in GitHub Desktop.
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