Finalized field configuration for now, fixed a bug in resolvers, added readme stub for data package

This commit is contained in:
2026-04-11 21:45:42 +02:00
parent ae40c680de
commit 25dc8b8d0f
4 changed files with 25 additions and 5 deletions

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

@@ -17,4 +17,12 @@ export function typed_value(coercion_function, default_value, description) {
export function typed_factory(coercion_function, factory_function, description) { export function typed_factory(coercion_function, factory_function, description) {
return new Field_Configuration(null, 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

@@ -53,12 +53,12 @@ export class Field_Configuration {
export class Schema { export class Schema {
constructor(field_schema) { constructor(field_schema, name=undefined) {
Object.assign(this, { field_schema }); Object.assign(this, { field_schema, name });
} }
load(value={}, target=undefined) { load(value={}, target=undefined) {
const { field_schema } = this; const { field_schema, name } = this;
for (const [value_name, value_value] of Object.entries(value)) { for (const [value_name, value_value] of Object.entries(value)) {
if (!field_schema[value_name]) { if (!field_schema[value_name]) {
@@ -69,8 +69,10 @@ export class Schema {
} }
const result = {}; const result = {};
const this_schema = name ? `schema "${name}"` : 'untitled schema';
for (const [field_name, field_config] of Object.entries(field_schema)) { for (const [field_name, field_config] of Object.entries(field_schema)) {
const sub_target = { schema: this, field_name, field_config, parent_target: target }; 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); result[field_name] = field_config.load(value[field_name], sub_target);
} }

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 });
} }