From 31b6eecdecf4c09729269fe812c5bbbd28e4e3eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20L=C3=B6vqvist?= Date: Thu, 9 Apr 2026 19:17:57 +0200 Subject: [PATCH] Added coercing system to field configuration, revised some of the data validation errors --- experiments/config.mjs | 19 ++++++++++++-- source/data/field-configuration.mjs | 39 +++++++++++++++++++++-------- source/errors.mjs | 20 ++++++++++++--- 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/experiments/config.mjs b/experiments/config.mjs index 2da6ad8..ed2c988 100644 --- a/experiments/config.mjs +++ b/experiments/config.mjs @@ -6,11 +6,26 @@ function mandatory_anything(value) { } -const fc = new Field_Configuration('Some_Field', mandatory_anything, 'Anything defined'); +function string_coercion_function(value) { + if (value === undefined) { + throw new Error('Undefined not allowed'); + } + return String(value); +} + +const fc = new Field_Configuration('Some_Field', null, string_coercion_function, 'Anything that could be a string'); + console.log(fc.check_validation(undefined)); console.log(fc.check_validation(true)); +console.log([ fc.coerce(123) ]); +console.log([ fc.coerce(true) ]); + +console.log([ fc.load(undefined, 'Some configuration') ]); +console.log([ fc.load(true) ]); + + //fc.validate(undefined); -fc.validate(undefined, 'New thingamabob object'); +console.log(fc.load(undefined, 'New thingamabob object')); diff --git a/source/data/field-configuration.mjs b/source/data/field-configuration.mjs index 9516d28..219a99b 100644 --- a/source/data/field-configuration.mjs +++ b/source/data/field-configuration.mjs @@ -1,24 +1,43 @@ -import { Invalid_Data } from '@efforting.tech/errors'; +import { Data_Validation_Failed, Data_Coercion_Failed } from '@efforting.tech/errors'; export class Field_Configuration { - constructor(name, validator=null, expected=undefined) { - Object.assign(this, { name, validator, expected }); + constructor(name, validation_function=null, coercion_function=null, expected_description=undefined) { + Object.assign(this, { name, validation_function, coercion_function, expected_description }); } check_validation(value) { - const { validator } = this; - - return !validator || validator(value); + const { validation_function } = this; + return !validation_function || validation_function(value); } validate(value, target=undefined) { - const { validator, name, expected } = this; + const { validation_function, name, expected_description } = this; if (!this.check_validation(value)) { - throw new Invalid_Data({ - name, validator, value, - target, expected, + throw new Data_Validation_Failed({ + name, validation_function, value, + target, expected_description, }); } } + coerce(value, target=undefined) { + const { coercion_function, name, expected_description } = this; + try { + return coercion_function ? coercion_function(value) : value; + } catch (e) { + throw new Data_Coercion_Failed({ + name, coercion_function, value, + target, expected_description, + upstream_error: e, + }) + } + } + + load(value, target=undefined) { + const coerced_value = this.coerce(value, target); + this.validate(coerced_value, target); + return coerced_value; + } + + } diff --git a/source/errors.mjs b/source/errors.mjs index 37622b0..aeed2c6 100644 --- a/source/errors.mjs +++ b/source/errors.mjs @@ -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 {