Skip to content

Commit

Permalink
api change
Browse files Browse the repository at this point in the history
  • Loading branch information
AmmarHalees committed Dec 22, 2023
1 parent 7d522f4 commit 09b60c6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
32 changes: 25 additions & 7 deletions src/core/validateField.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -8,9 +15,7 @@ export function validateField(
): Array<FieldError> {
const errors: Array<FieldError> = [];

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(
Expand Down Expand Up @@ -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<Criterion>;

if (validationFunction) {
validationFunction(value, criterion, message);
} else {
throw new Error(
`Validation function for ${key} does not exist in configMap`
);
}
}
});
} catch (e) {
Expand Down
11 changes: 9 additions & 2 deletions src/core/validateForm.ts
Original file line number Diff line number Diff line change
@@ -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<FieldError> = [];

/*-------- Error Guards --------*/
Expand Down
4 changes: 2 additions & 2 deletions src/internal/do-keys-match.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface Rule {
message: string;
}

export type FormDataShape = KeyValuePair | { [k: string]: FormDataEntryValue };

export interface RulesObject {
required?: Rule;
minLength?: Rule;
Expand Down

0 comments on commit 09b60c6

Please sign in to comment.