Skip to content

Commit

Permalink
Merge pull request #730 from hapijs/funcObj
Browse files Browse the repository at this point in the history
Fix function properties validation. Closes #729
  • Loading branch information
Marsup committed Oct 2, 2015
2 parents eefc2cc + cb99f6b commit ae4aafa
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
4 changes: 3 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,9 @@ var schema = Joi.date().iso();

Generates a schema object that matches a function type.

Supports the same methods of the [`object()`](#object) type.
Supports the same methods of the [`object()`](#object) type. Note that validating a function keys will cause the function
to be cloned. While the function will retain its prototype and closure, it will lose its `length` property value (will be
set to `0`).

```javascript
var func = Joi.func();
Expand Down
13 changes: 12 additions & 1 deletion lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,18 @@ internals.Object.prototype._base = function (value, state, options) {
// Ensure target is a local copy (parsed) or shallow copy

if (target === value) {
target = Object.create(Object.getPrototypeOf(value));
if (type === 'object') {
target = Object.create(Object.getPrototypeOf(value));
}
else {
target = function () {

return value.apply(this, arguments);
};

target.prototype = Hoek.clone(value.prototype);
}

var valueKeys = Object.keys(value);
for (var t = 0, tl = valueKeys.length; t < tl; ++t) {
target[valueKeys[t]] = value[valueKeys[t]];
Expand Down
68 changes: 68 additions & 0 deletions test/function.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Load modules

var Code = require('code');
var Lab = require('lab');
var Joi = require('../lib');
var Helper = require('./helper');
Expand All @@ -15,6 +16,7 @@ var internals = {};
var lab = exports.lab = Lab.script();
var describe = lab.describe;
var it = lab.it;
var expect = Code.expect;


describe('func', function () {
Expand Down Expand Up @@ -42,4 +44,70 @@ describe('func', function () {
['', false]
], done);
});

it('keeps validated value as a function', function (done) {

var schema = Joi.func().keys({ a: Joi.number() });

var b = 'abc';
var value = function () {

return b;
};

value.a = '123';

schema.validate(value, function (err, validated) {

expect(validated).to.be.a.function();
expect(validated()).to.equal('abc');
expect(validated).to.not.equal(value);
done();
});
});

it('retains validated value prototype', function (done) {

var schema = Joi.func().keys({ a: Joi.number() });

var value = function () {

this.x = 'o';
};

value.prototype.get = function () {

return this.x;
};

schema.validate(value, function (err, validated) {

expect(validated).to.be.a.function();
var p = new validated();
expect(p.get()).to.equal('o');
expect(validated).to.not.equal(value);
done();
});
});

it('keeps validated value as a function (no clone)', function (done) {

var schema = Joi.func();

var b = 'abc';
var value = function () {

return b;
};

value.a = '123';

schema.validate(value, function (err, validated) {

expect(validated).to.be.a.function();
expect(validated()).to.equal('abc');
expect(validated).to.equal(value);
done();
});
});
});

0 comments on commit ae4aafa

Please sign in to comment.