File

lib/shared/pipes/not-empty.pipe.ts

Description

Pipe to ensure a value is not empty.

Index

Methods

Constructor

constructor(fieldName: string)
Parameters :
Name Type Optional
fieldName string No

Methods

transform
transform(value: any)
Parameters :
Name Type Optional
value any No
Returns : any
import { BadRequestException, Injectable, PipeTransform } from "@nestjs/common";

/**
 * Pipe to ensure a value is not empty.
 */
@Injectable()
export class NotEmptyPipe implements PipeTransform {
  constructor(private readonly fieldName: string) {}

  transform(value: any) {
    if (!value || Object.keys(value).length === 0) {
      throw new BadRequestException(`${this.fieldName} cannot be empty`);
    }
    return value;
  }
}

results matching ""

    No results matching ""