lib/validators.ts and integrates with the insurance configuration.
The validarCotizacion Function
The main validation function accepts a form submission and returns an object containing any validation errors:lib/validators.ts
Validation Rules
Base Field Validation
All quote forms require these base fields:Insurance Type Validation
Insurance Type Validation
Name Validation
Name Validation
Email Validation
Email Validation
- Must contain
@symbol - Must have domain with
. - No whitespace allowed
Phone Validation
Phone Validation
- Minimum 8 digits
- Only numeric characters
- No spaces or special characters
Dynamic Field Validation
Fields defined in the insurance configuration are validated dynamically:Required Field Check
lib/validators.ts
required: true in the configuration, this validation ensures it has a value.
Number Field Validation
lib/validators.ts
min or max constraints:
- Converts the value to a number
- Checks against minimum value if defined
- Checks against maximum value if defined
Error Message Structure
Validation errors are returned as aRecord<string, string> where:
- Key: Field name (e.g.,
"nombre","email","edad") - Value: User-friendly error message in Spanish
Example Error Object
Usage in Forms
The validation function is called before submitting the form:app/components/CotizacionForm.tsx
Displaying Validation Errors
Errors are displayed inline below each form field:app/components/CotizacionForm.tsx
Visual Feedback
Red Border
Fields with errors receive a
border-red-500 class for visual emphasis.Error Message
Error text appears below the field in red with proper spacing.
Validation Examples
Best Practices
Validate on Submit
Always call
validarCotizacion before processing form submissions (email or WhatsApp).Show All Errors
Display all validation errors simultaneously so users can fix multiple issues at once.
Clear Errors Appropriately
Clear the error state after successful validation or when the user starts correcting a field.
Extending Validation
To add custom validation rules:- Add validation logic in
lib/validators.ts - Return appropriate error messages in the
erroresobject - Display errors in the form component
Example: Adding CPF/CUIT Validation
lib/validators.ts