@decaf-ts/decorator-validation
Version:
simple decorator based validation engine
57 lines • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelErrorDefinition = void 0;
/**
* @summary Helper Class to hold the error results
* @description holds error results in an 'indexable' manner
* while still providing the same result on toString
*
* @param {ModelErrors} errors
*
* @class ModelErrorDefinition
*
* @category Model
*/
class ModelErrorDefinition {
constructor(errors) {
for (const prop in errors) {
if (Object.prototype.hasOwnProperty.call(errors, prop) && errors[prop])
Object.defineProperty(this, prop, {
enumerable: true,
configurable: false,
value: errors[prop],
writable: false,
});
}
}
/**
* @summary Outputs the class to a nice readable string
*
* @override
*/
toString() {
const self = this;
return Object.keys(self)
.filter((k) => Object.prototype.hasOwnProperty.call(self, k) &&
typeof self[k] !== "function")
.reduce((accum, prop) => {
let propError = Object.keys(self[prop]).reduce((propAccum, key) => {
if (!propAccum)
propAccum = self[prop][key];
else
propAccum += `\n${self[prop][key]}`;
return propAccum;
}, undefined);
if (propError) {
propError = `${prop} - ${propError}`;
if (!accum)
accum = propError;
else
accum += `\n${propError}`;
}
return accum;
}, "");
}
}
exports.ModelErrorDefinition = ModelErrorDefinition;
//# sourceMappingURL=ModelErrorDefinition.js.map