Skip to content

Commit

Permalink
Remove ext desc. For #1987
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 25, 2019
1 parent 5386138 commit 917fdf7
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 122 deletions.
2 changes: 0 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -3073,13 +3073,11 @@ Extension objects use the following parameters :
- `coerce` - an optional function that runs before the base, usually serves when you want to coerce values of a different type than your base. It takes 3 arguments `value`, `state` and `prefs`.
- `pre` - an optional function that runs first in the validation chain, usually serves when you need to cast values. It takes 3 arguments `value`, `state` and `prefs`.
- `messages` - an optional object to add error definitions. Every key will be prefixed by the type name.
- `describe` - an optional function taking the fully formed description to post-process it.
- `rules` - an optional array of rules to add.
- `name` - name of the new rule. **Required**.
- `params` - an optional object containing **joi** schemas of each parameter ordered. You can also pass a single **joi** schema as long as it is a `Joi.object()`, of course some methods such as `pattern` or `rename` won't be useful or won't work at all in this given context.
- `setup` - an optional function that takes an object with the provided parameters to allow for internals manipulation of the schema when a rule is set, you can optionally return a new **joi** schema that will be taken as the new schema instance. At least one of `setup` or `validate` **must** be provided.
- `validate` - an optional function to validate values that takes 4 parameters `params`, `value`, `state` and `prefs`. At least one of `setup` or `validate` **must** be provided.
- `description` - an optional string or function taking the parameters as argument to describe what the rule is doing.

Factory functions are advised if you intend to publish your extensions for others to use, because they are capable of using an extended **joi** being built, thus avoiding any erasure when using multiple extensions at the same time. See an example of a factory function in the section below.

Expand Down
8 changes: 0 additions & 8 deletions lib/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,6 @@ internals.generator = function (joi, root, extension) {
}
}

if (extension.describe) {
type.prototype.describe = function () {

const description = ctor.prototype.describe.call(this);
return extension.describe.call(this, description);
};
}

return new type();
};
};
Expand Down
20 changes: 0 additions & 20 deletions lib/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,6 @@ exports.describe = function (schema) {
}
}

const options = test.options;
if (options) {
if (typeof options.description === 'string') {
item.description = options.description;
}
else if (typeof options.description === 'function') {
item.description = options.description(item.args);
}
}

desc.rules.push(item);
}

Expand Down Expand Up @@ -309,16 +299,6 @@ internals.Builder = class {
if (Object.keys(options).length) {
schema = schema.rule(options);
}

//const options = test.options;
//if (options) {
// if (typeof options.description === 'string') {
// item.description = options.description;
// }
// else if (typeof options.description === 'function') {
// item.description = options.description(item.args);
// }
//}
}
}

Expand Down
4 changes: 1 addition & 3 deletions lib/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ exports.extension = Joi.object({
coerce: Joi.func().minArity(1).maxArity(3),
pre: Joi.func().minArity(1).maxArity(3),
messages: Joi.object(),
describe: Joi.func().arity(1),
rules: Joi.array().items(Joi.object({
name: Joi.string().required(),
setup: Joi.func().arity(1),
Expand All @@ -62,8 +61,7 @@ exports.extension = Joi.object({
is: Joi.object().schema(),
then: Joi.object().schema('object'),
otherwise: Joi.object().pattern(/.*/, Joi.object().schema())
}),
description: [Joi.string(), Joi.func().arity(1)]
})
})
.or('setup', 'validate'))
})
Expand Down
89 changes: 0 additions & 89 deletions test/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1124,95 +1124,6 @@ describe('extension', () => {
]
});
});

it('should describe a schema with rules and parameters with custom description', () => {

const customJoi = Joi.extend({
name: 'myType',
rules: [
{
name: 'foo',
params: {
bar: Joi.string()
},
description: 'something',
validate(params, value, state, prefs) { }
},
{
name: 'bar',
params: {
baz: Joi.string()
},
description(params) {

expect(params).to.equal({ baz: 'baz' });
return 'whatever';
},
validate(params, value, state, prefs) { }
}
]
});

const schema = customJoi.myType().foo('bar').bar('baz');
expect(schema.describe()).to.equal({
type: 'myType',
rules: [
{ name: 'foo', description: 'something', args: { bar: 'bar' } },
{ name: 'bar', description: 'whatever', args: { baz: 'baz' } }
]
});
});

it('should describe a schema with rules and parameters with custom description', () => {

const customJoi = Joi.extend({
name: 'myType',
describe(description) {

expect(description).to.equal({
type: 'myType',
rules: [
{ name: 'foo', description: 'something', args: { bar: 'bar' } },
{ name: 'bar', description: 'whatever', args: { baz: 'baz' } }
]
});

description.type = 'zalgo';
return description;
},
rules: [
{
name: 'foo',
params: {
bar: Joi.string()
},
description: 'something',
validate(params, value, state, prefs) { }
},
{
name: 'bar',
params: {
baz: Joi.string()
},
description(params) {

expect(params).to.equal({ baz: 'baz' });
return 'whatever';
},
validate(params, value, state, prefs) { }
}
]
});

const schema = customJoi.myType().foo('bar').bar('baz');
expect(schema.describe()).to.equal({
type: 'zalgo',
rules: [
{ name: 'foo', description: 'something', args: { bar: 'bar' } },
{ name: 'bar', description: 'whatever', args: { baz: 'baz' } }
]
});
});
});

it('should return a custom Joi with types not inheriting root properties', () => {
Expand Down

0 comments on commit 917fdf7

Please sign in to comment.