Skip to content

Commit

Permalink
model validation implemented, need more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
luiseduardobrito committed Sep 24, 2013
1 parent 9d7f661 commit 7816037
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
17 changes: 17 additions & 0 deletions api/adapters/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ var Model = function(type) {
throw new Error("Could not parse required field '" + k + "'. " + e.message.toString());
}
}

else {
try {
type.get(model[k].type).check(obj[k]);
Expand All @@ -314,6 +315,22 @@ var Model = function(type) {

obj[k] = encrypt(obj[k], model[k].encryption)
}

if(model[k].validate && obj[k]
&& typeof model[k].validate === typeof function(){}) {

try {

var res = model[k].validate(obj[k]);

if(!res)
throw new Error("Validation error");
}

catch(e) {
throw new Error("Could not parse required field '" + k + "'. " + e.message.toString());
}
}
}

return obj;
Expand Down
9 changes: 7 additions & 2 deletions api/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = {
name: req.param("name"),
email: req.param("email"),
password: req.param("password"),
credit: req.param("credit"),
access_token: req.param("access_token") || ""
});

Expand Down Expand Up @@ -85,7 +86,8 @@ module.exports = {

response(res).json({
result: "error",
message: e.message.toString()
message: e.message.toString(),
code: 500
})

return;
Expand All @@ -95,6 +97,7 @@ module.exports = {
login: function(req, res) {

try {

model.find("user", {

email: req.param("email"),
Expand Down Expand Up @@ -128,11 +131,13 @@ module.exports = {
return;
});
}

catch(e) {

response(res).json({
result: "error",
message: e.message.toString()
message: e.message.toString(),
code: 500
});
}
},
Expand Down
7 changes: 7 additions & 0 deletions api/models/response_model.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//////////////////////////////////////////
// //
// Response Model - Required model //
// Default JSON schema for responses //
// //
//////////////////////////////////////////

module.exports = {

result: {
Expand Down
18 changes: 17 additions & 1 deletion api/models/user_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,23 @@ module.exports = {
type: "password",

// default: sha256
encprytion: "sha256"
encryption: "sha256"
},

credit: {

// sample credit card
required: false,
type: "string",

validate: function(number) {

// sample validation
if(number.length < 8)
throw new Error("The credit card should be at least 8 characters long");

return true;
}
},

access_token: {
Expand Down
3 changes: 1 addition & 2 deletions api/policies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ module.exports = function(req, res) {
throw new Error("Problem validating access policies");
}

else {
else
cb(_req, _res);
}

});

Expand Down
31 changes: 30 additions & 1 deletion tests/adapters/model_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,40 @@ exports.test_simpleUserModel = function(test) {

test.ok(user.name == "name", "User name should be valid");
test.ok(user.email == "[email protected]", "User email should be valid");
test.ok(user.password == "abcd1234", "User password should be valid");
test.ok(user.password, "User password should be valid");
test.ok(user.access_token == "01234567890", "User access_token should be valid");

test.ok(user._id, "Model _id hash should be valid");
test.ok(user.toJSON, "Model 'toJSON' should be valid");

test.done();
}

exports.test_simpleCreditCardValidation = function(test) {

test.expect(2);

var user = false;

try {

user = model.create("user", {

name: "name",
email: "[email protected]",
password: "abcd1234",
credit: "12345",
access_token: "01234567890"

});
}
catch(e) {

test.ok(e.message, "An error message is expected.");
test.ok(!user, "User should not be valid.");
test.done();
return;
}

test.fail();
}

0 comments on commit 7816037

Please sign in to comment.