Skip to content
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

Allow validate options to be passed through assert/attempt #1722 #1723

Merged
merged 1 commit into from May 29, 2019

Conversation

WesTyler
Copy link
Contributor

Per discussion in #1722

One note is that since attempt/assert leverage error.annotate(), the error message is messier
than I think @sarneeh was hoping for. This was already the case for attempt/assert on objects, though, and allowing abortEarly through does add the additional field annotations as well as the additional error.details.


const result = this.validate(value, schema);
const first = args[0];
const message = (
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be easier to just check if args[0] is an object?

Like:

const firstArgAsOptions = typeof args[0] === 'object';
const message = firstArgAsOptions ? null : args[0];
const options = firstArgAsOptions ? args[0] : args[1];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the possible values of message is a new Error instance, as new Error().

Unfortunately, that has the type 'object'

> typeof (new Error())
'object'

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed that case, you're right!

};

root.attempt = function (value, schema, message) {
root.attempt = function (value, schema, ...args/* [message], [options]*/) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thought: how about making it an [message | options] like in string regex function? This would allow to use validate options AND the custom message. The third argument would be then either 'custom message' or { message: 'custom message', ...otherValidateOptions }.

What do you think about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented it in a way that message, options, both, or neither are acceptable. I even added a couple of tests around using both message and options in a few different scenarios.

As far as following the regex() param usage pattern, that's definitely an option. I was trying to keep the impact on current usage as minimal as possible, because attempt is actually used in quite a few places by internals behind the scenes.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we can keep this implementation. I just wanted to share my thought :)

@@ -11,8 +11,8 @@
- [`validate(value, schema, [options], [callback])`](#validatevalue-schema-options-callback)
- [`compile(schema)`](#compileschema)
- [`describe(schema)`](#describeschema)
- [`assert(value, schema, [message])`](#assertvalue-schema-message)
- [`attempt(value, schema, [message])`](#attemptvalue-schema-message)
- [`assert(value, schema, [message], [options])`](#assertvalue-schema-message-options)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not clear that options can come in place of message. There is a notation for this case but I can't recall it right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just following the formatting for validate which has a similar flexible contract with options and callback both being optional.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. Then I guess that consistency is a better option here, let's leave it as is 😃

@jsardev
Copy link

jsardev commented Feb 13, 2019

Btw. thanks for taking this! 😃

@hueniverse hueniverse self-assigned this May 29, 2019
@hueniverse hueniverse added the feature New functionality or improvement label May 29, 2019
@hueniverse hueniverse added this to the 16.0.0 milestone May 29, 2019
@hueniverse hueniverse merged commit a700b3a into hapijs:master May 29, 2019
@hueniverse hueniverse added the v16 label Aug 10, 2019
@lock
Copy link

lock bot commented Jan 9, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants