11import { fileToDataURL } from '@sjsf/form/lib/file' ;
22import { defaultMerger , type Merger2 , type Validator } from '@sjsf/form/core' ;
33import {
4- DEFAULT_ID_PREFIX ,
5- DEFAULT_ID_SEPARATOR ,
6- DEFAULT_PSEUDO_ID_SEPARATOR ,
7- type FormValidator ,
8- type Schema ,
9- type SchemaValue ,
10- type UiSchemaRoot
4+ DEFAULT_ID_PREFIX ,
5+ DEFAULT_ID_SEPARATOR ,
6+ DEFAULT_PSEUDO_ID_SEPARATOR ,
7+ type FormValidator ,
8+ type Schema ,
9+ type SchemaValue ,
10+ type UiSchemaRoot ,
11+ type ValidationError
1112} from '@sjsf/form' ;
1213
1314import { JSON_CHUNKS_KEY , type InitialFormData , type ValidatedFormData } from '../model' ;
@@ -16,112 +17,108 @@ import type { Entry } from './entry';
1617import { parseSchemaValue } from './schema-value-parser' ;
1718import { makeFormDataEntriesConverter } from './convert-form-data-entries' ;
1819
19- export interface InitFormOptions < T , E , SendSchema extends boolean > {
20- schema : Schema ;
21- validator : FormValidator < E > ;
22- sendSchema ?: SendSchema ;
23- initialValue ?: T ;
24- /** @default false */
25- performValidation ?: boolean ;
26- }
20+ export type InitFormOptions < T , E , SendSchema extends boolean > = {
21+ sendSchema ?: SendSchema ;
22+ initialValue ?: T ;
23+ initialErrors ?: ValidationError < E > [ ] ;
24+ } & ( SendSchema extends true
25+ ? { schema : Schema }
26+ : {
27+ schema ?: never ;
28+ } ) ;
2729
2830export function initForm < T , E , SendSchema extends boolean = false > ( {
29- schema,
30- validator,
31- initialValue,
32- performValidation = false ,
33- sendSchema
31+ schema,
32+ initialValue,
33+ initialErrors = [ ] ,
34+ sendSchema
3435} : InitFormOptions < T , E , SendSchema > ) : InitialFormData < T , E , SendSchema > {
35- const errors = performValidation ? validator . validateFormData ( schema , initialValue as SchemaValue | undefined ) : [ ] ;
36- return {
37- initialValue,
38- initialErrors : errors ,
39- schema : ( sendSchema ? schema : undefined ) as SendSchema extends true ? Schema : undefined
40- } ;
36+ return {
37+ initialValue,
38+ initialErrors,
39+ schema : ( sendSchema ? schema : undefined ) as SendSchema extends true ? Schema : undefined
40+ } ;
4141}
4242
4343export interface FormDataParserOptions {
44- validator : Validator ;
45- merger ?: Merger2 ;
46- idPrefix ?: string ;
47- idSeparator ?: string ;
48- idPseudoSeparator ?: string ;
44+ validator : Validator ;
45+ merger ?: Merger2 ;
46+ idPrefix ?: string ;
47+ idSeparator ?: string ;
48+ idPseudoSeparator ?: string ;
4949}
5050
5151export function makeFormDataParser ( {
52- validator,
53- merger = defaultMerger ,
54- idPrefix = DEFAULT_ID_PREFIX ,
55- idSeparator = DEFAULT_ID_SEPARATOR ,
56- idPseudoSeparator = DEFAULT_PSEUDO_ID_SEPARATOR
52+ validator,
53+ merger = defaultMerger ,
54+ idPrefix = DEFAULT_ID_PREFIX ,
55+ idSeparator = DEFAULT_ID_SEPARATOR ,
56+ idPseudoSeparator = DEFAULT_PSEUDO_ID_SEPARATOR
5757} : FormDataParserOptions ) {
58- return async ( {
59- request,
60- schema,
61- uiSchema = { }
62- } : {
63- schema : Schema ;
64- request : Request ;
65- uiSchema ?: UiSchemaRoot ;
66- } ) : Promise < SchemaValue | undefined > => {
67- const formData = await request . formData ( ) ;
68- if ( formData . get ( JSON_CHUNKS_KEY ) ) {
69- const chunks = formData . getAll ( JSON_CHUNKS_KEY ) . join ( '' ) ;
70- return JSON . parse ( chunks ) ;
71- }
72- return parseSchemaValue ( {
73- idPrefix,
74- idSeparator,
75- idPseudoSeparator,
76- schema,
77- uiSchema,
78- entries : await Promise . all (
79- formData
80- . entries ( )
81- . map ( ( entry ) =>
82- entry [ 1 ] instanceof File
83- ? fileToDataURL ( request . signal , entry [ 1 ] ) . then (
84- ( data ) : Entry < string > => [ entry [ 0 ] , data ]
85- )
86- : ( entry as Entry < Exclude < FormDataEntryValue , File > > )
87- )
88- ) ,
89- validator,
90- merger,
91- convertEntries : makeFormDataEntriesConverter ( {
92- validator,
93- merger,
94- rootSchema : schema ,
95- rootUiSchema : uiSchema
96- } )
97- } ) ;
98- } ;
58+ return async ( {
59+ request,
60+ schema,
61+ uiSchema = { }
62+ } : {
63+ schema : Schema ;
64+ request : Request ;
65+ uiSchema ?: UiSchemaRoot ;
66+ } ) : Promise < SchemaValue | undefined > => {
67+ const formData = await request . formData ( ) ;
68+ if ( formData . get ( JSON_CHUNKS_KEY ) ) {
69+ const chunks = formData . getAll ( JSON_CHUNKS_KEY ) . join ( '' ) ;
70+ return JSON . parse ( chunks ) ;
71+ }
72+ return parseSchemaValue ( {
73+ idPrefix,
74+ idSeparator,
75+ idPseudoSeparator,
76+ schema,
77+ uiSchema,
78+ entries : await Promise . all (
79+ formData
80+ . entries ( )
81+ . map ( ( entry ) =>
82+ entry [ 1 ] instanceof File
83+ ? fileToDataURL ( request . signal , entry [ 1 ] ) . then (
84+ ( data ) : Entry < string > => [ entry [ 0 ] , data ]
85+ )
86+ : ( entry as Entry < Exclude < FormDataEntryValue , File > > )
87+ )
88+ ) ,
89+ validator,
90+ merger,
91+ convertEntries : makeFormDataEntriesConverter ( {
92+ validator,
93+ merger,
94+ rootSchema : schema ,
95+ rootUiSchema : uiSchema
96+ } )
97+ } ) ;
98+ } ;
9999}
100100
101101export interface ValidateFormOptions < E , SendData extends boolean > {
102- data : SchemaValue | undefined ;
103- schema : Schema ;
104- validator : FormValidator < E > ;
105- /** @default false */
106- sendData ?: SendData ;
102+ data : SchemaValue | undefined ;
103+ schema : Schema ;
104+ validator : FormValidator < E > ;
105+ /** @default false */
106+ sendData ?: SendData ;
107107}
108108
109- export function validateForm <
110- E ,
111- SendData extends boolean = false
112- > ( {
113- schema,
114- validator,
115- data,
116- sendData,
109+ export function validateForm < E , SendData extends boolean = false > ( {
110+ schema,
111+ validator,
112+ data,
113+ sendData
117114} : ValidateFormOptions < E , SendData > ) : ValidatedFormData < E , SendData > {
118- const errors = validator . validateFormData ( schema , data ) ;
119- return {
120- isValid : errors . length === 0 ,
121- sendData,
122- data : ( sendData ? data : undefined ) as SendData extends true
123- ? SchemaValue | undefined
124- : undefined ,
125- errors
126- } ;
115+ const errors = validator . validateFormData ( schema , data ) ;
116+ return {
117+ isValid : errors . length === 0 ,
118+ sendData,
119+ data : ( sendData ? data : undefined ) as SendData extends true
120+ ? SchemaValue | undefined
121+ : undefined ,
122+ errors
123+ } ;
127124}
0 commit comments