Added basic schema to field-configuration

This commit is contained in:
2026-04-09 19:55:38 +02:00
parent 31b6eecdec
commit 0cdc4e271b
3 changed files with 93 additions and 19 deletions

View File

@@ -4,24 +4,43 @@ import { inspect } from 'node:util';
export class Data_Validation_Failed extends Error {
constructor(data) {
const { value, target, expected_description, validation_function, name, upstream_error } = data;
const { value, target, expected_description, validation_function, upstream_error } = data;
const type = value === null ? 'null' : typeof value;
const target_info = target ? inspect(target) : 'unknown target';
const target_info = target?.info ? inspect(target.info) : 'unknown target';
const field_ref = target?.name ? `field "${target.name}"` : 'unknown field';
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}`);
super(`Data validation failed for ${field_ref} of ${target_info}. Encountered data of type "${type}" while expecting "${expected_desc}".${upstream_error_description}`);
this.data = data;
}
}
export class Superfluous_Data_Field extends Error {
constructor(data) {
const { field_value, field_name, target, upstream_error } = data;
const type = field_value === null ? 'null' : typeof field_value;
const target_info = target?.info ? inspect(target.info) : 'unknown target';
const upstream_error_description = upstream_error ? ` Upstream error was: ${upstream_error}` : '';
super(`Data validation failed for ${target_info}. Superfluous field "${field_name}" with value of type "${type}" encountered.${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 { value, target, expected_description, coercion_function, upstream_error } = data;
const type = value === null ? 'null' : typeof value;
const target_info = target ? inspect(target) : 'unknown target';
const target_info = target?.info ? inspect(target.info) : 'unknown target';
const field_ref = target?.name ? `field "${target.name}"` : 'unknown field';
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}`);
super(`Data coercion failed for ${field_ref} of ${target_info}. Encountered data of type "${type}" while expecting "${expected_desc}".${upstream_error_description}`);
this.data = data;
}
}