Schema - Time (String)
The .time
method is used to validate whether a given string is a properly formatted time. It supports various time formats such as HH:MM
, HH:MM:SS
, and HH:MM:SS.MS
, allowing flexibility in validating time inputs based on the specified format.
This method is particularly useful for scenarios where time validation is crucial, such as scheduling systems, time tracking, or input fields requiring precise time formats.
- format: Specifies the desired time format to validate. Supported formats are:
HH:MM
HH:MM:SS
HH:MM:SS.MS
Example 1: Validate Time in HH:MM
Format
Below is an example of how to use .time
to validate whether a value is a valid time in the HH:MM
format:
import { schema } from "vkrun"
const exampleSchema = schema().string().time("HH:MM")
const validateA = exampleSchema.validate("20:03") // Valid time
const validateB = exampleSchema.validate("20:1") // Invalid time
console.log(validateA) // true
console.log(validateB) // false
Example 2: Validate Time in HH:MM:SS
Format
import { schema } from "vkrun"
const exampleSchema = schema().string().time("HH:MM:SS")
const validateA = exampleSchema.validate("13:59:59") // Valid time
const validateB = exampleSchema.validate("13:59") // Invalid time
console.log(validateA) // true
console.log(validateB) // false
Example 3: Validate Time in HH:MM:SS.MS Format
import { schema } from "vkrun"
const exampleSchema = schema().string().time("HH:MM:SS.MS")
const validateA = exampleSchema.validate("08:30:45.123") // Valid time
const validateB = exampleSchema.validate("08:30:45.1234") // Invalid time
console.log(validateA) // true
console.log(validateB) // false