Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Query Validation Fns Added #100

Merged
merged 3 commits into from
Aug 31, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ArrayType.prototype._base = function() {

ArrayType.prototype.base = function() {

this.add('base', this._base());
this.add('base', this._base(), arguments);
return this;
}

Expand Down
56 changes: 48 additions & 8 deletions lib/types/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
* Module dependencies.
*/
var Utils = require("../utils");
var Log = require("../log");

/**
* Constants
*/
var INTERNAL_DATA_KEY = "_validators";
var INTERNAL_KEY_LIST = "_checks";
var INTERNAL_ARGS_LIST = "_args";

/**
* BaseType Constructor
Expand All @@ -17,6 +19,7 @@ var INTERNAL_KEY_LIST = "_checks";
var BaseType = function() {
this[INTERNAL_DATA_KEY] = [];
this[INTERNAL_KEY_LIST] = [];
this[INTERNAL_ARGS_LIST] = [];

if (typeof this.base !== "undefined" && this.base !== null) {
this.base();
Expand All @@ -34,7 +37,7 @@ BaseType.prototype.toString = function() {
return JSON.stringify(this.valueOf());
}

BaseType.prototype.add = function(key, value) {
BaseType.prototype.add = function(key, value, args) {
if (typeof key == "undefined" || key == null) {
throw "(type).add must given a key";
}
Expand All @@ -44,6 +47,7 @@ BaseType.prototype.add = function(key, value) {

this[INTERNAL_DATA_KEY].push(value);
this[INTERNAL_KEY_LIST].push(key)
this[INTERNAL_ARGS_LIST].push(args);
}

return this[INTERNAL_DATA_KEY];
Expand All @@ -64,7 +68,7 @@ BaseType.prototype._required = function(required) {

BaseType.prototype.required = function(required) {

this.add("required", this._required(required));
this.add("required", this._required(required), arguments);
return this;
}

Expand All @@ -78,7 +82,7 @@ BaseType.prototype._empty = function(){

BaseType.prototype.empty = function(){

this.add("empty", this._empty());
this.add("empty", this._empty(), arguments);
return this;
}

Expand All @@ -105,7 +109,7 @@ BaseType.prototype._valid = function(acceptable) {

BaseType.prototype.valid = function() {

this.add("valid", this._valid(Array.prototype.slice.call(arguments)));
this.add("valid", this._valid(Array.prototype.slice.call(arguments)), arguments);
return this;
}

Expand All @@ -120,7 +124,7 @@ BaseType.prototype._invalid = function(unacceptable) {

BaseType.prototype.invalid = function() {

this.add("invalid", this._invalid(Array.prototype.slice.call(arguments)));
this.add("invalid", this._invalid(Array.prototype.slice.call(arguments)), arguments);
return this;
}

Expand All @@ -144,7 +148,7 @@ BaseType.prototype._with = function(peers) {

BaseType.prototype.with = function() {

this.add("with", this._with(Array.prototype.slice.call(arguments)));
this.add("with", this._with(Array.prototype.slice.call(arguments)), arguments);
return this;
}

Expand All @@ -159,7 +163,7 @@ BaseType.prototype._without = function(peers) {

BaseType.prototype.without = function() {

this.add("without", this._without(Array.prototype.slice.call(arguments)));
this.add("without", this._without(Array.prototype.slice.call(arguments)), arguments);
return this;
}

Expand Down Expand Up @@ -210,7 +214,43 @@ BaseType.prototype._rename = function(to, options) {

BaseType.prototype.rename = function(to, options) {

this.add("rename", this._rename(to, options));
this.add("rename", this._rename(to, options), arguments);
return this;
}

BaseType.prototype.description = function(desc) {

if (typeof desc !== "string") {

var msg = "Validator description must be a string";
Log.err(msg);
throw msg;
}
this.description = desc || "";
return this;
}

BaseType.prototype.notes = function(notes) {

if (typeof notes !== "string" && !(notes instanceof Array)) {

var msg = "Validator notes must be a string or array";
Log.err(msg);
throw msg;
}
this.notes = notes || "";
return this;
}

BaseType.prototype.tags = function(tags) {

if (!(tags instanceof Array)) {

var msg = "Validator tags must be an array";
Log.err(msg);
throw msg;
}
this.tags = tags || [];
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/types/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ BooleanType.prototype._base = function() {

BooleanType.prototype.base = function() {

this.add('base', this._base());
this.add('base', this._base(), arguments);
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/types/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ FunctionType.prototype._base = function() {

FunctionType.prototype.base = function() {

this.add('base', this._base());
this.add('base', this._base(), arguments);
return this;
}

Expand Down
10 changes: 5 additions & 5 deletions lib/types/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ NumberType.prototype._base = function() {

NumberType.prototype.base = function() {

this.add('base', this._base());
this.add('base', this._base(), arguments);
return this;
}

Expand All @@ -50,7 +50,7 @@ NumberType.prototype._min = function(n) {

NumberType.prototype.min = function(n) {

this.add('min', this._min(n));
this.add('min', this._min(n), arguments);
return this;
}

Expand All @@ -63,7 +63,7 @@ NumberType.prototype._max = function(n) {

NumberType.prototype.max = function(n) {

this.add('max', this._max(n));
this.add('max', this._max(n), arguments);
return this;
}

Expand All @@ -76,7 +76,7 @@ NumberType.prototype._integer = function() {

NumberType.prototype.integer = function() {

this.add('integer', this._integer());
this.add('integer', this._integer(), arguments);
return this;
}

Expand All @@ -91,7 +91,7 @@ NumberType.prototype._float = function() {

NumberType.prototype.float = function() {

this.add('float', this._float());
this.add('float', this._float(), arguments);
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ObjectType.prototype._base = function() {

ObjectType.prototype.base = function() {

this.add('base', this._base());
this.add('base', this._base(), arguments);
return this;
}

Expand Down
10 changes: 5 additions & 5 deletions lib/types/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ StringType.prototype._base = function() {

StringType.prototype.base = function() {

this.add("base", this._base());
this.add("base", this._base(), arguments);
return this;
}

Expand All @@ -35,7 +35,7 @@ StringType.prototype._min = function(n) {

StringType.prototype.min = function(n) {

this.add("min", this._min(n));
this.add("min", this._min(n), arguments);
return this;
}

Expand All @@ -49,7 +49,7 @@ StringType.prototype._max = function(n) {

StringType.prototype.max = function(n) {

this.add("max", this._max(n));
this.add("max", this._max(n), arguments);
return this;
}

Expand All @@ -63,7 +63,7 @@ StringType.prototype._regex = function(n) {

StringType.prototype.regex = function(pattern) {

this.add('regex', this._regex(pattern));
this.add('regex', this._regex(pattern), arguments);
return this;
}

Expand All @@ -80,7 +80,7 @@ StringType.prototype._date = function(){

StringType.prototype.date = function(){

this.add('date', this._date.apply(arguments));
this.add('date', this._date.apply(arguments), arguments);
return this;
}

Expand Down
36 changes: 36 additions & 0 deletions test/integration/validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var request = require("request");
var should = require("should");
var Hapi = require('../../lib/hapi');
var Types = Hapi.Types;
var S = Types.String


var get = function (request) {
request.reply('Success!\n');
};

describe("Integration", function(){
describe("#Validation.query", function(){
Hapi.Process.initialize();
var config = {};
var http = new Hapi.Server.Server('0.0.0.0', 18080, config);

before(function(done){
http.setRoutesDefaults({ authentication: 'none' });
http.addRoutes([{ method: 'GET', path: '/test2', config: { handler: get, query: {p1: S().required().rename('itemId')}}}]);

http.start();
Hapi.Process.finalize();
done();
})

after(function(done){
http.stop && http.stop();
done();
})

it('should handle two #rename requests in a row', function(done){
done()
})
})
})
86 changes: 86 additions & 0 deletions test/unit/types/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,90 @@ describe("BaseType", function(){
done();
})
})

describe("#description", function(){
it('should set description', function(done){
var value = "walmart";
var base = new BaseType();
var result;
(function(){
result = base.description(value);
}).should.not.throw();
should.exist(result);
result.description.should.equal(value);
done();
})

it('should return error if description is not a string', function(done){
var value = 1;
var base = new BaseType();
var result;
(function(){
result = base.description(value);
}).should.throw();
// should.exist(result);
// result.description.should.equal(value);
done();
})
})

describe("#notes", function(){
it('should set notes if given as string', function(done){
var value = "walmart";
var base = new BaseType();
var result;
(function(){
result = base.notes(value);
}).should.not.throw();
should.exist(result);
result.notes.should.equal(value);
done();
})

it('should set notes if given as array', function(done){
var value = ["walmart", "@walmartlabs"];
var base = new BaseType();
var result;
(function(){
result = base.notes(value);
}).should.not.throw();
should.exist(result);
result.notes.should.equal(value);
done();
})

it('should return error if not given as string or array', function(done){
var value = 1;
var base = new BaseType();
var result;
(function(){
result = base.notes(value);
}).should.throw();
done();
})
})

describe("#tags", function(){
it('should set tags if given as array', function(done){
var value = ["walmart", "@walmartlabs"];
var base = new BaseType();
var result;
(function(){
result = base.tags(value);
}).should.not.throw();
should.exist(result);
result.tags.should.equal(value);
done();
})

it('should return error if not given as array', function(done){
var value = 1;
var base = new BaseType();
var result;
(function(){
result = base.notes(value);
}).should.throw();
done();
})
})
})