How to Allow null or Empty String in class-validator for Specific Fields? – Class-validator

by
Maya Patel
class-validator dto nestjs postgresql prisma

Quick Fix: Use conditional validation decorators along with IsOptional to allow specific fields to be null or empty while maintaining their validations, provided by class-validator in NestJS.

The Problem:

While using class-validator in a NestJS application, I’m facing a challenge with specific fields like "sample_result" and "sample_comment". I need to set these fields to null or an empty string ("") under certain circumstances. However, when I provide such values, I encounter validation errors, despite using decorators like @IsOptional() and @IsNumber() or @IsString(). How can I correctly configure class-validator to allow null or empty strings for these specific fields while ensuring that non-empty values still adhere to their respective validation constraints?

The Solutions:

Solution 1: Conditional Validation Decorators

To enable null or empty string for specific fields while ensuring validations when values are provided:

  1. Import necessary decorators:
import { ApiProperty } from '@nestjs/swagger';
import { IsNumber, IsString, IsOptional, ValidateIf } from 'class-validator';
  1. Modified class:
export class YourDto {
  @ApiProperty({ required: false })
  @IsNumber()
  @ValidateIf((obj) => obj.sample_result !== null && obj.sample_result !== '')
  sample_result?: number | null;

  @ApiProperty({ required: false })
  @IsString()
  @ValidateIf((obj) => obj.sample_comment !== null && obj.sample_comment !== '')
  sample_comment?: string | null;
}
  • ValidateIf decorates sample_result and sample_comment.
  • Validation only applied if fields aren’t null or empty strings.
  1. Allow null with | null type annotation:
sample_result?: number | null;
sample_comment?: string | null;

This allows null or empty string while ensuring validations when provided.

Solution 2: Set the nullable Flag to True

To allow null or an empty string in class-validator for specific fields, you can set the nullable flag to true for those fields. The nullable flag indicates whether the field is allowed to be null. When you set nullable to true, the field will be allowed to be null and empty string. Here’s how you can do it:

  @ApiProperty({ required: false, nullable: true })
  @IsNumber()
  @IsOptional()
  sample_result?: number;

  @ApiProperty({ required: false, nullable: true })
  @IsString()
  @IsOptional()
  sample_comment?: string;

With this configuration, the fields sample_result and sample_comment will be allowed to be null or an empty string. However, if a value is provided for these fields, it will be validated according to their respective validation rules (i.e., sample_result must be a number and sample_comment must be a string). This will allow you to update or set these fields to null or an empty string under certain circumstances while ensuring that the data integrity is maintained when values are provided.

Q&A

How to handle null/empty string in Class-Validator?

Use conditional validation decorators (ValidateIf) based on field values.

What about null values?

Use nullable: true with the @ApiProperty decorator.

Video Explanation:

The following video, titled "How to Validate Forms with Clean Architecture (You're Doing it ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Learning clean architecture can be tough. In this video I'll show you how you can solve the very common problem of validating forms by ...