From a088f97e2e799b556f954a09ab6a4db4d0e0776a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20L=C3=B6vqvist?= Date: Wed, 8 Apr 2026 23:32:05 +0200 Subject: [PATCH] Started textnodes, renamed tool with wrong name, started field configuration system --- experiments/config.mjs | 16 +++++++++++++ experiments/package.json | 3 ++- experiments/textnodes.mjs | 0 package-manifest.yaml | 6 +++++ source/data/field-configuration.mjs | 24 +++++++++++++++++++ source/errors.mjs | 13 ++++++++++ ...{stage-for-pnpn.mjs => stage-for-pnpm.mjs} | 2 +- 7 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 experiments/config.mjs create mode 100644 experiments/textnodes.mjs create mode 100644 source/data/field-configuration.mjs rename tools/{stage-for-pnpn.mjs => stage-for-pnpm.mjs} (98%) diff --git a/experiments/config.mjs b/experiments/config.mjs new file mode 100644 index 0000000..2da6ad8 --- /dev/null +++ b/experiments/config.mjs @@ -0,0 +1,16 @@ +import { Field_Configuration } from '@efforting.tech/data/field-configuration'; + + +function mandatory_anything(value) { + return value !== undefined; +} + + +const fc = new Field_Configuration('Some_Field', mandatory_anything, 'Anything defined'); + + +console.log(fc.check_validation(undefined)); +console.log(fc.check_validation(true)); + +//fc.validate(undefined); +fc.validate(undefined, 'New thingamabob object'); diff --git a/experiments/package.json b/experiments/package.json index cfc1bee..e7e513a 100644 --- a/experiments/package.json +++ b/experiments/package.json @@ -3,6 +3,7 @@ "type": "module", "dependencies": { "@efforting.tech/errors": "link:../build/packages/errors", - "@efforting.tech/rule-processing": "link:../build/packages/rule-processing" + "@efforting.tech/rule-processing": "link:../build/packages/rule-processing", + "@efforting.tech/data": "link:../build/packages/data" } } \ No newline at end of file diff --git a/experiments/textnodes.mjs b/experiments/textnodes.mjs new file mode 100644 index 0000000..e69de29 diff --git a/package-manifest.yaml b/package-manifest.yaml index 5cb54ac..f225fdc 100644 --- a/package-manifest.yaml +++ b/package-manifest.yaml @@ -21,6 +21,12 @@ packages: internal-dependencies: - errors + data: + path: source/data + #documentation: documentation/data + description: Data management + internal-dependencies: + - errors wip-packages: object-graph-storage: diff --git a/source/data/field-configuration.mjs b/source/data/field-configuration.mjs new file mode 100644 index 0000000..9516d28 --- /dev/null +++ b/source/data/field-configuration.mjs @@ -0,0 +1,24 @@ +import { Invalid_Data } from '@efforting.tech/errors'; + +export class Field_Configuration { + constructor(name, validator=null, expected=undefined) { + Object.assign(this, { name, validator, expected }); + } + + check_validation(value) { + const { validator } = this; + + return !validator || validator(value); + } + + validate(value, target=undefined) { + const { validator, name, expected } = this; + if (!this.check_validation(value)) { + throw new Invalid_Data({ + name, validator, value, + target, expected, + }); + } + } + +} diff --git a/source/errors.mjs b/source/errors.mjs index 98d6a00..37622b0 100644 --- a/source/errors.mjs +++ b/source/errors.mjs @@ -1,5 +1,18 @@ import { inspect } from 'node:util'; +// § GROUP: Configuration field errors + +export class Invalid_Data extends Error { + constructor(data) { + const { value, target, expected, validator, name } = 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}".`); + this.data = data; + } +} + // § GROUP: Resolving errors export class Item_Unresolvable extends Error { diff --git a/tools/stage-for-pnpn.mjs b/tools/stage-for-pnpm.mjs similarity index 98% rename from tools/stage-for-pnpn.mjs rename to tools/stage-for-pnpm.mjs index eba2b3a..20ec7af 100644 --- a/tools/stage-for-pnpn.mjs +++ b/tools/stage-for-pnpm.mjs @@ -89,7 +89,7 @@ for (const [package_name, package_data] of Object.entries(manifest.packages)) { const exports_map = {}; for (const file of linked_sources) { const name = path.basename(file, '.mjs'); - const key = name === pkg.name ? '.' : `./${name}`; + const key = name === path.basename(pkg.name) ? '.' : `./${name}`; exports_map[key] = `./${file}`; }