2 Commits

5 changed files with 26 additions and 6 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

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

View File

@@ -18,3 +18,11 @@ export function typed_value(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

@@ -53,12 +53,12 @@ export class Field_Configuration {
export class Schema {
constructor(field_schema) {
Object.assign(this, { field_schema });
constructor(field_schema, name=undefined) {
Object.assign(this, { field_schema, name });
}
load(value={}, target=undefined) {
const { field_schema } = this;
const { field_schema, name } = this;
for (const [value_name, value_value] of Object.entries(value)) {
if (!field_schema[value_name]) {
@@ -69,8 +69,10 @@ export class Schema {
}
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, 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);
}

View File

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