astrohelm / metaforge

Library 📝 that allow you to describe data structures by subset of JavaScript syntax and validate them at runtime
https://astrohelm.ru
MIT License
3 stars 1 forks source link

Module for fields calculations #21

Closed sashapop10 closed 10 months ago

sashapop10 commented 10 months ago

Describe the problem

Calculated schemas another words - Functional fields

const schema = new Schema({
  name: 'string',
  phrase: (parent, root) => 'Hello ' + parent.name + ' !',
})
const mode = true; // true - copy, false - assign; 
const sample = { name: 'Alexander' };
schema.calc(sample,true); // { result: { name: 'Alexander', phrase: 'Hello Alexander !' } };
sample; // { name: 'Alexander' }
schema.calc(sample); 
// If sample not an object in this mode - throw error
sample; // { name: 'Alexander', phrase: 'Hello Alexander !' }