script
const { Shapeshifter } = require('json-shapeshifter');
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' },
address: {
type: 'object',
properties: {
city: { type: 'string' },
street: { type: 'string' },
},
},
},
};
const shapeshifter = new Shapeshifter();
shapeshifter.loadJSON({ name: 'John', age: 30, address: { city: 'Beijing', street: 'Main St.' } });
shapeshifter.loadSchema(schema);
shapeshifter.rule({
ruleName: 'MyRule',
apply: (data, schema) => {
data.fullAddress = `${data.address.city}, ${data.address.street}`;
delete data.address;
return data;
},
});
shapeshifter.applyRule('MyRule');
console.log(shapeshifter.toJSON());