Skip to content

feat: improve isValid calculation by using AJV validation #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .prettierrc

This file was deleted.

10 changes: 10 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const config = {
semi: true,
trailingComma: 'all',
singleQuote: true,
printWidth: 85,
tabWidth: 2,
endOfLine: 'auto',
};

module.exports = config;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,6 @@
"json schema validation",
"use-ajv-form",
"react form validation"
]
],
"packageManager": "[email protected]+sha1.120e76442c589bb9a3267dbbefa5658c9943fcae"
}
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,13 @@ const useAJVForm = <T extends Record<string, any>>(
);
}, [state]);

const isValid = useMemo(
() => Object.keys(state).every((key) => !state[key].error),
[state],
);
const isValid = useMemo(() => {
const data = Object.keys(state).reduce((acc, inputName) => {
acc[inputName as keyof T] = getValue(state[inputName].value) as T[keyof T];
return acc;
}, {} as T);
return AJVValidate(data);
}, [state]);

useEffect(() => {
if (!options?.errors || !options.errors.length) {
Expand Down