specified
Version:
Type-safe typescript data specification verification
81 lines (80 loc) • 4.38 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var ValidationError = (function () {
function ValidationError(message, options) {
this.key = options ? options.key : undefined;
this.message = message;
this.code = options ? options.code : "";
this.value = options ? options.value : undefined;
this.allowed = options ? options.allowed : undefined;
this.nestedErrors = options && options.nestedErrors ? options.nestedErrors.map(function (ne) { return new ValidationError(ne.message, ne); }) : [];
}
ValidationError.prototype.getKey = function () {
return this.key;
};
ValidationError.prototype.getNestedErrors = function () {
return this.nestedErrors;
};
ValidationError.prototype.generateReportJson = function () {
return exports.FormatValidationFailure.generateReportJson(this);
};
ValidationError.prototype.generateErrorPathList = function () {
return exports.FormatValidationFailure.generateErrorPathList(this);
};
ValidationError.prototype.toString = function () {
return "ValidationError: " + this.message;
};
return ValidationError;
}());
exports.ValidationError = ValidationError;
exports.FormatValidationFailure = (function () {
var generateReportJsonImpl = function (err, options) {
var keyProp = typeof err.key === "undefined" ? {} : { key: err.key };
var nestedProp = err.nestedErrors ? { nested: err.nestedErrors.map(function (ve) { return generateReportJsonImpl(ve, options); }) } : {};
return __assign({}, (options.include.message && { msg: err.message }), (options.include.code && { code: err.code }), (options.include.value && { value: err.value }), (options.include.allowed && { allowed: err.allowed }), keyProp, nestedProp);
};
var generateErrorPathListImpl = function (err, options) {
var pathKey = typeof err.key !== "undefined" ? [err.key] : [];
if (err.nestedErrors) {
return err.nestedErrors.reduce(function (acc, ne) { return acc.concat(generateErrorPathListImpl(ne, options).map(function (nep) { return (__assign({ path: pathKey.concat(nep.path) }, (options.include.message && { msg: nep.msg }), (options.include.code && { code: nep.code }), (options.include.value && { value: nep.value }), (options.include.allowed && { allowed: nep.allowed }))); })); }, []);
}
else {
return [__assign({ path: pathKey }, (options.include.message && { msg: err.message }), (options.include.code && { code: err.code }), (options.include.value && { value: err.value }), (options.include.allowed && { allowed: err.allowed }))];
}
};
return {
generateReportJson: function (err, options) {
var optionsImpl = {
include: {
message: !options || !options.include || !options.include.hasOwnProperty("message") || options.include.message || false,
code: options && options.include && options.include.code || false,
value: options && options.include && options.include.value || false,
allowed: options && options.include && options.include.allowed || false
}
};
return generateReportJsonImpl(err, optionsImpl);
},
generateErrorPathList: function (err, options) {
var optionsImpl = {
include: {
message: !options || !options.include || !options.include.hasOwnProperty("message") || options.include.message || false,
code: options && options.include && options.include.code || false,
value: options && options.include && options.include.value || false,
allowed: options && options.include && options.include.allowed || false
}
};
return generateErrorPathListImpl(err, optionsImpl);
}
};
})();