UNPKG

@tmlmobilidade/types

Version:

This package provides shared Zod validation schemas and their corresponding TypeScript types for use across projects. All types are automatically inferred from the schemas to ensure full type safety and reduce maintenance overhead.

22 lines (21 loc) 785 B
/* * */ import { DateTime } from 'luxon'; import { z } from 'zod'; /* * */ export const OPERATIONAL_DATE_FORMAT = 'yyyyMMdd'; export const operationalDateSchema = z .string() .transform(validateOperationalDate) .brand('OperationalDate'); /** * This function validates if a string is a valid operational date. * Throws an error if the date is invalid. * @param date - The date to be validated. * @returns The given string as an OperationalDate. */ export function validateOperationalDate(date) { const parsedDate = DateTime.fromFormat(date, OPERATIONAL_DATE_FORMAT); if (!parsedDate.isValid) throw new Error(`Invalid date format '${date}', expected format: ${OPERATIONAL_DATE_FORMAT}, explanation: ${parsedDate.invalidExplanation}`); return date; }