4 Commits

11 changed files with 176 additions and 47 deletions

View File

@@ -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

View File

@@ -0,0 +1,9 @@
# @efforting.tech/data
Data processing modules.
**TODO:** *Explain in more detail what this vague description actually refers to.*
## field-configuration-factories.mjs
Currently there are a few factories defined but we might add more as specific needs arises throughout the library.

View File

@@ -1,31 +0,0 @@
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'));

50
experiments/config1.mjs Normal file
View 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
View 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' }

View File

@@ -1,6 +1,6 @@
scope: '@efforting.tech' scope: '@efforting.tech'
registry: 'https://npm.efforting.tech/' registry: 'https://npm.efforting.tech/'
version: 0.2.4 version: 0.2.6
author: author:
name: 'Mikael Lövqvist' name: 'Mikael Lövqvist'

5
package.json Normal file
View File

@@ -0,0 +1,5 @@
{
"dependencies": {
"experiments": "^0.3.0"
}
}

View File

@@ -0,0 +1,28 @@
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);
}
export function required(description) {
return new Field_Configuration((value) => value !== undefined, null, null, description);
}
export function typed_required(coercion_function, description) {
return new Field_Configuration((value) => value !== undefined, coercion_function, null, description);
}

View File

@@ -1,8 +1,9 @@
import { Data_Validation_Failed, Data_Coercion_Failed } from '@efforting.tech/errors'; import { Data_Validation_Failed, Data_Coercion_Failed, Superfluous_Data_Field } from '@efforting.tech/errors';
export class Field_Configuration { export class Field_Configuration {
constructor(name, validation_function=null, coercion_function=null, expected_description=undefined) { constructor(validation_function=null, coercion_function=null, factory_function=null, expected_description=undefined) {
Object.assign(this, { name, validation_function, coercion_function, expected_description }); Object.assign(this, { validation_function, coercion_function, factory_function, expected_description });
} }
check_validation(value) { check_validation(value) {
@@ -11,29 +12,36 @@ export class Field_Configuration {
} }
validate(value, target=undefined) { validate(value, target=undefined) {
const { validation_function, name, expected_description } = this; const { validation_function, expected_description } = this;
if (!this.check_validation(value)) { if (!this.check_validation(value)) {
throw new Data_Validation_Failed({ throw new Data_Validation_Failed({
name, validation_function, value, validation_function, value,
target, expected_description, target, expected_description,
}); });
} }
} }
coerce(value, target=undefined) { coerce(value, target=undefined) {
const { coercion_function, name, expected_description } = this; const { coercion_function, expected_description } = this;
try { try {
return coercion_function ? coercion_function(value) : value; return coercion_function ? coercion_function(value) : value;
} catch (e) { } catch (e) {
throw new Data_Coercion_Failed({ throw new Data_Coercion_Failed({
name, coercion_function, value, coercion_function, value,
target, expected_description, target, expected_description,
upstream_error: e, upstream_error: e,
}) })
} }
} }
load(value, target=undefined) { 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); const coerced_value = this.coerce(value, target);
this.validate(coerced_value, target); this.validate(coerced_value, target);
return coerced_value; return coerced_value;
@@ -41,3 +49,34 @@ export class Field_Configuration {
} }
export class Schema {
constructor(field_schema, name=undefined) {
Object.assign(this, { field_schema, name });
}
load(value={}, target=undefined) {
const { field_schema, name } = 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 = {};
const this_schema = name ? `schema "${name}"` : 'untitled schema';
for (const [field_name, field_config] of Object.entries(field_schema)) {
const sub_target = { schema: this, name: field_name, config: field_config, parent_target: target, info: `Field "${field_name}" of ${this_schema}` };
result[field_name] = field_config.load(value[field_name], sub_target);
}
return result;
}
}

View File

@@ -4,24 +4,43 @@ import { inspect } from 'node:util';
export class Data_Validation_Failed extends Error { export class Data_Validation_Failed extends Error {
constructor(data) { 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 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 expected_desc = expected_description ?? `data that would pass validation using ${validation_function}`;
const upstream_error_description = upstream_error ? ` Upstream error was: ${upstream_error}` : ''; 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; this.data = data;
} }
} }
export class Data_Coercion_Failed extends Error { export class Data_Coercion_Failed extends Error {
constructor(data) { 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 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 expected_desc = expected_description ?? `data that would be coerced using ${coercion_function}`;
const upstream_error_description = upstream_error ? ` Upstream error was: ${upstream_error}` : ''; 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; this.data = data;
} }
} }

View File

@@ -30,6 +30,7 @@ export class Chained_Resolver extends Abstract_Resolver {
export class Mapping_Resolver extends Abstract_Resolver { export class Mapping_Resolver extends Abstract_Resolver {
constructor(rules=new Map(), key_function=null) { constructor(rules=new Map(), key_function=null) {
super();
Object.assign(this, { rules, key_function }); Object.assign(this, { rules, key_function });
} }