Skip to content

Instantly share code, notes, and snippets.

@taburetkin
Created September 11, 2018 10:34
Show Gist options
  • Save taburetkin/2368fcf8190fabe26c8237cb35716102 to your computer and use it in GitHub Desktop.
Save taburetkin/2368fcf8190fabe26c8237cb35716102 to your computer and use it in GitHub Desktop.
model-schema
import { betterResult } from 'bbmn/utils';
//import { flat, unflat } from '../../utils/index.js';
// function deepClone(arg = {}){
// return unflat(flat(arg || {}));
// }
export default function ModelSchema(properties = {}){
this.properties = _.reduce(properties, (memo, property, name) => {
memo[name] = this._createProperty(name, property);
return memo;
}, {});
}
_.extend(ModelSchema.prototype, {
_createProperty(name, property = {}){
return new PropertySchema({ name, property, modelSchema: this });
},
getProperty(name, { create = false } = {}){
let property = this.properties[name];
if(property || !create) {
return property;
}
property = this.properties[name] = this._createProperty(name);
return property;
},
getValidation(name) {
let property = this.getProperty(name);
return property && property.getValidation() || {};
},
getType(name) {
let property = this.getProperty(name);
return property && property.getType() || {};
},
getLabel(name){
let property = this.getProperty(name);
return property && property.getLabel() || '';
}
});
export function PropertySchema({ name, property, modelSchema }){
_.extend(this, property);
this.name = name;
this.modelSchema = modelSchema;
}
_.extend(PropertySchema.prototype, {
getValidation() {
return this.validation || {};
},
getType() {
return this.value || {};
},
getDisplay(){
return this.display || {};
},
getLabel(value, hash){
let label = this.getDisplay().label;
return betterResult({ label },'label', { args: [value, hash] });
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment