dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
111 lines (109 loc) • 3.75 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
// src/validation-algorithms/validation-error/validation-error.ts
import { concatPath } from "../../utilities/concat-object-path.mjs";
import dedent from "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 = concatPath(this.path.flatten());
}
return this.fieldPathCache;
}
get pathSegments() {
return this.path.flatten().map((p) => ({ key: p }));
}
details() {
return dedent`
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 = 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 dedent`
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");
}
export {
AggregateValidationError,
ValidationError
};