From 6b85ac1af29774f399723006a80a38b0afa628fc Mon Sep 17 00:00:00 2001 From: Johan Book <13253042+johanbook@users.noreply.github.com> Date: Tue, 3 Dec 2024 16:46:34 +0100 Subject: [PATCH 1/2] Improve validation result type --- src/utils/types.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) 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; From 230a0d2bb4bf66223e64c9235a8f7dd777c1c011 Mon Sep 17 00:00:00 2001 From: Johan Book <13253042+johanbook@users.noreply.github.com> Date: Tue, 3 Dec 2024 17:20:42 +0100 Subject: [PATCH 2/2] fix: fix type error --- src/index.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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;