dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
58 lines (56 loc) • 2.44 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __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, {
ValidationError: () => ValidationError
});
module.exports = __toCommonJS(validation_error_exports);
var import_concat_object_path = require("../../utilities/concat-object-path.js");
var ValidationErrorSymbol = Symbol("ValidationError");
var _a, _b;
var ValidationError = class extends (_b = TypeError, _a = ValidationErrorSymbol, _b) {
constructor(path, expected, value, customMessage) {
super(
customMessage != null ? customMessage : "value does not conform the data type structure definition"
);
__publicField(this, _a, true);
__publicField(this, "path");
__publicField(this, "expectedValueType");
__publicField(this, "receivedValue");
__publicField(this, "fieldPathCache", null);
this.expectedValueType = expected;
this.path = path;
this.receivedValue = value;
}
static isValidationError(e) {
return typeof e === "object" && e !== null && e instanceof Error && ValidationErrorSymbol in e;
}
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 }));
}
};