WIP
npm install model-prefect
let ford = {
fullName: "Ford Prefect",
nickname: "Ix",
species: "Betelgeusian"
};
const opts = {
"map": {
"fullName": "name"
},
"exclude" : ["nickname"]
};
let fordPresenter = new ModelPrefect(ford, opts);
// {"name": "Ford Prefect", "species": "Betelgeusian"}
Usage with Betelgeuse
import Betelgeuse, { Types } from 'betelgeuse';
import ModelPrefect from 'model-prefect';
ModelPrefect.goesToBetelgeuse(Betelgeuse); // Add `toPresenter` function to Betelgeuse instances
class Ford extends Betelgeuse {
static schema = {
fullName: {
type: Types.string,
minLength: 3
},
nickname: {
type: Types.string,
minLength: 3
},
species: Types.string
}
static presenter = {
"map": {
"fullName": "name"
}
"exclude": ["nickname"]
}
}
let ford = new Ford({
fullName: "Ford Prefect",
nickname: "Ix",
species: "Betelgeusian"
});
ford.toPresenter();
// {"name": "Ford Prefect", "species": "Betelgeusian"}
Or
class FordPresenter extends ModelPrefect {
static presenter = {
"map": {
"fullName": "name"
}
"exclude": ["nickname"]
}
}
let fordPresenter = new FordPresenter(ford);
// {"name": "Ford Prefect", "species": "Betelgeusian"}
Schema
class FordPresenter extends ModelPrefect {
static options = {
"betelgeuse" : true, // convert validation schema
"presenter": {
"fullName": "name"
}
"exclude": ["nickname"]
}
}
let fordPresenter = new FordPresenter(ford);
/*
{
"name": "Ford Prefect",
"species": "Betelgeusian",
"_schema": {
"name": {
"type": "string",
"minLength": 3
},
"species": {
"type": "string"
}
}
}
*/
fordPresenter.isValid
// Function.isValid
fordPresenter.fields()
// {"name": "Ford Prefect", "species": "Betelgeusian"}