Skip to content

Commit e002c43

Browse files
committed
Merge branch 'master' of github.com:agjs/use-ajv-form
2 parents b848e93 + 1ca75b2 commit e002c43

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ You can customize `useAJVForm` using the following options:
213213
| `userDefinedMessages` | `Record<string, AJVMessageFunction>` | Custom error messages for validation errors. |
214214
| `shouldDebounceAndValidate` | `boolean` | If `true`, enables debouncing for field validation. |
215215
| `debounceTime` | `number` | Time in milliseconds for debouncing validation. Ignored if `shouldDebounceAndValidate` is set to false. |
216+
| `ajv` | `Ajv` | Your own AJV instance that might have custom keywords, errors, etc. |
216217
217218
## Usage in Practice
218219

pnpm-lock.yaml

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useMemo, useRef, useState } from 'react';
22
import { addUserDefinedKeywords, getErrors, getInitial, getValue } from './utils';
3-
import { ajv } from './utils/validation';
3+
import { ajv as ajvInternal } from './utils/validation';
44

55
import { ErrorObject, JSONSchemaType, KeywordDefinition, SchemaObject } from 'ajv';
66
import { useDebounce } from './Hooks/useDebounce';
@@ -17,6 +17,7 @@ const useAJVForm = <T extends Record<string, any>>(
1717
initial: T,
1818
schema: JSONSchemaType<T> | SchemaObject,
1919
options?: {
20+
ajv?: typeof ajvInternal;
2021
customKeywords?: KeywordDefinition[];
2122
errors?: ErrorObject[];
2223
userDefinedMessages?: Record<string, AJVMessageFunction>;
@@ -25,6 +26,8 @@ const useAJVForm = <T extends Record<string, any>>(
2526
debug?: boolean;
2627
},
2728
): UseFormReturn<T> => {
29+
const ajvInstance = options?.ajv || ajvInternal;
30+
2831
const initialStateRef = useRef<IState<T>>(getInitial(initial));
2932

3033
const [state, setState] = useState<IState<T>>(getInitial(initial));
@@ -41,10 +44,10 @@ const useAJVForm = <T extends Record<string, any>>(
4144
);
4245

4346
if (options?.customKeywords?.length) {
44-
addUserDefinedKeywords(ajv, options.customKeywords);
47+
addUserDefinedKeywords(ajvInstance, options.customKeywords);
4548
}
4649

47-
const AJVValidate = ajv.compile(schema);
50+
const AJVValidate = ajvInstance.compile(schema);
4851

4952
const resetForm = () => {
5053
logger.log('Form reset to initial state');

src/useAjvForm.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { act, renderHook } from '@testing-library/react-hooks';
22
import { JSONSchemaType } from 'ajv';
3+
import Ajv from 'ajv';
34
import { vi } from 'vitest';
45
import useAJVForm from '.';
56

@@ -126,7 +127,7 @@ describe('useAJVForm', () => {
126127
expect(validation.isValid).toBe(true);
127128
});
128129

129-
it('isValid should be false when the initial state is set or when reset is called', () => {
130+
it('isValid should be true when the initial state is set or when reset is called', () => {
130131
const initialData = { title: 'Foo' };
131132
const schema: JSONSchemaType<{ title: string }> = {
132133
type: 'object',

0 commit comments

Comments
 (0)