@metamask/superstruct
Version:
A simple and composable way to validate data in JavaScript (and TypeScript).
29 lines • 1.13 kB
JavaScript
/**
* A `StructFailure` represents a single specific failure in validation.
*/
/**
* `StructError` objects are thrown (or returned) when validation fails.
*
* Validation logic is design to exit early for maximum performance. The error
* represents the first error encountered during validation. For more detail,
* the `error.failures` property is a generator function that can be run to
* continue validation and receive all the failures in the data.
*/
export class StructError extends TypeError {
constructor(failure, failures) {
let cached;
const { message, explanation, ...rest } = failure;
const { path } = failure;
const cause = path.length === 0 ? message : `At path: ${path.join('.')} -- ${message}`;
super(explanation ?? cause);
if (explanation !== null && explanation !== undefined) {
this.cause = cause;
}
Object.assign(this, rest);
this.name = this.constructor.name;
this.failures = () => {
return (cached ?? (cached = [failure, ...failures()]));
};
}
}
//# sourceMappingURL=error.mjs.map