Skip to content

Commit

Permalink
Change to function(). For #2012
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Aug 11, 2019
1 parent 84e8509 commit f4b6cd9
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 54 deletions.
30 changes: 15 additions & 15 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
})
Expand Down
40 changes: 20 additions & 20 deletions lib/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
Expand All @@ -115,7 +115,7 @@ internals.desc = {
}),

func: Joi.object({
function: Joi.func().required(),
function: Joi.function().required(),
options: {
literal: true
}
Expand All @@ -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()
Expand All @@ -155,7 +155,7 @@ internals.desc = {

internals.desc.entity = Joi.alternatives([
Joi.boolean(),
Joi.func(),
Joi.function(),
Joi.number(),
Joi.string(),
internals.desc.buffer,
Expand All @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});

Expand All @@ -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 });
});

Expand Down
12 changes: 6 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
)
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions test/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ describe('Manifest', () => {
Joi.binary(),
Joi.boolean(),
Joi.date(),
Joi.func(),
Joi.function(),
Joi.number(),
Joi.object(),
Joi.string(),
Expand All @@ -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(''),
Expand Down
18 changes: 17 additions & 1 deletion test/types/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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();
Expand Down
14 changes: 7 additions & 7 deletions test/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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');

Expand Down

0 comments on commit f4b6cd9

Please sign in to comment.