99import { z } from 'zod' ;
1010
1111import { FormField } from '@/core/domain/entities/form' ;
12+ import { AnswerValue } from '@/core/domain/entities/response' ;
1213import { FIELD_TYPES } from '@/core/domain/value-objects/field-types' ;
1314
15+ /** Zod schema types that can be produced for a single form field (string, number | null, string[]) */
16+ export type FieldSchema =
17+ | z . ZodType < string >
18+ | z . ZodType < number | null >
19+ | z . ZodType < string [ ] > ;
20+
1421/**
1522 * Builds a Zod schema dynamically from form fields.
1623 * Each field ID becomes a key in the resulting object schema.
1724 */
18- export const buildResponseSchema = ( fields : FormField [ ] ) => {
19- const shape : Record < string , z . ZodTypeAny > = { } ;
25+ export const buildResponseSchema = (
26+ fields : FormField [ ] ,
27+ ) : z . ZodType < Record < string , AnswerValue > > => {
28+ const shape : Record < string , FieldSchema > = { } ;
2029
2130 for ( const field of fields ) {
2231 shape [ field . id ] = buildFieldSchema ( field ) ;
@@ -26,7 +35,7 @@ export const buildResponseSchema = (fields: FormField[]) => {
2635} ;
2736
2837/** Builds the Zod schema for a single field based on its type and configuration */
29- export const buildFieldSchema = ( field : FormField ) : z . ZodTypeAny => {
38+ export const buildFieldSchema = ( field : FormField ) : FieldSchema => {
3039 switch ( field . type ) {
3140 case FIELD_TYPES . SHORT_TEXT :
3241 case FIELD_TYPES . LONG_TEXT :
@@ -60,7 +69,9 @@ export const buildFieldSchema = (field: FormField): z.ZodTypeAny => {
6069 ? z . array ( z . string ( ) ) . min ( 1 , 'Selecciona al menos una opción' )
6170 : z . array ( z . string ( ) ) ;
6271
63- default :
64- return z . unknown ( ) ;
72+ default : {
73+ const exhaustive : never = field . type ;
74+ throw new Error ( `Unhandled field type: ${ exhaustive } ` ) ;
75+ }
6576 }
6677} ;
0 commit comments