Replies: 6 comments 23 replies
|
Some examples of valid strings: "2023-08-05T12:24:59.000Z" // options undefined
"2023-08-05T12:24:59.000-05:00" // options undefined
// seconds, milliseconds, and timezone are optional by default, or you can explicitly exclude them
"2023-08-05T12:24" // {date: true, time: true, seconds: false, timezone: false} or undefined
"2023-08-05T12:24:00-05:00" // {date: true, time: true, milliseconds: false} or undefined
"2023-08-05" // {time: false}
"T12:24:59.000Z" // {date: false}
"12:24" // {date: false, seconds: false, timezone: false}
|
|
Thank you for your contribution. I don't know yet if Valibot should contain such complex validations or if we should leave that to the community and external libraries. As a compromise, I could also imagine an extra repository, which is maintained and extended with the community containing complex and more specialized validations. I am looking forward to feedback on this. |
Try // Transforms a Date, string, or number into a Date
export const dateSchema = transform(
// Input types: Date, string, number
union(
[date(), string([isoTimestamp()]), number([minValue(0)])],
"Must be a valid Date object, ISO string, or UNIX timestamp"
),
// Output type: Date
(input) => new Date(input)
); |
|
@sillvva export const SystemStatus = object({
alloc: number(),
//...
startTime: date(), // TODO: change it to use your solution.
sys: number(),
tilde: string(),
//...
}); |
|
How do you validate Date string like the following ? |
|
A question that might be relevant to this discussion: How come something like 2021-09-30T21 is a valid ISO 8601 date, but can't be parsed with new Date()? Valibot parses these as valid, while zod parses them as invalid: Here's a jsfiddle with an input field if you want to test more: https://jsfiddle.net/wq7tjec7/14/ |


Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Notes:
secondsandmillisecondsare optional.secondsmust be true formillisecondsto be validated.Tis optional if date is not validated.Zor a UTC offset (ex.+/-hh:mm)All reactions