diff --git a/lib/index.js b/lib/index.js index f3ec1c1fa..6e9db172d 100755 --- a/lib/index.js +++ b/lib/index.js @@ -24,18 +24,46 @@ const internals = { number: require('./types/number'), object: require('./types/object'), string: require('./types/string'), - symbol: require('./types/symbol') + symbol: require('./types/symbol'), + + binds: [ + 'any', + 'alt', + 'alternatives', + 'array', + 'bool', + 'boolean', + 'binary', + 'date', + 'func', + 'lazy', + 'number', + 'object', + 'string', + 'symbol', + + 'assert', + 'attempt', + 'bind', + 'compile', + 'defaults', + 'extend', + 'validate' + ] }; +internals.anyMethods = Object.keys(Any.prototype) + .filter((key) => key[0] !== '_' && key !== 'isImmutable') + .concat(internals.binds); + + internals.root = function () { const any = new Any(); - const root = any.clone(); - Any.prototype._root = root; root._root = root; - root._binds = new Set(['any', 'alternatives', 'alt', 'array', 'bool', 'boolean', 'binary', 'date', 'func', 'number', 'object', 'string', 'symbol', 'validate', 'describe', 'compile', 'assert', 'attempt', 'lazy', 'defaults', 'extend', 'allow', 'valid', 'only', 'equal', 'invalid', 'disallow', 'not', 'required', 'exist', 'optional', 'forbidden', 'strip', 'when', 'empty', 'default', 'failover']); + root._binds = new Set(internals.anyMethods); root.any = function (...args) { @@ -86,11 +114,9 @@ internals.root = function () { root.bind = function () { const joi = Object.create(this); - - joi._binds.forEach((bind) => { - + for (const bind of joi._binds) { joi[bind] = joi[bind].bind(joi); - }); + } return joi; };