GoogleFeud / ts-runtime-checks

A typescript transformer that automatically generates validation code from your types.
https://googlefeud.github.io/ts-runtime-checks/
MIT License
312 stars 7 forks source link

[BUG] Date validation generate excessive code which does not work #28

Closed FredTreg closed 1 year ago

FredTreg commented 1 year ago

Describe the bug When validating a Date, Assert produces more than 80 lines of code. I understand that the validation mechanism is generic, but for this builtin type, could the code be reduced to if (!(a instanceof Date))? Besides the generated code does not work with Error: Expected d.toLocaleString to be an object which should not be expected.

As Dates are not part of Json, I would understand if this is not a target for ts-runtime-checks, I'll use NoCheck<Date> on my types if that's the case.

Playground link playground

Expected behavior Date fields validate correctly

Additional context

GoogleFeud commented 1 year ago

I've always thought of Date as a class but the typescript library defines it as an interface and for that reason it's not treated as a class by the library. I'll add Date as an exception in the next patch so the code gets generated correctly!

In the meanwhile, you could use a custom Check:

type $Date = NoCheck<Date> & Check<"$self instanceof Date", "to be a date">;
FredTreg commented 1 year ago

Thanks for the workaround!

I think the issue also exists with Object