-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "options" to describe output #1132
Conversation
lib/any.js
Outdated
if (this._settings) { | ||
|
||
const optionSet = ['abortEarly', 'convert', 'allowUnknown', 'skipFunctions', 'stripUnknown', 'language', 'presence', 'context', 'noDefaults']; | ||
if (optionSet.some((option) => this._settings.hasOwnProperty(option))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aren't doing a double check on this._settings.hasOwnProperty
? I assume this is to prevent setting the empty object should https://github.com/hapijs/joi/pull/1132/files#diff-834068731e2e6ef966ecfef994bbae5eR694 always be false?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right. This was the cleaner option I could come up with, since this._settings
also contains ref
info from the .when
method.
The first hasOwnProperty
check in the conditional will only fire off for the first option
found (if any), and you are right - that was to prevent setting the empty object.
The second hasOwnProperty
is to ensure that only the options specified here can get reported by describe
. (Those specific options are referenced by the any.options
docs here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you expand on which properties you are filtering with this specifically ? Maybe they shouldn't be there in the 1st place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Marsup sorry, crazy busy at work the last week.
.when
is using this._settings
to save a reference to the baseType here. That's the main issue.
The settings I want to preserve are set by .options here and by .strict here.
Now that I look at it more, I think I can remove the filtering and just check to make sure that baseType
is not the only value and that it doesn't get set. I'll take a stab at that as soon as I get a chance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, just pushed a commit to (hopefully) clean up the logic per this discussion.
Cleaned up the
Scenario 1 also explicitly filters out the |
While this is still sitting here, I found a "quirk"(?) in the behavior that I'm not if I like or not. On this branch, using the new Joi.object().keys({
a: Joi.string()
}).options({ presence: 'required' }).describe();
/*
{ type: 'object',
options: { presence: 'required' },
children: {
a: {
type: 'string',
invalids: [ '' ]
}
}
}
*/ Thoughts on having the Joi.object().keys({
a: Joi.string().required()
}).required().describe();
/*
{ type: 'object',
flags: { presence: 'required' },
children: {
a: {
type: 'string',
flags: { presence: 'required' },
invalids: [ '' ]
}
}
}
*/ I think I like this, because the |
OK, had time to think about it peacefully. I think I have a better strategy, also fixing another potential issue you might not have seen yet :
Now the "correct place" is supposedly following the same logic as the validation, meaning that something (don't know what yet) should follow the last when, assuming that none of the whens has both Sounds like good enough directions to you ? |
And about the "quirk", options are passed all the way down, so we can either assume people will know that, or apply it, but descriptions will take a hit on performance. |
Alright, I pushed a couple of updates per discussions:
For the quirk, I would rather not apply it all the way down exactly for the performance reason. I don't know that the extra visibility is valuable enough to justify a perf hit. |
47109ce
to
e4a61c6
Compare
lib/any.js
Outdated
@@ -92,6 +92,7 @@ module.exports = internals.Any = class { | |||
obj.isJoi = true; | |||
obj._type = this._type; | |||
obj._settings = internals.concatSettings(this._settings); | |||
obj._baseType = Hoek.clone(this._baseType); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think you need a clone here, this has no effect on joi objects.
lib/any.js
Outdated
} | ||
|
||
if (this._baseType) { | ||
description.baseType = this._baseType.describe(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just base
should be enough.
lib/any.js
Outdated
@@ -683,6 +684,22 @@ module.exports = internals.Any = class { | |||
} | |||
} | |||
|
|||
if (this._settings) { | |||
|
|||
const settings = Hoek.clone(this._settings); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So all this block can be shortened to description.options = Hoek.clone(this._settings);
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. This used to be much messier, so when I pulled baseType
out of _settings I was happy to have removed a bunch of conditional logic and didn't even see that I could simplify it even more haha.
@Marsup anything else you're waiting on from me for this? No rush, just making sure I'm not holding anything up. |
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |
Adds the options set by
Joi.any().options({})
to the output from.describe()
, per #1130Old Behavior
New Behavior