Skip to content

Commit

Permalink
Merge branch 'master' of github.com:spumko/hapi
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Aug 24, 2013
2 parents d30ca4c + ba172b1 commit 86d9c46
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {
"hoek": "0.9.x",
"boom": "0.4.x",
"joi": "0.4.x",
"joi": "1.1.x",
"catbox": "1.0.x",
"hawk": "1.x.x",
"shot": "0.4.x",
Expand Down
28 changes: 28 additions & 0 deletions test/unit/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var Lab = require('lab');
var Querystring = require('querystring');
var Path = require('path');
var Hapi = require('../..');
var Validation = require('../../lib/validation');

Expand Down Expand Up @@ -286,5 +287,32 @@ describe('Validation', function () {
done();
});
});

it('can raise custom validation errors', function (done) {

var route = {
method: 'GET',
path: '/',
config: {
handler: testHandler,
validate: {
payload: {
username: S().required(),
languagePath: Path.join(__dirname, 'validation', 'messages.json')
}
}
}
};

var payload = { username: '' };
var request = createRequestObject(null, route, payload);

Validation.payload(request, function (err) {

expect(err).to.exist;
expect(err.message).to.contain('Invalid value for `username`: `empty`.');
done();
});
});
});
});
18 changes: 18 additions & 0 deletions test/unit/validation/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"base": {
"unknown": "Unrecognized `{{key}}` field.",
"without": "The `{{key}}` field must be omitted if `{{value}}` is specified.",
"invalid": "Invalid value for `{{key}}`: `{{value}}`.",
"rename": {
"allowOverwrite": "`{{value}}` is already assigned to the `{{key}}` field."
}
},
"string": {
"base": "The `{{key}}` field must be a string.",
"email": "The `{{key}}` field must be a valid e-mail address.",
"date": "The `{{key}}` field must be a valid date.",
"min": "The `{{key}}` field must be at least {{value}} characters long.",
"max": "The `{{key}}` field may not exceed {{value}} characters.",
"regex": "The `{{key}}` field failed one or more validation constraints."
}
}

0 comments on commit 86d9c46

Please sign in to comment.