1
1
import { useEffect , useMemo , useRef , useState } from 'react' ;
2
2
import { addUserDefinedKeywords , getErrors , getInitial , getValue } from './utils' ;
3
- import { ajv } from './utils/validation' ;
3
+ import { ajv as ajvInternal } from './utils/validation' ;
4
4
5
5
import { ErrorObject , JSONSchemaType , KeywordDefinition , SchemaObject } from 'ajv' ;
6
6
import { useDebounce } from './Hooks/useDebounce' ;
@@ -17,6 +17,7 @@ const useAJVForm = <T extends Record<string, any>>(
17
17
initial : T ,
18
18
schema : JSONSchemaType < T > | SchemaObject ,
19
19
options ?: {
20
+ ajv ?: typeof ajvInternal ;
20
21
customKeywords ?: KeywordDefinition [ ] ;
21
22
errors ?: ErrorObject [ ] ;
22
23
userDefinedMessages ?: Record < string , AJVMessageFunction > ;
@@ -25,6 +26,8 @@ const useAJVForm = <T extends Record<string, any>>(
25
26
debug ?: boolean ;
26
27
} ,
27
28
) : UseFormReturn < T > => {
29
+ const ajvInstance = options ?. ajv || ajvInternal ;
30
+
28
31
const initialStateRef = useRef < IState < T > > ( getInitial ( initial ) ) ;
29
32
30
33
const [ state , setState ] = useState < IState < T > > ( getInitial ( initial ) ) ;
@@ -41,10 +44,10 @@ const useAJVForm = <T extends Record<string, any>>(
41
44
) ;
42
45
43
46
if ( options ?. customKeywords ?. length ) {
44
- addUserDefinedKeywords ( ajv , options . customKeywords ) ;
47
+ addUserDefinedKeywords ( ajvInstance , options . customKeywords ) ;
45
48
}
46
49
47
- const AJVValidate = ajv . compile ( schema ) ;
50
+ const AJVValidate = ajvInstance . compile ( schema ) ;
48
51
49
52
const resetForm = ( ) => {
50
53
logger . log ( 'Form reset to initial state' ) ;
0 commit comments