From 09b60c67b00ef48c3016cf453dfd62557036020b Mon Sep 17 00:00:00 2001 From: Ammar Halees Date: Sat, 23 Dec 2023 01:44:45 +0300 Subject: [PATCH] api change --- src/core/validateField.ts | 32 +++++++++++++++++++++++++------- src/core/validateForm.ts | 11 +++++++++-- src/internal/do-keys-match.ts | 4 ++-- src/types/index.ts | 2 ++ 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/core/validateField.ts b/src/core/validateField.ts index fb5445a..7cf98d7 100644 --- a/src/core/validateField.ts +++ b/src/core/validateField.ts @@ -1,4 +1,11 @@ -import { RulesObject, FieldError, CustomFunction } from "../types"; +import { + RulesObject, + FieldError, + CustomFunction, + ConfigMap, + ValidationFunction, + Criterion, +} from "../types"; import _utils from "../utils"; export function validateField( @@ -8,9 +15,7 @@ export function validateField( ): Array { const errors: Array = []; - const configMap: { - [key: string]: (value: string, criterion: any, message: string) => void; - } = { + const configMap: ConfigMap = { minLength: (value: string, criterion: number, message: string) => { if (!_utils.isString(value)) throw new Error( @@ -44,11 +49,24 @@ export function validateField( }, }; + function isKeyOfConfigMap(key: string): key is keyof ConfigMap { + return key in configMap; + } + try { Object.entries(rulesObject).forEach(([key, { criterion, message }]) => { - if (configMap[key] && configMap[key] !== undefined) { - const defaultFunction = () => {}; - (configMap[key] || defaultFunction)(value, criterion, message); + if (isKeyOfConfigMap(key)) { + const validationFunction = configMap[ + key as keyof ConfigMap + ] as ValidationFunction; + + if (validationFunction) { + validationFunction(value, criterion, message); + } else { + throw new Error( + `Validation function for ${key} does not exist in configMap` + ); + } } }); } catch (e) { diff --git a/src/core/validateForm.ts b/src/core/validateForm.ts index e313027..e24335b 100644 --- a/src/core/validateForm.ts +++ b/src/core/validateForm.ts @@ -1,9 +1,16 @@ import { doKeysMatch } from "../internal/do-keys-match"; -import { FieldError, NameRuleMap, KeyValuePair, RulesObject } from "../types"; +import { + FieldError, + NameRuleMap, + KeyValuePair, + RulesObject, + FormDataShape, +} from "../types"; import _utils from "../utils"; import { validateField } from "./validateField"; -export function validateForm(data: KeyValuePair | FormData, NameRuleMap: NameRuleMap) { + +export function validateForm(data: FormDataShape, NameRuleMap: NameRuleMap) { const errors: Array = []; /*-------- Error Guards --------*/ diff --git a/src/internal/do-keys-match.ts b/src/internal/do-keys-match.ts index 0b0ce70..39d0de4 100644 --- a/src/internal/do-keys-match.ts +++ b/src/internal/do-keys-match.ts @@ -1,6 +1,6 @@ -import { KeyValuePair, NameRuleMap } from "../types"; +import { FormDataShape, NameRuleMap } from "../types"; -export function doKeysMatch(data: KeyValuePair | FormData, NameRuleMap: NameRuleMap) { +export function doKeysMatch(data: FormDataShape, NameRuleMap: NameRuleMap) { const dataKeys = Object.keys(data); const NameRuleMapKeys = Object.keys(NameRuleMap); diff --git a/src/types/index.ts b/src/types/index.ts index 89f5907..4cdb3f9 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -5,6 +5,8 @@ export interface Rule { message: string; } +export type FormDataShape = KeyValuePair | { [k: string]: FormDataEntryValue }; + export interface RulesObject { required?: Rule; minLength?: Rule;