Open zettadam opened 1 week ago
I have come up with this function which passed all tests I had written so far:
export function validateTime(config: Config) {
// defaults
const _required = config.required ?? !config.optional ?? false
return pipe(
string(),
!_required ? minLength(0) : nonEmpty('Required field'),
check((input) => {
if (!_required && !input) return true
return /^(?:0\d|1\d|2[0-3]):[0-5]\d$/u.test(input)
}, 'Invalid time format'),
)
}
But I'd rather use check
as a last resort.
Here are two examples:
import * as v from 'valibot';
// Time or undefined
const Schema1 = v.optional(v.pipe(v.string(), v.isoTime()));
// Time or empty string
const Schema2 = v.union([v.literal(''), v.pipe(v.string(), v.isoTime())]);
What is the best way to validate optional ISO time?
When I write schema like this in the Playground:
I receive cryptic errors.
When I copy this code into my editor, I receive cryptic TS errors related to
union
.