osr
Version:
osr
48 lines (44 loc) • 1.1 kB
JavaScript
var Schema = function(schema){
this.schema = schema;
this.index = [];
this.required = [];
this.unique = [];
for(var key in schema){
if(schema[key].index){
this.index.push(key);
}
if(schema[key].required){
this.required.push(key);
}
if(schema[key].unique){
this.unique.push(key);
}
}
}
Schema.prototype.check = function(obj){
this.required.forEach(function(item,index){
if(!obj[item]){
return item;
}
})
return;
}
Schema.prototype.getMainKey = function(name,obj){
var key = obj[this.unique[0]];
if(!key){
key = "*";
}
return name+"::"+this.unique[0]+"::"+key;
}
Schema.prototype.getMainValue = function(name,obj){
var value = {};
for(var key in this.schema){
if(typeof(obj[key]) == "object"){
value[key] = JSON.stringify(obj[key]);
}else{
value[key] = obj[key];
}
}
return value;
}
module.exports = exports = Schema;