Finalized field configuration for now, fixed a bug in resolvers, added readme stub for data package
This commit is contained in:
@@ -17,4 +17,12 @@ 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