diff --git a/API.md b/API.md index ed52eb60f..8e66e3df6 100755 --- a/API.md +++ b/API.md @@ -97,11 +97,11 @@ - [`date.max(date)`](#datemaxdate) - [`date.min(date)`](#datemindate) - [`date.timestamp([type])`](#datetimestamptype) - - [`func` - inherits from `Any`](#func---inherits-from-any) - - [`func.arity(n)`](#funcarityn) - - [`func.class()`](#funcclass) - - [`func.maxArity(n)`](#funcmaxarityn) - - [`func.minArity(n)`](#funcminarityn) + - [`function` - inherits from `object`](#function---inherits-from-object) + - [`function.arity(n)`](#funcarityn) + - [`function.class()`](#funcclass) + - [`function.maxArity(n)`](#funcmaxarityn) + - [`function.minArity(n)`](#funcminarityn) - [`link(ref)` - inherits from `Any`](#linkref---inherits-from-any) - [`link.ref(ref)`](#linkrefref) - [`number` - inherits from `Any`](#number---inherits-from-any) @@ -2037,7 +2037,7 @@ const schema = Joi.date().timestamp('unix'); // for unix timestamp (seconds) Possible validation errors: [`date.format`](#dateformat) -### `func` - inherits from `Any` +### `function` - inherits from `object` Generates a schema object that matches a function type. @@ -2046,51 +2046,51 @@ to be cloned. While the function will retain its prototype and closure, it will set to `0`). ```js -const func = Joi.func(); +const func = Joi.function(); await func.validateAsync(function () {}); ``` Possible validation errors: [`object.base`](#objectbase) -#### `func.arity(n)` +#### `function.arity(n)` Specifies the arity of the function where: - `n` - the arity expected. ```js -const schema = Joi.func().arity(2); +const schema = Joi.function().arity(2); ``` Possible validation errors: [`function.arity`](#functionarity) -#### `func.class()` +#### `function.class()` Requires the function to be a class. ```js -const schema = Joi.func().class(); +const schema = Joi.function().class(); ``` Possible validation errors: [`function.class`](#functionclass) -#### `func.maxArity(n)` +#### `function.maxArity(n)` Specifies the maximal arity of the function where: - `n` - the maximum arity expected. ```js -const schema = Joi.func().maxArity(3); +const schema = Joi.function().maxArity(3); ``` Possible validation errors: [`function.maxArity`](#functionmaxarity) -#### `func.minArity(n)` +#### `function.minArity(n)` Specifies the minimal arity of the function where: - `n` - the minimal arity expected. ```js -const schema = Joi.func().minArity(1); +const schema = Joi.function().minArity(1); ``` Possible validation errors: [`function.minArity`](#functionminarity) diff --git a/benchmarks/suite.js b/benchmarks/suite.js index ad6a0a2d9..56096949b 100755 --- a/benchmarks/suite.js +++ b/benchmarks/suite.js @@ -79,7 +79,7 @@ module.exports = (Joi) => [ ).single().sparse().required(), bar: Joi.number().min(12).max(353).default(56).positive(), baz: Joi.date().timestamp('unix'), - qux: [Joi.func().minArity(12).strict(), Joi.binary().max(345)], + qux: [Joi.function().minArity(12).strict(), Joi.binary().max(345)], quxx: Joi.string().ip({ version: ['ipv6'] }), quxxx: [554, 'azerty', true] }) diff --git a/lib/schemas.js b/lib/schemas.js index 21b1f1400..f160d091f 100755 --- a/lib/schemas.js +++ b/lib/schemas.js @@ -54,41 +54,41 @@ internals.rule = Joi.object({ Joi.object({ name: Joi.string().pattern(internals.nameRx).required(), ref: Joi.boolean(), - assert: Joi.alternatives([Joi.func(), Joi.object().schema()]).when('ref', { is: true, then: Joi.required() }), - normalize: Joi.func(), - message: Joi.string().when('assert', { is: Joi.func(), then: Joi.required() }) + assert: Joi.alternatives([Joi.function(), Joi.object().schema()]).when('ref', { is: true, then: Joi.required() }), + normalize: Joi.function(), + message: Joi.string().when('assert', { is: Joi.function(), then: Joi.required() }) }) ), convert: Joi.boolean(), manifest: Joi.boolean(), - method: Joi.func().allow(false), + method: Joi.function().allow(false), multi: Joi.boolean(), - validate: Joi.func() + validate: Joi.function() }); exports.extension = Joi.object({ type: Joi.string().required(), - args: Joi.func(), + args: Joi.function(), base: Joi.object().schema(), coerce: [ - Joi.func().maxArity(3), - Joi.object({ method: Joi.func().maxArity(3).required(), from: Joi.array().items(Joi.string()).single() }) + Joi.function().maxArity(3), + Joi.object({ method: Joi.function().maxArity(3).required(), from: Joi.array().items(Joi.string()).single() }) ], flags: Joi.object().pattern(internals.nameRx, Joi.object({ setter: Joi.string(), default: Joi.any() })), manifest: { - build: Joi.func().arity(2) + build: Joi.function().arity(2) }, messages: [Joi.object(), Joi.string()], - fork: Joi.func().arity(3), - modifiers: Joi.object().pattern(internals.nameRx, Joi.func().minArity(1).maxArity(2)), - overrides: Joi.object().pattern(internals.nameRx, Joi.func()), - prepare: Joi.func().maxArity(3), - rebuild: Joi.func().arity(1), + fork: Joi.function().arity(3), + modifiers: Joi.object().pattern(internals.nameRx, Joi.function().minArity(1).maxArity(2)), + overrides: Joi.object().pattern(internals.nameRx, Joi.function()), + prepare: Joi.function().maxArity(3), + rebuild: Joi.function().arity(1), rules: Joi.object().pattern(internals.nameRx, internals.rule), terms: Joi.object().pattern(internals.nameRx, Joi.object({ init: Joi.array().allow(null).required(), @@ -97,13 +97,13 @@ exports.extension = Joi.object({ Joi.object({ mapped: Joi.string().required() }) ]) })), - validate: Joi.func().maxArity(3) + validate: Joi.function().maxArity(3) }) .and('fork', 'rebuild') .strict(); -exports.extensions = Joi.array().items(Joi.object(), Joi.func().arity(1)).strict(); +exports.extensions = Joi.array().items(Joi.object(), Joi.function().arity(1)).strict(); // Manifest @@ -115,7 +115,7 @@ internals.desc = { }), func: Joi.object({ - function: Joi.func().required(), + function: Joi.function().required(), options: { literal: true } @@ -128,7 +128,7 @@ internals.desc = { separator: Joi.string().length(1).allow(false), ancestor: Joi.number().min(0).integer().allow('root'), map: Joi.array().items(Joi.array().length(2)).min(1), - adjust: Joi.func(), + adjust: Joi.function(), iterables: Joi.boolean() }) .required() @@ -155,7 +155,7 @@ internals.desc = { internals.desc.entity = Joi.alternatives([ Joi.boolean(), - Joi.func(), + Joi.function(), Joi.number(), Joi.string(), internals.desc.buffer, @@ -173,7 +173,7 @@ internals.desc.values = Joi.array() .items( null, Joi.boolean(), - Joi.func(), + Joi.function(), Joi.number().allow(Infinity, -Infinity), Joi.string().allow(''), Joi.symbol(), diff --git a/test/base.js b/test/base.js index 9b405d41c..cf917dd8b 100755 --- a/test/base.js +++ b/test/base.js @@ -970,7 +970,7 @@ describe('any', () => { it('sets literal function default', () => { const func = () => 'just a function'; - const schema = Joi.func().default(func, { literal: true }); + const schema = Joi.function().default(func, { literal: true }); expect(schema.validate(undefined)).to.equal({ value: func }); }); @@ -986,7 +986,7 @@ describe('any', () => { return defaultFn; }; - const schema = Joi.func().default(defaultGeneratorFn); + const schema = Joi.function().default(defaultGeneratorFn); expect(schema.validate(undefined)).to.equal({ value: defaultFn }); }); diff --git a/test/index.js b/test/index.js index 0f3753950..8ddb66db4 100755 --- a/test/index.js +++ b/test/index.js @@ -688,8 +688,8 @@ describe('Joi', () => { const config = { module: Joi.alternatives([ Joi.object({ - compile: Joi.func().required(), - execute: Joi.func() + compile: Joi.function().required(), + execute: Joi.function() }), Joi.string() ]).required() @@ -712,8 +712,8 @@ describe('Joi', () => { const config = { module: Joi.alt().try( Joi.object({ - compile: Joi.func().required(), - execute: Joi.func() + compile: Joi.function().required(), + execute: Joi.function() }).required(), Joi.string().required() ) @@ -727,8 +727,8 @@ describe('Joi', () => { const config = { module: Joi.alt().try([ Joi.object({ - compile: Joi.func().required(), - execute: Joi.func() + compile: Joi.function().required(), + execute: Joi.function() }), Joi.string() ]).required() diff --git a/test/manifest.js b/test/manifest.js index 6fb400fb6..a6ce819e1 100755 --- a/test/manifest.js +++ b/test/manifest.js @@ -270,7 +270,7 @@ describe('Manifest', () => { Joi.binary(), Joi.boolean(), Joi.date(), - Joi.func(), + Joi.function(), Joi.number(), Joi.object(), Joi.string(), @@ -282,7 +282,7 @@ describe('Manifest', () => { internals.test([ Joi.string().required(), - Joi.func().default(() => null, { literal: true }), + Joi.function().default(() => null, { literal: true }), Joi.object().default(), Joi.boolean().optional(), Joi.string().empty(''), diff --git a/test/types/function.js b/test/types/function.js index 5061f11a3..97ea86b8b 100755 --- a/test/types/function.js +++ b/test/types/function.js @@ -18,7 +18,7 @@ describe('function', () => { it('throws an exception if arguments were passed.', () => { - expect(() => Joi.func('invalid argument.')).to.throw('The function type does not allow arguments'); + expect(() => Joi.function('invalid argument.')).to.throw('The function type does not allow arguments'); }); it('validates a function', () => { @@ -37,6 +37,22 @@ describe('function', () => { ]); }); + it('supports func() alias', () => { + + Helper.validate(Joi.func().required(), [ + [function () { }, true], + ['', false, null, { + message: '"value" must be of type function', + details: [{ + message: '"value" must be of type function', + path: [], + type: 'object.base', + context: { label: 'value', value: '', type: 'function' } + }] + }] + ]); + }); + it('validates a function arity', () => { const schema = Joi.function().arity(2).required(); diff --git a/test/types/object.js b/test/types/object.js index dcc640bd8..02bdbd1a8 100755 --- a/test/types/object.js +++ b/test/types/object.js @@ -1143,7 +1143,7 @@ describe('object', () => { const schema = Joi.object({ a: Joi.string(), - b: Joi.func().keys({ c: Joi.string(), d: Joi.number() }), + b: Joi.function().keys({ c: Joi.string(), d: Joi.number() }), d: Joi.number() }).and('a', 'b.c'); @@ -2607,7 +2607,7 @@ describe('object', () => { const schema = Joi.object({ a: Joi.string(), - b: Joi.func().keys({ c: Joi.string(), d: Joi.number() }), + b: Joi.function().keys({ c: Joi.string(), d: Joi.number() }), d: Joi.number() }) .nand('a', 'b.c'); @@ -2903,7 +2903,7 @@ describe('object', () => { const schema = Joi.object({ a: Joi.string(), - b: Joi.func().keys({ c: Joi.string() }), + b: Joi.function().keys({ c: Joi.string() }), d: Joi.number() }).or('a', 'b.c'); @@ -3050,7 +3050,7 @@ describe('object', () => { const schema = Joi.object({ a: Joi.string(), - b: Joi.func().keys({ c: Joi.string(), d: Joi.number() }), + b: Joi.function().keys({ c: Joi.string(), d: Joi.number() }), d: Joi.number() }).oxor('a', 'b.c'); @@ -4014,7 +4014,7 @@ describe('object', () => { const schema = Joi.object({ a: Joi.string(), - b: Joi.func().keys({ c: Joi.string(), d: Joi.number() }), + b: Joi.function().keys({ c: Joi.string(), d: Joi.number() }), d: Joi.number() }).with('a', 'b.c'); @@ -4274,7 +4274,7 @@ describe('object', () => { const schema = Joi.object({ a: Joi.string(), - b: Joi.func().keys({ c: Joi.string(), d: Joi.number() }), + b: Joi.function().keys({ c: Joi.string(), d: Joi.number() }), d: Joi.number() }) .without('a', ['b.c', 'b.d']); @@ -4693,7 +4693,7 @@ describe('object', () => { const schema = Joi.object({ a: Joi.string(), - b: Joi.func().keys({ c: Joi.string(), d: Joi.number() }), + b: Joi.function().keys({ c: Joi.string(), d: Joi.number() }), d: Joi.number() }).xor('a', 'b.c');