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

fix: Misleading IsMilitaryTime default error message and optional : (colon)

Open bokjo opened this issue 3 years ago • 0 comments

Description

Don't know whether should classify it as a bug, more as a remark I guess!

@IsMilitaryTime() will validate to true both 0800 and 08:00 which is fine. However, I want to have the ":" always validated which cannot be specified now ":" is optional in the regex).

https://github.com/typestack/class-validator/blob/2ef8ff0f32be53e75dade2a0b86b158d87427176/src/decorator/string/IsMilitaryTime.ts#L12

When the validation for @IsMilitaryTime() fails the default message returned is '$property must be a valid representation of military time in the format HH:MM' which is a bit misleading because the ":" is inferred to be always there which is not the case for a military time I believe... also the format should be HH24:MM?

https://github.com/typestack/class-validator/blob/2ef8ff0f32be53e75dade2a0b86b158d87427176/src/decorator/string/IsMilitaryTime.ts#L27

Proposal default error message '$property must be a valid representation of military time in the format HH24MM or HH24:MM'

  // 0800 and 08:00 are valid options
  @IsString()
  @IsMilitaryTime({ message: "openingTime must be a valid time in format HH24:MM" })
  openingTime: string;

  // Current "fix" on my end... 
  // 0800 => not valid
  // 08:00 => valid
  @IsString()
  @IsMilitaryTime({ message: "openingTime must be a valid time in format HH24:MM" })
  @Length(5, 5, { message: "openingTime has to be 5 characters long in format HH24:MM" })
  @Contains(":", { message: "openingTime must contain ':' in format HH24:MM" })
  openingTime: string;

p.s it would be nice to specify whether you want the ":" validated or not, no idea whether this breaks the concrete definition of military time!

bokjo avatar Jul 12 '22 12:07 bokjo