diff --git a/src/index.ts b/src/index.ts index 174206b..705ec91 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,8 +10,15 @@ import { ajv as ajvInternal } from './utils/validation'; import { ErrorObject, JSONSchemaType, KeywordDefinition, SchemaObject } from 'ajv'; import { useDebounce } from './Hooks/useDebounce'; -import { AJVMessageFunction, FormField, IState, UseFormReturn } from './utils/types'; +import { + AJVMessageFunction, + FormField, + IState, + UseFormReturn, + ValidateResult, +} from './utils/types'; import Logger from './utils/Logger'; + const useAJVForm = >( initial: T, schema: JSONSchemaType | SchemaObject, @@ -114,7 +121,7 @@ const useAJVForm = >( }); }; - const validateForm = () => { + const validateForm = (): ValidateResult => { const data = Object.keys(state).reduce((acc, inputName) => { acc[inputName as keyof T] = getValue(state[inputName].value) as T[keyof T]; return acc; diff --git a/src/utils/types.ts b/src/utils/types.ts index 0de3e38..f2cc083 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -42,15 +42,21 @@ export type InitialState = { }; }; -export type ValidateResult = { - isValid: boolean; - data: T | null; - errors?: Partial<{ [K in keyof T]: T[K] }>; -}; +export type ValidateResult = + | { + isValid: true; + data: T; + } + | { + isValid: false; + data: null; + errors?: Partial<{ [K in keyof T]: T[K] }>; + }; export type useFormErrors = { [K in keyof T]?: string; }; + export interface UseFormReturn { data: T; state: IState;