emd-utils
Version:
a custom utils for using javascript
79 lines (71 loc) • 2.23 kB
JavaScript
import { constants } from "./constants";
const checks = {
isEmpty: (inputValue) => {
if (inputValue.length === 0) {
return "Input cannot be empty.";
}
},
isTooShort: (inputValue, length) => {
if (inputValue.length < length) {
return `Input must be at least ${length} characters long.`;
}
},
isTooLong: (inputValue, length) => {
if (inputValue.length > length) {
return `Input cannot be longer than ${length} characters.`;
}
},
isValidByRegex: (inputValue, regExp, errorMsg) => {
if (!regExp.test(inputValue)) {
return errorMsg;
}
},
isNumber: (inputValue) => {
if (isNaN(inputValue)) {
return "Input must be a number.";
}
}
};
export const validateInput = (fieldType, inputValue) => {
const regexList = {
[]: /^[a-zA-Z0-9]*$/,
[]: /^[a-zA-Z0-9 ]*$/,
};
const fieldRules = {
[]: [
[],
[],
[],
[], "cannot contain special characters or spaces"]
],
[]: [
[],
[],
[],
[], "cannot contain special characters"]
],
[]: [
[],
[],
[],
[],
],
[]: [
[],
[],
[],
],
};
if (!fieldRules[fieldType]) {
return { isError: false, errorMessage: "" };
}
for (let rule of fieldRules[fieldType]) {
const [check, ...params] = rule;
const errorMessage = check(...params);
if (errorMessage) {
return { isError: true, errorMessage };
}
}
return { isError: false, errorMessage: "" };
};
export default validateInput;