Skip to content

Commit

Permalink
Change arg to args. For #1867
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 4, 2019
1 parent 62a0417 commit fe8e21d
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 125 deletions.
20 changes: 10 additions & 10 deletions lib/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ exports.root = function (root, extensions) {

internals.generator = function (joi, root, extension) {

return function (args) {
return function (ctorArgs) {

let base = extension.base;
if (typeof base === 'function') {
base = base(...args);
base = base(...ctorArgs);
}

if (!base) {
Expand Down Expand Up @@ -143,17 +143,17 @@ internals.generator = function (joi, root, extension) {
}

let hasRef = false;
let arg = {};
let args = {};

for (let i = 0; i < ruleArgs.length; ++i) {
arg[ruleArgs[i]] = rArgs[i];
args[ruleArgs[i]] = rArgs[i];
if (!hasRef && Ref.isRef(rArgs[i])) {
hasRef = true;
}
}

if (validateArgs) {
arg = joi.attempt(arg, validateArgs);
args = joi.attempt(args, validateArgs);
}

let schema;
Expand All @@ -162,17 +162,17 @@ internals.generator = function (joi, root, extension) {

const validate = function (value, state, prefs) {

return rule.validate.call(this, arg, value, state, prefs);
return rule.validate.call(this, args, value, state, prefs);
};

schema = this._test(rule.name, arg, validate, { description: rule.description, hasRef });
schema = this._test(rule.name, args, validate, { description: rule.description, hasRef });
}
else {
schema = this.clone();
}

if (rule.setup) {
const newSchema = rule.setup.call(schema, arg);
const newSchema = rule.setup.call(schema, args);
if (newSchema !== undefined) {
Hoek.assert(Common.isSchema(newSchema), `Setup of extension Joi.${this._type}().${rule.name}() must return undefined or a Joi object`);
schema = newSchema;
Expand All @@ -181,10 +181,10 @@ internals.generator = function (joi, root, extension) {
if (rule.validate) {
const validate = function (value, state, prefs) {

return rule.validate.call(this, arg, value, state, prefs);
return rule.validate.call(this, args, value, state, prefs);
};

schema = schema._test(rule.name, arg, validate, { description: rule.description, hasRef });
schema = schema._test(rule.name, args, validate, { description: rule.description, hasRef });
}
}

Expand Down
41 changes: 17 additions & 24 deletions lib/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,47 +133,40 @@ exports.describe = function (schema) {

const item = { name: test.name };

let arg = test.arg;
if (arg !== undefined) {
if (test.rule) {
const keys = Object.keys(arg);
if (keys.length === 1) {
arg = arg[keys[0]];
}
else {
const args = {};
for (const key of keys) {
const inner = arg[key];
if (inner !== undefined) {
args[key] = Common.isResolvable(inner) ? inner.describe() : inner;
}
}

arg = args;
if (test.args) {
item.args = {};
for (const key in test.args) {
const arg = test.args[key];
if (key === 'options' &&
!Object.keys(arg).length) {

continue;
}

item.args[key] = Common.isResolvable(arg) || Common.isSchema(arg) ? arg.describe() : arg;
}

if (arg !== undefined) {
item.arg = Common.isResolvable(arg) || Common.isSchema(arg) ? arg.describe() : arg;
if (!Object.keys(item.args).length) {
delete item.args;
}
}

const options = test.options;
if (options) {
if (options.hasRef) {
item.arg = {};
const keys = Object.keys(test.arg);
item.args = {};
const keys = Object.keys(test.args);
for (const key of keys) {
const value = test.arg[key];
item.arg[key] = Common.isResolvable(value) ? value.describe() : value;
const value = test.args[key];
item.args[key] = Common.isResolvable(value) ? value.describe() : value;
}
}

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

Expand Down
53 changes: 36 additions & 17 deletions lib/types/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,24 +772,36 @@ module.exports = internals.Any = class {

const obj = this.clone();

if (options.refs) {
for (const key in options.args) {
const resolver = options.refs[key];
if (resolver) {
let arg = options.args[key];
if (Common.isResolvable(arg)) {
rule.resolve.push(key);
obj._refs.register(arg);
}
else {
if (resolver.normalize) {
arg = resolver.normalize(arg);
options.args[key] = arg;
// Args

const args = options.args;
if (args) {
for (const key in args) {
let arg = args[key];
if (arg === undefined) {
delete args[key];
continue;
}

if (options.refs) {
const resolver = options.refs[key];
if (resolver) {
if (Common.isResolvable(arg)) {
rule.resolve.push(key);
obj._refs.register(arg);
}
else {
if (resolver.normalize) {
arg = resolver.normalize(arg);
options.args[key] = arg;
}

Hoek.assert(resolver.assert(arg), resolver.message);
Hoek.assert(resolver.assert(arg), resolver.message);
}
}
}

args[key] = arg;
}
}

Expand All @@ -802,7 +814,14 @@ module.exports = internals.Any = class {
obj._ruleset = null;
}

const test = { rule, name, arg: options.args };
const test = { rule, name };

if (args &&
Object.keys(args).length) {

test.args = args;
}

if (rule.warn) {
test.warn = true;
}
Expand Down Expand Up @@ -865,15 +884,15 @@ module.exports = internals.Any = class {
return Validator.validate(value, this, state, prefs);
}

_test(name, arg, func, options) {
_test(name, args, func, options) {

const obj = this.clone();

if (obj._ruleset === false) {
obj._ruleset = null;
}

obj._tests.push({ func, name, arg, options });
obj._tests.push({ func, name, args, options });

return obj;
}
Expand Down
1 change: 1 addition & 0 deletions lib/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ internals.Array = class extends Any {

const hases = this._getRules('has');
for (const has of hases) {

if (id === has.args.schema._flags.id) {
const obj = this.clone();
has.args.schema = schema;
Expand Down
22 changes: 2 additions & 20 deletions lib/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ internals.Object = class extends Any {
const obj = this.clone();
const clone = Hoek.clone(test);
clone.rule._options.args.schema = schema;
clone.arg = clone.rule._options.args;
clone.args = clone.rule._options.args;
obj._tests[i] = clone;
return obj._rebuild();
}
Expand All @@ -316,24 +316,6 @@ internals.Object = class extends Any {

const description = super.describe();

if (description.rules) {
for (const rule of description.rules) {

// Coverage off for future-proof descriptions, only object().assert() is used right now

if (/* $lab:coverage:off$ */rule.arg &&
typeof rule.arg === 'object' &&
rule.arg.schema &&
rule.arg.ref /* $lab:coverage:on$ */) {

rule.arg = {
schema: rule.arg.schema.describe(),
ref: rule.arg.ref
};
}
}
}

if (this._inners.children &&
!shallow) {

Expand Down Expand Up @@ -618,7 +600,7 @@ internals.Object = class extends Any {
if (test.name === 'assert') {
const clone = Hoek.clone(test);
clone.rule._options.args.schema = clone.rule._options.args.schema.tailor(targets);
clone.arg = clone.rule._options.args;
clone.args = clone.rule._options.args;
obj._tests[i] = clone;
}
}
Expand Down
20 changes: 10 additions & 10 deletions test/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ describe('extension', () => {

expect(merged.describe()).to.equal({
type: 'myType',
rules: [{ name: 'test', arg: {} }, { name: 'test', arg: {} }]
rules: [{ name: 'test' }, { name: 'test' }]
});
});

Expand Down Expand Up @@ -1082,8 +1082,8 @@ describe('extension', () => {
expect(schema.describe()).to.equal({
type: 'myType',
rules: [
{ name: 'foo', arg: {} },
{ name: 'bar', arg: {} }
{ name: 'foo' },
{ name: 'bar' }
]
});
});
Expand All @@ -1110,7 +1110,7 @@ describe('extension', () => {
expect(schema.describe()).to.equal({
type: 'myType',
rules: [
{ name: 'foo', arg: { bar: 'bar', baz: 42, qux: { ref: 'value', key: 'a.b', path: ['a', 'b'] }, quux: { ref: 'global', key: 'c.d', path: ['c', 'd'] } } }
{ name: 'foo', args: { bar: 'bar', baz: 42, qux: { ref: 'value', key: 'a.b', path: ['a', 'b'] }, quux: { ref: 'global', key: 'c.d', path: ['c', 'd'] } } }
]
});
});
Expand Down Expand Up @@ -1147,8 +1147,8 @@ describe('extension', () => {
expect(schema.describe()).to.equal({
type: 'myType',
rules: [
{ name: 'foo', description: 'something', arg: { bar: 'bar' } },
{ name: 'bar', description: 'whatever', arg: { baz: 'baz' } }
{ name: 'foo', description: 'something', args: { bar: 'bar' } },
{ name: 'bar', description: 'whatever', args: { baz: 'baz' } }
]
});
});
Expand All @@ -1162,8 +1162,8 @@ describe('extension', () => {
expect(description).to.equal({
type: 'myType',
rules: [
{ name: 'foo', description: 'something', arg: { bar: 'bar' } },
{ name: 'bar', description: 'whatever', arg: { baz: 'baz' } }
{ name: 'foo', description: 'something', args: { bar: 'bar' } },
{ name: 'bar', description: 'whatever', args: { baz: 'baz' } }
]
});

Expand Down Expand Up @@ -1198,8 +1198,8 @@ describe('extension', () => {
expect(schema.describe()).to.equal({
type: 'zalgo',
rules: [
{ name: 'foo', description: 'something', arg: { bar: 'bar' } },
{ name: 'bar', description: 'whatever', arg: { baz: 'baz' } }
{ name: 'foo', description: 'something', args: { bar: 'bar' } },
{ name: 'bar', description: 'whatever', args: { baz: 'baz' } }
]
});
});
Expand Down
6 changes: 3 additions & 3 deletions test/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('Manifest', () => {
email: {
type: 'string',
invalids: [''],
rules: [{ name: 'email', arg: {} }]
rules: [{ name: 'email' }]
},
domain: {
type: 'string',
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('Manifest', () => {
{
type: 'string',
invalids: [''],
rules: [{ name: 'min', arg: { limit: 3 } }]
rules: [{ name: 'min', args: { limit: 3 } }]
}
]
},
Expand All @@ -112,7 +112,7 @@ describe('Manifest', () => {
failover: { value: 1 }
},
invalids: [''],
rules: [{ name: 'max', arg: { limit: 3 } }]
rules: [{ name: 'max', args: { limit: 3 } }]
},
required: {
type: 'string',
Expand Down
Loading

0 comments on commit fe8e21d

Please sign in to comment.