Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ae40c680de | |||
| 0cdc4e271b | |||
| 31b6eecdec | |||
| a088f97e2e | |||
| d2900bfd12 |
2
Makefile
2
Makefile
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
build/packages:
|
build/packages:
|
||||||
mkdir -p $@
|
mkdir -p $@
|
||||||
node tools/stage-for-pnpn.mjs package-manifest.yaml source $@
|
node tools/stage-for-pnpm.mjs package-manifest.yaml source $@
|
||||||
|
|
||||||
publish:
|
publish:
|
||||||
cd build/packages && ./publish-all.sh
|
cd build/packages && ./publish-all.sh
|
||||||
|
|||||||
50
experiments/config1.mjs
Normal file
50
experiments/config1.mjs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import { Schema, 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(null, string_coercion_function, null, 'Anything that could be a string');
|
||||||
|
const fd = new Field_Configuration(null, string_coercion_function, () => 'baz', 'A string. Defaults to \'baz\'');
|
||||||
|
|
||||||
|
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'));
|
||||||
|
|
||||||
|
|
||||||
|
const s = new Schema({
|
||||||
|
foo: fc,
|
||||||
|
bar: fd,
|
||||||
|
});
|
||||||
|
|
||||||
|
const s2 = new Schema({
|
||||||
|
thing: fc,
|
||||||
|
stuff: s,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(s2.load({
|
||||||
|
stuff: { foo: 'Hello World' },
|
||||||
|
thing: 123,
|
||||||
|
}));
|
||||||
|
|
||||||
|
// { thing: '123', stuff: { foo: 'Hello World', bar: 'baz' } }
|
||||||
9
experiments/config2.mjs
Normal file
9
experiments/config2.mjs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import * as F from '@efforting.tech/data/field-configuration-factories';
|
||||||
|
|
||||||
|
const s = new F.Schema({
|
||||||
|
foo: F.value(123, 'The value'),
|
||||||
|
bar: F.factory((t) => `Field ${t.field_name} was not set`, 'The factory'),
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
console.log(s.load()) // { foo: 123, bar: 'Field bar was not set' }
|
||||||
@@ -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
@@ -1,6 +1,6 @@
|
|||||||
scope: '@efforting.tech'
|
scope: '@efforting.tech'
|
||||||
registry: 'https://npm.efforting.tech/'
|
registry: 'https://npm.efforting.tech/'
|
||||||
version: 0.2.2
|
version: 0.2.5
|
||||||
|
|
||||||
author:
|
author:
|
||||||
name: 'Mikael Lövqvist'
|
name: 'Mikael Lövqvist'
|
||||||
@@ -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:
|
||||||
|
|||||||
5
package.json
Normal file
5
package.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"experiments": "^0.3.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
20
source/data/field-configuration-factories.mjs
Normal file
20
source/data/field-configuration-factories.mjs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import { Field_Configuration } from './field-configuration.mjs';
|
||||||
|
export { Schema } from './field-configuration.mjs';
|
||||||
|
|
||||||
|
//constructor(validation_function=null, coercion_function=null, factory_function=null, expected_description=undefined) {
|
||||||
|
|
||||||
|
export function value(default_value, description) {
|
||||||
|
return new Field_Configuration(null, null, () => default_value, description);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function factory(factory_function, description) {
|
||||||
|
return new Field_Configuration(null, null, factory_function, description);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function typed_value(coercion_function, default_value, description) {
|
||||||
|
return new Field_Configuration(null, coercion_function, () => default_value, description);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function typed_factory(coercion_function, factory_function, description) {
|
||||||
|
return new Field_Configuration(null, coercion_function, factory_function, description);
|
||||||
|
}
|
||||||
80
source/data/field-configuration.mjs
Normal file
80
source/data/field-configuration.mjs
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
import { Data_Validation_Failed, Data_Coercion_Failed, Superfluous_Data_Field } from '@efforting.tech/errors';
|
||||||
|
|
||||||
|
|
||||||
|
export class Field_Configuration {
|
||||||
|
constructor(validation_function=null, coercion_function=null, factory_function=null, expected_description=undefined) {
|
||||||
|
Object.assign(this, { validation_function, coercion_function, factory_function, expected_description });
|
||||||
|
}
|
||||||
|
|
||||||
|
check_validation(value) {
|
||||||
|
const { validation_function } = this;
|
||||||
|
return !validation_function || validation_function(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
validate(value, target=undefined) {
|
||||||
|
const { validation_function, expected_description } = this;
|
||||||
|
if (!this.check_validation(value)) {
|
||||||
|
throw new Data_Validation_Failed({
|
||||||
|
validation_function, value,
|
||||||
|
target, expected_description,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
coerce(value, target=undefined) {
|
||||||
|
const { coercion_function, expected_description } = this;
|
||||||
|
try {
|
||||||
|
return coercion_function ? coercion_function(value) : value;
|
||||||
|
} catch (e) {
|
||||||
|
throw new Data_Coercion_Failed({
|
||||||
|
coercion_function, value,
|
||||||
|
target, expected_description,
|
||||||
|
upstream_error: e,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
load(value=undefined, target=undefined) {
|
||||||
|
|
||||||
|
const { factory_function } = this;
|
||||||
|
|
||||||
|
if ((value === undefined) && factory_function) {
|
||||||
|
value = factory_function(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
const coerced_value = this.coerce(value, target);
|
||||||
|
this.validate(coerced_value, target);
|
||||||
|
return coerced_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export class Schema {
|
||||||
|
|
||||||
|
constructor(field_schema) {
|
||||||
|
Object.assign(this, { field_schema });
|
||||||
|
}
|
||||||
|
|
||||||
|
load(value={}, target=undefined) {
|
||||||
|
const { field_schema } = this;
|
||||||
|
|
||||||
|
for (const [value_name, value_value] of Object.entries(value)) {
|
||||||
|
if (!field_schema[value_name]) {
|
||||||
|
throw new Superfluous_Data_Field({
|
||||||
|
field_value: value_value, field_name: value_name, target,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = {};
|
||||||
|
for (const [field_name, field_config] of Object.entries(field_schema)) {
|
||||||
|
const sub_target = { schema: this, field_name, field_config, parent_target: target };
|
||||||
|
result[field_name] = field_config.load(value[field_name], sub_target);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,49 @@
|
|||||||
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, upstream_error } = data;
|
||||||
|
const type = value === null ? 'null' : typeof value;
|
||||||
|
|
||||||
|
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_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, upstream_error } = data;
|
||||||
|
const type = value === null ? 'null' : typeof value;
|
||||||
|
|
||||||
|
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_ref} 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,22 +89,34 @@ 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}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { description } = pkg;
|
const { description } = pkg;
|
||||||
|
const dependencies = {};
|
||||||
|
|
||||||
|
const internal_deps = pkg['internal-dependencies'] ?? [];
|
||||||
|
for (const idep of internal_deps) {
|
||||||
|
dependencies[path.join(scope, idep)] = version;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const pkg_json = JSON.stringify({
|
const pkg_json = JSON.stringify({
|
||||||
name: pkg_scope_path,
|
name: pkg_scope_path,
|
||||||
description,
|
description,
|
||||||
exports: exports_map,
|
exports: exports_map,
|
||||||
|
dependencies,
|
||||||
...common_package_data,
|
...common_package_data,
|
||||||
}, null, ' ');
|
}, null, ' ');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
writeFileSync(path.join(pkg_dir, 'package.json'), pkg_json, 'utf-8');
|
writeFileSync(path.join(pkg_dir, 'package.json'), pkg_json, 'utf-8');
|
||||||
//console.log({linked_sources}); // ['errors.mjs']
|
//console.log({linked_sources}); // ['errors.mjs']
|
||||||
|
|
||||||
root_package.dependencies[pkg_scope_path] = 'workspace:*';
|
root_package.dependencies[pkg_scope_path] = version;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user