reservation implementation

This commit is contained in:
root
2026-06-11 15:35:25 -04:00
parent 6eeba99c45
commit 37a6ed8a76
30 changed files with 2598 additions and 477 deletions
+158
View File
@@ -0,0 +1,158 @@
/**
* Zod integration helpers for the input validation/formatting library.
*
* Provides pre-built Zod refinements and transforms that can be
* chained onto `.string()` schemas throughout the API.
*
* Usage:
* import { textField, emailField } from '../lib/zodValidation'
* const schema = z.object({
* firstName: textField('name'),
* email: emailField(),
* })
*/
import { z } from 'zod'
import { sanitizeAndFormat, getMaxLength } from './inputValidation'
import type { FieldType } from './inputValidation'
// ─── Character validation (inline refine) ──────────────────────
/** Global allowed character pattern from the validation plan */
const GLOBAL_ALLOWED = /^[a-zA-Z0-9@\-/\s]*$/
export const globalCharRefine = (val: string) =>
GLOBAL_ALLOWED.test(val) || { message: 'Only letters, numbers, @, -, and / are allowed' }
// ─── Email validation ──────────────────────────────────────────
const EMAIL_REGEX = /^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$/
export const emailFormatRefine = (val: string) =>
EMAIL_REGEX.test(val) || { message: 'Please enter a valid email address' }
// ─── Pre-built Zod string wrappers ─────────────────────────────
/**
* Returns the base Zod chain: .string() → .min(1) → character refine → trim → transform
* If optional is true, wraps in .optional() at the end.
*/
function baseTextField(fieldType: FieldType, optional = false) {
const maxLength = getMaxLength(fieldType)
let schema = z
.string()
.min(1, { message: 'This field is required' })
.refine(globalCharRefine)
.trim()
.transform((val) => sanitizeAndFormat(val, fieldType))
.refine(
(val) => val.length <= maxLength,
{ message: `Maximum ${maxLength} characters allowed` }
)
if (optional) {
// For optional fields: allow undefined input, but if a value is provided, apply all rules
return z
.string()
.optional()
.transform((val) => {
if (val === undefined || val === null) return undefined
return sanitizeAndFormat(val, fieldType)
})
.refine(
(val) => val === undefined || val.length === 0 || (globalCharRefine(val as string) === true),
{ message: 'Only letters, numbers, @, -, and / are allowed' }
)
}
return schema
}
/**
* Text field with title case formatting.
* fieldType must be one of: name, nationality, streetAddress, city,
* pickupLocation, returnLocation, country
*/
export function textField(fieldType: FieldType) {
return baseTextField(fieldType, false)
}
/** Optional version of textField */
export function optionalTextField(fieldType: FieldType) {
return baseTextField(fieldType, true)
}
/**
* Title-case-all fields: fullAddress, commercialName, legalCompanyName
*/
export function titleCaseAllField(fieldType: FieldType) {
return baseTextField(fieldType, false)
}
export function optionalTitleCaseAllField(fieldType: FieldType) {
return baseTextField(fieldType, true)
}
/**
* Uppercase fields (letters only in allowed chars): licensePlate, vin, cin,
* passportNumber, internationalPermitNumber, driverLicenseNumber,
* licenseCategory, iceNumber, commercialRegistry
*/
export function upperField(fieldType: FieldType) {
return baseTextField(fieldType, false)
}
export function optionalUpperField(fieldType: FieldType) {
return baseTextField(fieldType, true)
}
/**
* Car fields (title case, letters/numbers/spaces/hyphen allowed):
* carMark, carModel, carColor
*/
export function carTextField(fieldType: FieldType) {
return baseTextField(fieldType, false)
}
export function optionalCarTextField(fieldType: FieldType) {
return baseTextField(fieldType, true)
}
/**
* Preserved format field: zipCode
*/
export function zipField() {
return baseTextField('zipCode', false)
}
export function optionalZipField() {
return baseTextField('zipCode', true)
}
/**
* Email field with full validation chain
*/
export function emailField() {
return z
.string()
.min(1, { message: 'Email is required' })
.refine(globalCharRefine)
.trim()
.transform((val) => sanitizeAndFormat(val, 'email'))
.refine(emailFormatRefine)
}
export function optionalEmailField() {
return z
.string()
.optional()
.transform((val) => {
if (val === undefined || val === null || val.trim() === '') return undefined
return sanitizeAndFormat(val, 'email')
})
.refine(
(val) => val === undefined || emailFormatRefine(val as string) === true,
{ message: 'Please enter a valid email address' }
)
}