@IsOptional()
@Transform(({ value }) => (value ? new Date(value) : null))
@IsDate()
doj: Date;
The above code works fine when doj is undefined. It has the same behavior when doj is also null.
However, I want the validation to pass when it is undefined and fail if null. How do I achieve this with class validator?
EDIT:
Based on the accepted answer, I made a function to validate and transform dates. This is for nestjs with graphql btw.
import { BadRequestException } from '@nestjs/common';
import { TransformFnParams } from 'class-transformer';
import * as moment from 'moment';
export const dateTransformValidate = (
params: TransformFnParams & { nullable: boolean }
) => {
const date = params.value;
if (date == null) {
if (params.nullable === false) {
throw new BadRequestException(`Cannot remove ${params.key}.`);
}
return date;
}
const m = moment(date);
if (m.isValid()) return date;
throw new BadRequestException('Invalid date.');
};
Usage:
// allow undefined but not nulls
@Field({ nullable: true })
@IsOptional()
@Transform((params) => dateTransformValidate({ ...params, nullable: false }))
dob: Date;
// allow undefined and null
@Field({ nullable: true })
@IsOptional()
@Transform(dateTransformValidate)
doj: Date;
Just check out if doj is null and throw an Error
Sandbox