Compare commits
2 Commits
v0.2.4
...
31b6eecdec
| Author | SHA1 | Date | |
|---|---|---|---|
| 31b6eecdec | |||
| a088f97e2e |
31
experiments/config.mjs
Normal file
31
experiments/config.mjs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { Field_Configuration } from '@efforting.tech/data/field-configuration';
|
||||||
|
|
||||||
|
|
||||||
|
function mandatory_anything(value) {
|
||||||
|
return value !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
console.log(fc.load(undefined, 'New thingamabob object'));
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@efforting.tech/errors": "link:../build/packages/errors",
|
"@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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
0
experiments/textnodes.mjs
Normal file
0
experiments/textnodes.mjs
Normal file
@@ -21,6 +21,12 @@ packages:
|
|||||||
internal-dependencies:
|
internal-dependencies:
|
||||||
- errors
|
- errors
|
||||||
|
|
||||||
|
data:
|
||||||
|
path: source/data
|
||||||
|
#documentation: documentation/data
|
||||||
|
description: Data management
|
||||||
|
internal-dependencies:
|
||||||
|
- errors
|
||||||
|
|
||||||
wip-packages:
|
wip-packages:
|
||||||
object-graph-storage:
|
object-graph-storage:
|
||||||
|
|||||||
43
source/data/field-configuration.mjs
Normal file
43
source/data/field-configuration.mjs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import { Data_Validation_Failed, Data_Coercion_Failed } from '@efforting.tech/errors';
|
||||||
|
|
||||||
|
export class Field_Configuration {
|
||||||
|
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 { validation_function } = this;
|
||||||
|
return !validation_function || validation_function(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
validate(value, target=undefined) {
|
||||||
|
const { validation_function, name, expected_description } = this;
|
||||||
|
if (!this.check_validation(value)) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,30 @@
|
|||||||
import { inspect } from 'node:util';
|
import { inspect } from 'node:util';
|
||||||
|
|
||||||
|
// § GROUP: Configuration field errors
|
||||||
|
|
||||||
|
export class Data_Validation_Failed extends Error {
|
||||||
|
constructor(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_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
|
// § GROUP: Resolving errors
|
||||||
|
|
||||||
export class Item_Unresolvable extends Error {
|
export class Item_Unresolvable extends Error {
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ for (const [package_name, package_data] of Object.entries(manifest.packages)) {
|
|||||||
const exports_map = {};
|
const exports_map = {};
|
||||||
for (const file of linked_sources) {
|
for (const file of linked_sources) {
|
||||||
const name = path.basename(file, '.mjs');
|
const name = path.basename(file, '.mjs');
|
||||||
const key = name === pkg.name ? '.' : `./${name}`;
|
const key = name === path.basename(pkg.name) ? '.' : `./${name}`;
|
||||||
exports_map[key] = `./${file}`;
|
exports_map[key] = `./${file}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user