I am using Nestjs Crud to build certain CRUD endpoints. In one of the PATCH/POST endpoints, I am trying to validate dates that I am getting from the Frontend (validFrom, ValidTo).
Conditions:
This is the code that is there:
public async validateDates(validFrom: string, validTo: string): Promise<void> {
if (moment(validTo).isSameOrBefore(moment().startOf('day'))) {
throw new BadRequestException('validTo date should be should be greater than todays date.');
} else if (moment(validFrom).isBefore(moment().startOf('day'))) {
throw new BadRequestException('validFrom date should not be in the past.');
} else if (moment(validFrom).isSameOrAfter(moment(validTo))) {
throw new BadRequestException('validFrom date should be less than validTo date.');
}
}
IF I try to enter validTo date as today's date, I get the error 'validFrom date should be less than validTo date', but ideally, I should be getting the error ''validTo date should be should be greater than todays date.'
Is there a better way to perform these validations?
What do you pass into the validFrom and validTo? If it contains time then you would need to do something like moment(validTo).startOf('day').isSameOrBefore(moment().startOf('day'))