class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

fix: There might be a bug in IsISO8601/IsDateString in strict mode

Open GCastilho opened this issue 3 years ago • 4 comments

Description

According to the type docs, when using IsDateString({ strict: true}): "If strict is true, performs additional checks for valid dates, e.g. invalidates dates like 2009-02-29". But those dates appear to be passing the validation in my app

Minimal code-snippet showcasing the problem So I created a repo for bug reproduction here. In essence:

class ValidatorTest {
	@IsDateString({ strict: true })
	isoDate: string
}

const instance = new ValidatorTest()
instance.isoDate = '2009-02-29'

await assert.rejects(() => validateOrReject(instance), 'Class successfully validated') // <<<< Fails

Expected behavior

According to the docs, a date as "2009-02-29" should fail when using isoDate validation with strict = true; (taken directly from the docs)

Actual behavior

The validator passes

Version

class-validator: 0.13.1 node: 14

GCastilho avatar May 05 '21 14:05 GCastilho

Same issue here. Seems to be an issue with class-validator itself, not validator.js it's using. Following code works as expected with validator.js:

const validator = require('validator');

validator.isISO8601('2009-02-29', {strict: true});

https://runkit.com/hakimio/60aca6995be746001af0ca45

hakimio avatar May 25 '21 07:05 hakimio

Ok, found the bug here. Unlike IsISO8601, IsDateString doesn't send the options object to the validator.

Line 27 should be changed to the following in IsDateString.ts:

validate: (value, args): boolean => isDateString(value, args.constraints[0])

Current workaround would be to use IsISO8601 instead of IsDateString.

@NoNameProvided do you want me to create a new PR or can you just fix this?

hakimio avatar May 26 '21 12:05 hakimio

PR #1097

hakimio avatar May 26 '21 12:05 hakimio

Related: https://github.com/validatorjs/validator.js/issues/2003

thenbe avatar Jul 30 '22 01:07 thenbe

Merged in https://github.com/typestack/class-validator/pull/1720.

NoNameProvided avatar Nov 20 '22 15:11 NoNameProvided

This feature/fix has been released in 0.14.0.

NoNameProvided avatar Dec 09 '22 18:12 NoNameProvided

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Jan 10 '23 01:01 github-actions[bot]