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

Date validation default error message #654

Closed
HugeLetters opened this issue Jun 14, 2024 · 7 comments
Closed

Date validation default error message #654

HugeLetters opened this issue Jun 14, 2024 · 7 comments
Assignees
Labels
enhancement New feature or request fix A smaller enhancement or bug fix

Comments

@HugeLetters
Copy link

HugeLetters commented Jun 14, 2024

When receiving an invalid date(say new Date("invalid")) what you get as an error is

Expected Date but received Date

which is really confusing
I would suggest checking for invalid date case and then producing an error message like

Expected Date but received InvalidDate

I'm willing to work on a PR

My current workaround is this

const DateSchema = date((issue) => {
  if (issue.input instanceof Date) {
    return 'Expected Date but received InvalidDate';
  }

  return issue.message;
})

Given we're already handling an error input could only be a Date if it's only an invalid date but we could do a more rigorous check here like

issue.input instanceof Date && Number.isNaN(issue.input.getTime())
@fabian-hiller
Copy link
Owner

Thanks for bringing this up. We could easily fix this, but we have to be careful not to break the current language-neutral expected and received system, which follows a specific syntax. Instead of English words, we use symbols like ! and <= to describe both properties. Something like InvalidDate makes no sense because there is no InvalidDate data type in JavaScript. Also, Invalid Date does not work because it breaks our syntax. We could change it to !Date to produce an error message that isn't confusing, but that way it's less clear what the error actually is. I would appreciate your feedback on this.

@fabian-hiller fabian-hiller self-assigned this Jun 15, 2024
@fabian-hiller fabian-hiller added enhancement New feature or request fix A smaller enhancement or bug fix labels Jun 15, 2024
@HugeLetters
Copy link
Author

Would enclosing "Invalid Date" in quotes work? I've proposed it cause that's the way it's stringified in most environments
image
image

@fabian-hiller
Copy link
Owner

This is a good idea. The only drawback might be that it is not clear whether the input was the string "Invalid Date" or an invalid date object, but it is probably better than the current solution.

@HugeLetters
Copy link
Author

HugeLetters commented Jun 15, 2024

We could use quotes which are not used for strings then :)
If string are displayed with " then we might use ` or '

@fabian-hiller
Copy link
Owner

I am not sure I want to introduce a new syntax just for one case. I recommend going with "Invalid Date" until we find a better solution.

@fabian-hiller
Copy link
Owner

Valibot v0.33.1 is available 🎉

@HugeLetters
Copy link
Author

great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fix A smaller enhancement or bug fix
Projects
None yet
Development

No branches or pull requests

2 participants