File tree 2 files changed +20
-7
lines changed
2 files changed +20
-7
lines changed Original file line number Diff line number Diff line change @@ -10,8 +10,15 @@ import { ajv as ajvInternal } from './utils/validation';
10
10
11
11
import { ErrorObject , JSONSchemaType , KeywordDefinition , SchemaObject } from 'ajv' ;
12
12
import { useDebounce } from './Hooks/useDebounce' ;
13
- import { AJVMessageFunction , FormField , IState , UseFormReturn } from './utils/types' ;
13
+ import {
14
+ AJVMessageFunction ,
15
+ FormField ,
16
+ IState ,
17
+ UseFormReturn ,
18
+ ValidateResult ,
19
+ } from './utils/types' ;
14
20
import Logger from './utils/Logger' ;
21
+
15
22
const useAJVForm = < T extends Record < string , any > > (
16
23
initial : T ,
17
24
schema : JSONSchemaType < T > | SchemaObject ,
@@ -114,7 +121,7 @@ const useAJVForm = <T extends Record<string, any>>(
114
121
} ) ;
115
122
} ;
116
123
117
- const validateForm = ( ) => {
124
+ const validateForm = ( ) : ValidateResult < T > => {
118
125
const data = Object . keys ( state ) . reduce ( ( acc , inputName ) => {
119
126
acc [ inputName as keyof T ] = getValue ( state [ inputName ] . value ) as T [ keyof T ] ;
120
127
return acc ;
Original file line number Diff line number Diff line change @@ -42,15 +42,21 @@ export type InitialState<T> = {
42
42
} ;
43
43
} ;
44
44
45
- export type ValidateResult < T > = {
46
- isValid : boolean ;
47
- data : T | null ;
48
- errors ?: Partial < { [ K in keyof T ] : T [ K ] } > ;
49
- } ;
45
+ export type ValidateResult < T > =
46
+ | {
47
+ isValid : true ;
48
+ data : T ;
49
+ }
50
+ | {
51
+ isValid : false ;
52
+ data : null ;
53
+ errors ?: Partial < { [ K in keyof T ] : T [ K ] } > ;
54
+ } ;
50
55
51
56
export type useFormErrors < T > = {
52
57
[ K in keyof T ] ?: string ;
53
58
} ;
59
+
54
60
export interface UseFormReturn < T > {
55
61
data : T ;
56
62
state : IState < T > ;
You can’t perform that action at this time.
0 commit comments