MetaForge V0.7.0
Update 2023-11-08
Calculated fields
Calculated fields supposed to do preprocessing of your schema;
Warning: experimental. We do not support some types yet: Map, Set
Example
const schema = {
$id: 'user',
name: 'string',
phrase: (sample, schema, root) => 'Hello ' + schema.name + ' !',
};
const sample = { name: 'Alexander' };
new Schema(schema).calc(sample); // { ..., name: 'Alexander', phrase: 'Hello Alexander !'};
schema; // { $id: 'user', name: 'Alexander', phrase: 'Hello Alexander !'};
Writing calculated fields
Calculated fields is a function that receives two arguments:
- root: root object
{ input: Sample }
- parent: assigned target object
Warning: your return value will be assigned to samples
Additional
Method schema.calc
receives mode as second parameter; This method allow to specify
return value as:
- Schema.calc(sample, true); // Returns copy of sample with assigned values
- Schema.calc(sample); // Returns sample object with assigned values