question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature: flag for `@IsDateString()` decorator to validate date without time

See original GitHub issue

It`s more feature request. I think it would be nice to have ability validate date string without time using @IsDateString() decorator. For example, ‘2000-07-26’, not ‘2000-07-26T00:00:01.967Z’. Cannot find how to do that not using @Matches() decorator.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:13
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

21reactions
ghostcommented, Oct 2, 2020

I create custom one.

import { registerDecorator, ValidationOptions } from 'class-validator';

export function IsOnlyDate(validationOptions?: ValidationOptions) {
  return function(object: Object, propertyName: string) {
    registerDecorator({
      name: 'IsOnlyDate',
      target: object.constructor,
      propertyName: propertyName,
      constraints: [],
      options: {
        message: 'Please provide only date like 2020-12-08',
        ...validationOptions,
      },
      validator: {
        validate(value: any) {
          const regex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/;
          return typeof value === 'string' && regex.test(value);
        },
      },
    });
  };
}
11reactions
RomanSolianikcommented, Feb 12, 2021

@talski of course we can use regular expressions… basically we can use them for all validations, but I think the point of the creation of this library was to simplify this process

Read more comments on GitHub >

github_iconTop Results From Across the Web

flag for @IsDateString() to validate date without time
It`s more feature request. I think it would be nice to have ability validate date string without time using @IsDateString() decorator.
Read more >
How to validate an array of Date with class validator?
Try this one: export class CopyMealsPlanDto { ...// Another array @IsDateString({}, { each: true }) dates: Date[]; }. Run code snippet
Read more >
class-validator - npm
Allows use of decorator and non-decorator based validation. Internally uses validator.js to perform validation. Class-validator works on ...
Read more >
Class Validator Date String - StackBlitz
IsISO8601 } from 'class-validator';. class Test {. @IsDateString({ strict: true } as any). dateString = '2019-09-03';. @IsISO8601().
Read more >
typestack/class-validator - CD2H gitForager
Allows use of decorator and non-decorator based validation. ... If you do not want to have such properties on your object, pass special...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found