nralcm
Version:
This is a framework based on NodeJs to manage rest api request lifecycle
46 lines (45 loc) • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Checks for valid value
* @param type type of value
* @param value Value to be checked
* @returns true|false
*/
function isValidType(type, value) {
if (type.name === "Number") {
return !isNaN(value);
}
if (type.name === "String" && typeof value === "string") {
return true;
}
if (type.name === "Boolean" && (value.toLowerCase() === "true" || value.toLowerCase() === "false")) {
return true;
}
if (type.name === "Array") {
return checkValidArray(value);
}
if (type.constructor && type.constructor.name === "Date") {
return Date.parse(value) !== NaN;
}
try {
JSON.parse(JSON.stringify(value));
return true;
}
catch (e) {
return false;
}
}
exports.isValidType = isValidType;
/**
* Check for valid array
* @param str array string
* @returns true|false
*/
function checkValidArray(str) {
const arrayStr = str.normalize();
if (arrayStr.startsWith("[") && arrayStr.endsWith("]")) {
return true;
}
return false;
}