Skip to content

Commit

Permalink
Merge pull request #1 from wpreul/feature/functions
Browse files Browse the repository at this point in the history
Adding basic function support
  • Loading branch information
geek committed Feb 7, 2013
2 parents 2805865 + e14ff21 commit a3469fa
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/types/function.js
@@ -0,0 +1,44 @@
// Load modules

var NodeUtil = require('util');
var BaseType = require('./base');
var Utils = require('../utils');


// Declare internals

var internals = {};


module.exports = internals.createType = function () {

return new internals.FunctionType();
};


module.exports.FunctionType = internals.FunctionType = function () {

internals.FunctionType.super_.call(this);
Utils.mixin(this, BaseType);
return this;
};

NodeUtil.inherits(internals.FunctionType, BaseType);


internals.FunctionType.prototype.__name = "Function";


internals.FunctionType.prototype._base = function() {

return function(value) {

return (value === null || typeof value === "function");
};
};

internals.FunctionType.prototype.base = function () {

this.add('base', this._base(), arguments);
return this;
};
43 changes: 43 additions & 0 deletions test/types/function.js
@@ -0,0 +1,43 @@
// Load modules

var Chai = require('chai');
var Joi = process.env.TEST_COV ? require('../../lib-cov') : require('../../lib');
var FunctionType = process.env.TEST_COV ? require('../../lib-cov/types/function') : require('../../lib/types/function');
var Support = require('../support/meta');


// Declare internals

var internals = {};


// Test shortcuts

var expect = Chai.expect;
var verifyBehavior = Support.verifyValidatorBehavior;


describe('Types', function () {

describe('Function', function () {

var F = FunctionType; // Joi.types.Function;

it('should have mixins', function (done) {

var result = F();
expect(result.validate).to.exist;
done();
});

it('should validate a function', function (done) {

var t = F().required();
verifyBehavior(t, [
[function(){ }, true],
['', false]
], done);
});
});
});

0 comments on commit a3469fa

Please sign in to comment.