Skip to content
Merged
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
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T extends Record<string, any>>(
initial: T,
schema: JSONSchemaType<T> | SchemaObject,
Expand Down Expand Up @@ -114,7 +121,7 @@ const useAJVForm = <T extends Record<string, any>>(
});
};

const validateForm = () => {
const validateForm = (): ValidateResult<T> => {
const data = Object.keys(state).reduce((acc, inputName) => {
acc[inputName as keyof T] = getValue(state[inputName].value) as T[keyof T];
return acc;
Expand Down
16 changes: 11 additions & 5 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,21 @@ export type InitialState<T> = {
};
};

export type ValidateResult<T> = {
isValid: boolean;
data: T | null;
errors?: Partial<{ [K in keyof T]: T[K] }>;
};
export type ValidateResult<T> =
| {
isValid: true;
data: T;
}
| {
isValid: false;
data: null;
errors?: Partial<{ [K in keyof T]: T[K] }>;
};

export type useFormErrors<T> = {
[K in keyof T]?: string;
};

export interface UseFormReturn<T> {
data: T;
state: IState<T>;
Expand Down
Loading