-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd64dde
commit 23d84c3
Showing
3 changed files
with
54 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters