type-graphql icon indicating copy to clipboard operation
type-graphql copied to clipboard

GraphQLISODateTime scalar does not throw error on bad input values

Open brycepolly opened this issue 3 years ago • 3 comments

Describe the Bug The conversion function assumes that new Date("some garbage") will throw an exception for bad input values on the Date constructor. When given bad inputs the Date object will just return Invalid Date which is still a Date object, and not throw.

To Reproduce Create an input object with a field which is type Date Pass in in invalid date string for that fields value The field get set to null

Expected Behavior Throws the error here https://github.com/MichalLytek/type-graphql/blob/3137ab532fbe6742d06f938d95eefddee1eea435/src/scalars/isodate.ts#L7 back to the caller.

Environment (please complete the following information):

  • OS: Linux
  • Node 16.17
  • Package version 1.2.0-rc1
  • TypeScript version 4.7

Additional Context I've replaced the provided GraphQLISODateTime with my own copy of it and have modified the conversion function like so to fix this for us.

function convertStringToDate(dateString: string) {
  const value = new Date(dateString);
  if (value instanceof Date && !isNaN(value.valueOf())) {
    return value;
  }
  throw new Error("Provided date string is invalid and cannot be parsed");
}

brycepolly avatar Sep 20 '22 03:09 brycepolly

I think for new major release we should stop having own scalars with bugs and just use graphql-scalars package 👀

MichalLytek avatar Sep 20 '22 07:09 MichalLytek

I think for new major release we should stop having own scalars with bugs and just use graphql-scalars package 👀

Thanks for that, I'll actually use the one from graphql-scalars instead.

brycepolly avatar Sep 20 '22 21:09 brycepolly

Hey @MichalLytek @brycepolly I would love to pick this up. However a little bit of guidance would be most appreciated!

frostzt avatar Oct 12 '22 16:10 frostzt

Closing via 480ec1d 🔒

We now use graphql-scalars so this issue is not relevant anymore.

MichalLytek avatar Apr 17 '24 12:04 MichalLytek