Compare commits
2 Commits
ae40c680de
...
v0.2.6
| Author | SHA1 | Date | |
|---|---|---|---|
| 8442383fc3 | |||
| 25dc8b8d0f |
9
documentation/data/readme.md
Normal file
9
documentation/data/readme.md
Normal 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.
|
||||
@@ -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'
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user