@chix/flow
Version:
67 lines • 2.4 kB
JavaScript
;
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
var instanceOf = require("instance-of");
var lodash_1 = require("lodash");
function validateData(type, data) {
var e_1, _a;
if (Array.isArray(type)) {
try {
for (var type_1 = __values(type), type_1_1 = type_1.next(); !type_1_1.done; type_1_1 = type_1.next()) {
var _type = type_1_1.value;
if (validateData(_type, data)) {
return true;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (type_1_1 && !type_1_1.done && (_a = type_1.return)) _a.call(type_1);
}
finally { if (e_1) throw e_1.error; }
}
return false;
}
switch (type) {
case 'string':
return typeof data === 'string';
case 'enum':
console.warn('TODO: validate enum correctly');
return typeof data === 'string';
case 'array':
return Object.prototype.toString.call(data) === '[object Array]';
case 'integer':
case 'number':
return Object.prototype.toString.call(data) === '[object Number]';
case 'null':
type = type.charAt(0).toUpperCase() + type.slice(1);
return Object.prototype.toString.call(data) === '[object ' + type + ']';
case 'boolean':
case 'bool':
return data === true || data === false || data === 0 || data === 1;
case 'any':
return true;
case 'object':
if (lodash_1.isPlainObject(data)) {
return true;
}
return instanceOf(data, type);
case 'function':
return true;
default:
return instanceOf(data, type);
}
}
exports.validateData = validateData;
//# sourceMappingURL=validateData.js.map