Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
FredTreg opened this issue Jul 29, 2023 · 2 comments
Closed

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

FredTreg opened this issue Jul 29, 2023 · 2 comments

Comments

@FredTreg
Copy link
Contributor

FredTreg commented Jul 29, 2023

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
Copy link
Owner

GoogleFeud commented Jul 29, 2023

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
Copy link
Contributor Author

Thanks for the workaround!

I think the issue also exists with Object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants