@@ -15,18 +15,20 @@ import {
15
15
type FormMerger ,
16
16
type UiOptionsRegistry ,
17
17
type Creatable ,
18
- create
18
+ create ,
19
+ type FormIdBuilder ,
20
+ type IdBuilderFactoryOptions
19
21
} from '@sjsf/form' ;
20
22
import {
21
23
type IdOptions ,
22
24
DEFAULT_ID_SEPARATOR ,
23
- DEFAULT_ID_PSEUDO_SEPARATOR ,
24
- createFormIdBuilder
25
+ DEFAULT_ID_PSEUDO_SEPARATOR
25
26
} from '@sjsf/form/id-builders/legacy' ;
26
27
import { DEFAULT_INDEX_SEPARATOR } from '@sjsf/form/id-builders/modern' ;
27
28
28
29
import {
29
30
FORM_DATA_FILE_PREFIX ,
31
+ ID_PREFIX_KEY ,
30
32
JSON_CHUNKS_KEY ,
31
33
type InitialFormData ,
32
34
type SerializableOptionalFormOptions ,
@@ -40,6 +42,7 @@ import {
40
42
type UnknownEntryConverter
41
43
} from './convert-form-data-entries.js' ;
42
44
import type { EntriesConverter } from './entry.js' ;
45
+ import { parseIdPrefix } from './id-prefix-parser.js' ;
43
46
44
47
export type InitFormOptions < T , SendSchema extends boolean > = SerializableOptionalFormOptions < T > & {
45
48
sendSchema ?: SendSchema ;
@@ -70,6 +73,7 @@ export interface FormHandlerOptions<SendData extends boolean> extends IdOptions
70
73
uiSchema ?: UiSchemaRoot ;
71
74
uiOptionsRegistry ?: UiOptionsRegistry ;
72
75
idIndexSeparator ?: string ;
76
+ idBuilder : Creatable < FormIdBuilder , IdBuilderFactoryOptions > ;
73
77
validator : Creatable < Validator , ValidatorFactoryOptions > ;
74
78
merger : Creatable < FormMerger , MergerFactoryOptions > ;
75
79
createEntriesConverter ?: Creatable <
@@ -96,6 +100,7 @@ export function createFormHandler<SendData extends boolean>({
96
100
schema,
97
101
uiSchema = { } ,
98
102
uiOptionsRegistry = { } ,
103
+ idBuilder : createIdBuilder ,
99
104
merger : createMerger ,
100
105
validator : createValidator ,
101
106
createEntriesConverter = createFormDataEntriesConverter ,
@@ -107,10 +112,11 @@ export function createFormHandler<SendData extends boolean>({
107
112
sendData,
108
113
createReviver = createDefaultReviver
109
114
} : FormHandlerOptions < SendData > ) {
110
- const idBuilder = createFormIdBuilder ( {
115
+ const idBuilder = create ( createIdBuilder , {
111
116
idPrefix,
112
- idSeparator,
113
- idPseudoSeparator
117
+ schema,
118
+ uiOptionsRegistry,
119
+ uiSchema
114
120
} ) ;
115
121
const validator : Validator = create ( createValidator , {
116
122
schema,
@@ -142,6 +148,14 @@ export function createFormHandler<SendData extends boolean>({
142
148
( errors : ValidationError [ ] ) => ValidatedFormData < SendData >
143
149
]
144
150
> => {
151
+ const idPrefix = formData . has ( ID_PREFIX_KEY )
152
+ ? ( formData . get ( ID_PREFIX_KEY ) as string )
153
+ : parseIdPrefix ( {
154
+ formData,
155
+ idIndexSeparator,
156
+ idPseudoSeparator,
157
+ idSeparator
158
+ } ) ;
145
159
const data = formData . has ( JSON_CHUNKS_KEY )
146
160
? JSON . parse ( formData . getAll ( JSON_CHUNKS_KEY ) . join ( '' ) , createReviver ( formData ) )
147
161
: await parseSchemaValue ( signal , {
@@ -163,11 +177,12 @@ export function createFormHandler<SendData extends boolean>({
163
177
: [ ] ;
164
178
function validated ( errors : ValidationError [ ] ) {
165
179
return {
180
+ idPrefix,
166
181
isValid : errors . length === 0 ,
167
- sendData : sendData ? data : undefined ,
168
- data : data as FormValue ,
182
+ sendData,
183
+ data : ( sendData ? data : undefined ) as SendData extends true ? FormValue : undefined ,
169
184
errors
170
- } as ValidatedFormData < SendData > ;
185
+ } satisfies ValidatedFormData < SendData > ;
171
186
}
172
187
return [ validated ( errors ) , data , validated ] ;
173
188
} ;
0 commit comments