Skip to content

Commit

Permalink
errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AmmarHalees committed Dec 24, 2023
1 parent fd64dde commit 23d84c3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
75 changes: 38 additions & 37 deletions src/core/validateForm.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
import { doKeysMatch } from "../internal/do-keys-match";
import { RequiredParamError, handleError } from "../internal/error-management";
import { FieldError, NameRuleMap, RulesObject, FormDataShape } from "../types";
import _utils from "../utils";
import { validateField } from "./validateField";


export function validateForm(data: FormDataShape, NameRuleMap: NameRuleMap) {
const errors: Array<FieldError> = [];

/*-------- Error Guards --------*/

if (!NameRuleMap) throw new Error("NameRuleMap is required");
if (!data) throw new Error("Form Data is required");
if (_utils.isObject(data) && Object.keys(data).length === 0)
throw new Error("Form Data is required");

if (!_utils.isObject(data))
throw new Error(`Form Data cannot be ${typeof data}`);

if (_utils.isObject(NameRuleMap) && Object.keys(NameRuleMap).length === 0) {
throw new Error("NameRuleMap is required");
}

if (!_utils.isObject(NameRuleMap)) {
throw new Error(`NameRuleMap cannot be ${typeof NameRuleMap}`);
}

doKeysMatch(data, NameRuleMap);

/*-------- Normal Function --------*/

Object.entries(data).forEach(([key, value]) => {
let stringVal = "";

if (typeof value === "string") {
stringVal = value;
} else if (value instanceof FormData) {
stringVal = value.get(key) as string;
try {
if (!NameRuleMap) throw new RequiredParamError("NameRuleMap is required");
if (!data) throw new RequiredParamError("data is required");
if (!_utils.isObject(data))
throw new TypeError(`Form Data cannot be ${typeof data}`);
if (!_utils.isObject(NameRuleMap)) {
throw new TypeError(`NameRuleMap cannot be ${typeof NameRuleMap}`);
}

if (NameRuleMap) {
errors.push(
...validateField(
stringVal,
key,
NameRuleMap[key as keyof typeof NameRuleMap] as RulesObject
)
);
doKeysMatch(data, NameRuleMap);

/*-------- Normal Function --------*/

Object.entries(data).forEach(([key, value]) => {
let stringVal = "";

if (typeof value === "string") {
stringVal = value;
} else if (value instanceof FormData) {
stringVal = value.get(key) as string;
}

if (NameRuleMap) {
errors.push(
...validateField(
stringVal,
key,
NameRuleMap[key as keyof typeof NameRuleMap] as RulesObject
)
);
}
});
} catch (error) {
if (error instanceof Error) {
handleError(error);
} else {
console.log(error);
}
});
}

return errors;
}
5 changes: 4 additions & 1 deletion src/internal/do-keys-match.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { FormDataShape, NameRuleMap } from "../types";
import { MappingError } from "./error-management";

export function doKeysMatch(data: FormDataShape, NameRuleMap: NameRuleMap) {
export function doKeysMatch(
data: FormDataShape,
NameRuleMap: NameRuleMap
): void {
const dataKeys = Object.keys(data);
const NameRuleMapKeys = Object.keys(NameRuleMap);

Expand Down
14 changes: 12 additions & 2 deletions src/internal/error-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ class CustomTypeError extends Error {
}
}

// RequiredParamError

class RequiredParamError extends Error {
constructor(paramName: string) {
super();
this.name = "RequiredParamError";
this.message = `${paramName} is required`;
}
}

function handleError(error: Error) {
if (error.name === "TYPE_ERROR") {
console.error("Type Error:", error.message);
Expand All @@ -31,8 +41,8 @@ function handleError(error: Error) {
console.error(
"Internal Error",
error.message,
"Please report this at https://github.com/AmmarHalees/onsubmit/issues"
"Please report this at" + "https://github.com/AmmarHalees/onsubmit/issues"
);
}
}
export { MappingError, CustomTypeError, handleError };
export { MappingError, CustomTypeError, RequiredParamError, handleError };

0 comments on commit 23d84c3

Please sign in to comment.