dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
140 lines (138 loc) • 5.41 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
// src/validation-algorithms/validation-error/validation-error.ts
var validation_error_exports = {};
__export(validation_error_exports, {
AggregateValidationError: () => AggregateValidationError,
ValidationError: () => ValidationError
});
module.exports = __toCommonJS(validation_error_exports);
var import_concat_object_path = require("../../utilities/concat-object-path.js");
var import_dedent = __toESM(require("dedent"));
var ValidationError = class extends TypeError {
constructor(path, expected, value, customMessage) {
super(
customMessage != null ? customMessage : "value does not conform the data type structure definition"
);
__publicField(this, "ValidationError", true);
__publicField(this, "path");
__publicField(this, "isAggregate", false);
__publicField(this, "expectedValueType");
__publicField(this, "receivedValue");
/** @internal */
__publicField(this, "originType");
__publicField(this, "fieldPathCache", null);
this.expectedValueType = expected;
this.path = path;
this.receivedValue = value;
}
static isValidationError(e) {
return typeof e === "object" && e !== null && e instanceof TypeError && "ValidationError" in e && e.ValidationError === true;
}
get fieldPath() {
if (this.fieldPathCache == null) {
this.fieldPathCache = (0, import_concat_object_path.concatPath)(this.path.flatten());
}
return this.fieldPathCache;
}
get pathSegments() {
return this.path.flatten().map((p) => ({ key: p }));
}
details() {
return import_dedent.default`
ValidationError: ${this.message}
Path: ${this.fieldPath}
Expected: ${this.expectedValueType.toString()}
Got: ${typeof this.receivedValue}
`;
}
detailsJson() {
return {
err: "ValidationError",
message: this.message,
path: this.fieldPath,
expected: this.expectedValueType.toString(),
gotType: typeof this.receivedValue,
gotValue: this.receivedValue
};
}
};
var AggregateValidationError = class extends TypeError {
constructor(path, errors, customMessage) {
super(
customMessage != null ? customMessage : "value does not conform the data type structure definition"
);
__publicField(this, "errors", errors);
__publicField(this, "AggregateValidationError", true);
__publicField(this, "path");
__publicField(this, "isAggregate", true);
__publicField(this, "fieldPathCache", null);
this.path = path;
}
static isAggregateValidationError(e) {
return typeof e === "object" && e !== null && e instanceof TypeError && "AggregateValidationError" in e && e.AggregateValidationError === true;
}
get fieldPath() {
if (this.fieldPathCache == null) {
this.fieldPathCache = (0, import_concat_object_path.concatPath)(this.path.flatten());
}
return this.fieldPathCache;
}
get pathSegments() {
return this.path.flatten().map((p) => ({ key: p }));
}
details() {
const errorsDetails = this.errors.map((e) => {
let d = padLines(e.details(), " ");
if (ValidationError.isValidationError(e) && e.originType != null && e.expectedValueType !== e.originType) {
d = ` By: ${e.originType.toString()}
` + d;
}
return d;
});
return import_dedent.default`
AggregateValidationError: ${this.message}
Path: ${this.fieldPath}
Errors:
` + "\n" + errorsDetails.join("\n\n");
}
detailsJson() {
return {
err: "AggregateValidationError",
message: this.message,
path: this.fieldPath,
errors: this.errors.map((e) => e.detailsJson())
};
}
};
function padLines(text, pad) {
return text.split("\n").map((line) => pad + line).join("\n");
}