Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from wpreul/feature/functions
Adding basic function support
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); | ||
|