diff --git a/src/core/validateField.ts b/src/core/validateField.ts index 00ed4a1..95e1cee 100644 --- a/src/core/validateField.ts +++ b/src/core/validateField.ts @@ -16,13 +16,16 @@ export function validateField( ): Array { const errors: Array = []; try { + + if (!_utils.isString(value)) + throw new TypeError("'value' must be a string"); + + if (!_utils.isString(name)) throw new TypeError("'name' must be a string"); + const configMap: ConfigMap = { minLength: (value: string, criterion: number, message: string) => { /*--- Error Guards ---*/ - if (!_utils.isString(value)) - throw new TypeError("'value' must be a string"); - if (!_utils.isNumber(criterion)) throw new TypeError("'criterion' must be a number"); @@ -38,9 +41,6 @@ export function validateField( maxLength: (value: string, criterion: number, message: string) => { /*--- Error Guards ---*/ - if (!_utils.isString(value)) - throw new TypeError("'value' must be a string"); - if (!_utils.isNumber(criterion)) throw new TypeError("'criterion' must be a number"); @@ -56,9 +56,6 @@ export function validateField( pattern: (value: string, criterion: RegExp, message: string) => { /*--- Error Guards ---*/ - if (!_utils.isString(value)) - throw new TypeError("'value' must be a string"); - // if (!_utils.isRegExp(criterion)) // throw new TypeError("'criterion' must be a RegExp"); @@ -72,9 +69,6 @@ export function validateField( custom: (value: string, criterion: CustomFunction, message: string) => { /*--- Error Guards ---*/ - if (!_utils.isString(value)) - throw new TypeError("'value' must be a string"); - // if (!_utils.isFunction(criterion)) // throw new TypeError("'criterion' must be a function"); @@ -90,9 +84,6 @@ export function validateField( required: (value: string, criterion: boolean, message: string) => { /*--- Error Guards ---*/ - if (!_utils.isString(value)) - throw new TypeError("'value' must be a string"); - // if (!_utils.isBoolean(criterion)) // throw new TypeError("'criterion' must be a boolean"); diff --git a/src/internal/do-keys-match.ts b/src/internal/do-keys-match.ts index 39d0de4..401ce70 100644 --- a/src/internal/do-keys-match.ts +++ b/src/internal/do-keys-match.ts @@ -1,18 +1,19 @@ import { FormDataShape, NameRuleMap } from "../types"; +import { MappingError } from "./error-management"; export function doKeysMatch(data: FormDataShape, NameRuleMap: NameRuleMap) { const dataKeys = Object.keys(data); const NameRuleMapKeys = Object.keys(NameRuleMap); if (dataKeys.length !== NameRuleMapKeys.length) { - throw new Error( + throw new MappingError( `Data keys and NameRuleMap keys do not match. Data keys: ${dataKeys}, NameRuleMap keys: ${NameRuleMapKeys}` ); } dataKeys.forEach((key) => { if (!NameRuleMapKeys.includes(key)) { - throw new Error( + throw new MappingError( `Data keys and NameRuleMap keys do not match. Data keys: ${dataKeys}, NameRuleMap keys: ${NameRuleMapKeys}` ); } diff --git a/src/internal/error-management.ts b/src/internal/error-management.ts index 39a4011..7ad91c2 100644 --- a/src/internal/error-management.ts +++ b/src/internal/error-management.ts @@ -2,7 +2,7 @@ type ERROR_NAMES = "TYPE_ERROR" | "MAPPING_ERROR"; class MappingError extends Error { constructor( - message: string = "Validation Error", + message: string = "Mapping Error", name: ERROR_NAMES = "MAPPING_ERROR" ) { super(); @@ -28,13 +28,13 @@ class TypeError extends Error { function handleError(error: Error): void { switch (error.constructor) { case TypeError: - console.log("Type Error:", error.message); + console.error("Type Error:", error.message); break; case MappingError: - console.log("Mapping Error:", error.message); + console.error("Mapping Error:", error.message); break; default: - console.log( + console.error( "Internal Error", error.message, "Please report this at" + diff --git a/src/types/index.ts b/src/types/index.ts index 4cdb3f9..e50cbf9 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,33 +1,43 @@ +export interface CustomFunction { + (value: string): boolean; +} + +export interface KeyValuePair { + [key: string]: string; +} + export type Criterion = string | number | CustomFunction | boolean | RegExp; -export interface Rule { - criterion: Criterion; +export interface Rule { + criterion: TCriterion; message: string; } -export type FormDataShape = KeyValuePair | { [k: string]: FormDataEntryValue }; +export interface RequiredRule extends Rule {} + +export interface MinLengthRule extends Rule {} + +export interface MaxLengthRule extends Rule {} + +export interface PatternRule extends Rule {} + +export interface CustomRule extends Rule {} export interface RulesObject { - required?: Rule; - minLength?: Rule; - maxLength?: Rule; - pattern?: Rule; - custom?: Rule; + required?: RequiredRule; + minLength?: MinLengthRule; + maxLength?: MaxLengthRule; + pattern?: PatternRule; + custom?: CustomRule; } +export type FormDataShape = KeyValuePair | { [k: string]: FormDataEntryValue }; + export interface FieldError { name: string; message: string; } -export interface CustomFunction { - (value: string): boolean; -} - -export interface KeyValuePair { - [key: string]: string; -} - export interface NameRuleMap { [key: string]: RulesObject; }