Added coercing system to field configuration, revised some of the data validation errors

This commit is contained in:
2026-04-09 19:17:57 +02:00
parent a088f97e2e
commit 31b6eecdec
3 changed files with 62 additions and 16 deletions

View File

@@ -2,17 +2,29 @@ import { inspect } from 'node:util';
// § GROUP: Configuration field errors
export class Invalid_Data extends Error {
export class Data_Validation_Failed extends Error {
constructor(data) {
const { value, target, expected, validator, name } = data;
const { value, target, expected_description, validation_function, name, upstream_error } = data;
const type = value === null ? 'null' : typeof value;
const target_info = target ? inspect(target) : 'unknown target';
const expected_desc = expected ?? `data that would pass validation using ${validator}`;
super(`Data validation failed for field "${name}" of ${target_info}. Encountered data of type "${type}" while expecting "${expected_desc}".`);
const expected_desc = expected_description ?? `data that would pass validation using ${validation_function}`;
const upstream_error_description = upstream_error ? ` Upstream error was: ${upstream_error}` : '';
super(`Data validation failed for field "${name}" of ${target_info}. Encountered data of type "${type}" while expecting "${expected_desc}".${upstream_error_description}`);
this.data = data;
}
}
export class Data_Coercion_Failed extends Error {
constructor(data) {
const { value, target, expected_description, coercion_function, name, upstream_error } = data;
const type = value === null ? 'null' : typeof value;
const target_info = target ? inspect(target) : 'unknown target';
const expected_desc = expected_description ?? `data that would be coerced using ${coercion_function}`;
const upstream_error_description = upstream_error ? ` Upstream error was: ${upstream_error}` : '';
super(`Data coercion failed for field "${name}" of ${target_info}. Encountered data of type "${type}" while expecting "${expected_desc}".${upstream_error_description}`);
this.data = data;
}
}
// § GROUP: Resolving errors
export class Item_Unresolvable extends Error {