Skip to main content
Module

x/value_schema/dist-deno/libs/ValueSchemaError.ts

simple, easy-to-use, and declarative input validator; supports Node.js, TypeScript, Deno, and Bun
Go to Latest
File
import { Key, Values } from "./types.ts";export const CAUSE = { TYPE: "type", UNDEFINED: "undefined", NULL: "null", EMPTY_STRING: "empty-string", ONLY: "only", CONVERTER: "converter", MIN_VALUE: "min-value", MAX_VALUE: "max-value", MIN_LENGTH: "min-length", MAX_LENGTH: "max-length", PATTERN: "pattern", CHECKSUM: "checksum"} as const;type CAUSE = typeof CAUSE[keyof typeof CAUSE];/** * Value-Schema Error */export class ValueSchemaError extends Error { public readonly cause: CAUSE; public readonly value: unknown; public readonly keyStack: Key[]; /** * throw an error * @param cause cause of error * @param values input/output values * @param keyStack path to key that caused error * @throws error object */ static raise(cause: CAUSE, values: Values, keyStack: Key[]): never { throw new ValueSchemaError(cause, values.input, keyStack); } /** * check whether error is instance of ValueSchemaError or not * @param err error to check * @returns Yes/No */ static is(err: unknown): err is ValueSchemaError { return err instanceof ValueSchemaError; } /** * constructor * @param cause cause of error * @param value input value * @param keyStack path to key that caused error */ constructor(cause: CAUSE, value: unknown, keyStack: Key[]) { super(`${cause}; ${value}; ${keyStack}`); this.name = "ValueSchemaError"; this.cause = cause; this.value = value; this.keyStack = [...keyStack]; }}