fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
5.6k stars 169 forks source link

Best way to validate optional ISO time #682

Open zettadam opened 1 week ago

zettadam commented 1 week ago

What is the best way to validate optional ISO time?

When I write schema like this in the Playground:

import * as v from 'valibot'
const Schema = v.pipe(
    v.string(),
    v.union([
        v.literal(''),
        v.isoTime('Invalid time format')
    ])
)
const result = v.safeParse(Schema, '08:34')

console.log('result', result)

I receive cryptic errors.

When I copy this code into my editor, I receive cryptic TS errors related to union.

zettadam commented 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.

fabian-hiller commented 1 week ago

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())]);