Created
February 10, 2015 21:38
-
-
Save dalssoft/e661cc693ccfef61d266 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Model Lib | |
- Validar design da Lib | |
- Definição de campos | |
- Definição das validações dos campos | |
- Definição de fill do objeto | |
- Objetos complexos (obj.prop1.prop2) | |
- Integração com o repositório | |
- Permitir Herença do Model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var util = require('util'); | |
var inspector = require('schema-inspector'); | |
var merge = require('merge') | |
var _ = require('underscore') | |
// ############# LIB ############# | |
var validate = function(obj) { | |
var schema = listSchemas(obj); | |
return inspector.validate({properties: schema}, obj); | |
} | |
var sanitize = function(obj, properties) { | |
var schema = listSchemas(obj); | |
inspector.sanitize({strict: true, properties: schema}, properties); | |
} | |
var listSchemas = function(obj) { | |
var schemas = []; | |
var schema; | |
if (obj.__schema) { | |
schema = obj.__schema | |
schemas.push(schema); | |
} | |
if (obj.constructor.super_ && obj.constructor.super_.prototype.__schema) { | |
schemas = schemas.concat(listSchemas(obj.constructor.super_.prototype)); | |
} | |
var ret = {}; | |
for (var _i = 0, _len = schemas.length; _i < _len; _i++) { | |
var schema = schemas[_i]; | |
ret = merge.recursive(false, ret, schema); | |
} | |
return ret; | |
} | |
var Model = (function() { | |
function Model() {} | |
Model.prototype.isValid = function() { | |
return validate(this); | |
} | |
Model.prototype.assignProperties = function(properties) { | |
properties = merge.clone(properties); | |
sanitize(this, properties); | |
properties = merge.recursive(false, this, properties); | |
_.extend(this, properties); | |
} | |
return Model; | |
})(); | |
// ############# BUSINESS ############# | |
var Mencao = (function() { | |
function Mencao() {} | |
util.inherits(Mencao, Model); | |
Mencao.prototype.__schema = { | |
descricao: { type: 'string', minLength: 1 } | |
} | |
return Mencao; | |
})(); | |
var MencaoTwitter = (function() { | |
function MencaoTwitter() {} | |
util.inherits(MencaoTwitter, Mencao); | |
MencaoTwitter.prototype.__schema = { | |
tipo: { type: 'string', optional: false, eq: "twitter" } | |
} | |
return MencaoTwitter; | |
})(); | |
var MencaoTwitterRetweet = (function() { | |
function MencaoTwitterRetweet() {} | |
util.inherits(MencaoTwitterRetweet, MencaoTwitter); | |
MencaoTwitterRetweet.prototype.__schema = { | |
retweet: { type: 'boolean', optional: false, eq: true } | |
} | |
return MencaoTwitterRetweet; | |
})(); | |
// ############# TEST ############# | |
var test = function(Type) { | |
var m, method, name, _ref; | |
console.log("---- Instance ---- "); | |
for (name in Type) { | |
method = Type[name]; | |
console.log(name); | |
} | |
console.log("---- Instance Proto ---- "); | |
_ref = Type.prototype; | |
for (name in _ref) { | |
method = _ref[name]; | |
console.log(name); | |
} | |
console.log("---- Instance: Usando ---- "); | |
m = new Type(); | |
console.log("__schema(Type)", m.__schema); | |
return m; | |
}; | |
console.log("############# TEST - MENCAO #############"); | |
data = { | |
descricao: "Mencao1", | |
tipo: "twitter", | |
retweet: true, | |
date: "alo alo" | |
} | |
m = test(Mencao); | |
m.descricao = ''; | |
console.log("validate", m.isValid()); | |
m.assignProperties(data); | |
console.log("assignProperties", m); | |
console.log("validate", m.isValid()); | |
console.log("############# TEST - MENCAO TWITTER #############"); | |
m = test(MencaoTwitter); | |
m.tipo = ''; | |
console.log("validate", m.isValid()); | |
m.assignProperties(data); | |
console.log("assignProperties", m); | |
console.log("validate", m.isValid()); | |
console.log("############# TEST - MENCAO TWITTER RETWEET #############"); | |
m = test(MencaoTwitterRetweet); | |
m.retweet = false; | |
console.log("validate", m.isValid()); | |
m.assignProperties(data); | |
console.log("assignProperties", m); | |
console.log("validate", m.isValid()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment