Skip to content

Commit

Permalink
typing
Browse files Browse the repository at this point in the history
  • Loading branch information
AmmarHalees committed Dec 24, 2023
1 parent 3134476 commit b19a452
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
21 changes: 6 additions & 15 deletions src/core/validateField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ export function validateField(
): Array<FieldError> {
const errors: Array<FieldError> = [];
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");

Expand All @@ -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");

Expand All @@ -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");

Expand All @@ -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");

Expand All @@ -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");

Expand Down
5 changes: 3 additions & 2 deletions src/internal/do-keys-match.ts
Original file line number Diff line number Diff line change
@@ -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}`
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/internal/error-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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" +
Expand Down
42 changes: 26 additions & 16 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -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<TCriterion = Criterion> {
criterion: TCriterion;
message: string;
}

export type FormDataShape = KeyValuePair | { [k: string]: FormDataEntryValue };
export interface RequiredRule extends Rule<boolean> {}

export interface MinLengthRule extends Rule<number> {}

export interface MaxLengthRule extends Rule<number> {}

export interface PatternRule extends Rule<RegExp> {}

export interface CustomRule extends Rule<CustomFunction> {}

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;
}
Expand Down

0 comments on commit b19a452

Please sign in to comment.