ducktyper
Version:
A tool for validating input based on the fundamental js idea of duck typing. The object of this is to allow for clearer input validation and specification from the perspective of the developer. It will reduce the amount of time it takes to do input valida
79 lines (78 loc) • 3.15 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.duckfaults = exports.makeDuck = void 0;
const sortDucks_1 = __importDefault(require("./sortDucks"));
const makeDuckValidator_1 = __importDefault(require("./makeDuckValidator"));
const mergeObjects_1 = __importDefault(require("./mergeObjects"));
const isObject_1 = __importDefault(require("./is/isObject"));
const settings_1 = require("./settings");
//should the user pass in input name? or can it be collected from else where?
function makeDuck(...args) {
if (args.length === 0 || args === undefined) {
throw new Error("no arguments passed into ducker unable to make type");
}
const [isDucks, others] = (0, sortDucks_1.default)(args);
const validators = isDucks.concat(others.map(makeDuckValidator_1.default));
//never rename this
function isDuck(obj, options) {
options = (0, mergeObjects_1.default)(settings_1.ISDUCK_OPTIONS, options || {});
//dumb edge case that should be refactored so that it does not exist up here.
if (options.allowUndefined === false && (obj === undefined || obj === null)) {
if (options.throw) {
throw new Error(options.message);
}
else {
return false;
}
}
else if (options.allowUndefined === true && (obj === undefined || obj === null)) {
return true;
}
try {
//for some reason this throws a bug with the default values.
const valid = validators.map(validator => {
return validator(obj, {
throw: options.throw,
allowEmpty: options.allowEmpty,
allowEmptyString: options.allowEmptyString,
allowEmptyArray: options.allowEmptyArray,
});
}).every((results) => results === true);
if (valid === false) {
throw new Error(options.message);
}
return true;
}
catch (e) {
if (options.throw) {
//return child message false then we return parent
if (options.childMessage === false) {
throw new Error(options.message);
}
throw e;
}
else {
return false;
}
}
}
isDuck.isDuck = true;
return isDuck;
}
exports.makeDuck = makeDuck;
function duckfaults(duck, options) {
if (!(0, isObject_1.default)(options)) {
throw new Error("options must be an object");
}
let updated = (0, mergeObjects_1.default)(settings_1.ISDUCK_OPTIONS, options);
//never rename this
function isDuck(obj, options) {
return duck(obj, (0, mergeObjects_1.default)(updated, options));
}
isDuck.isDuck = true;
return isDuck;
}
exports.duckfaults = duckfaults;