-
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
Allow validate options to be passed through assert/attempt #1722 #1723
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,14 +167,21 @@ internals.root = function () { | |
} | ||
}; | ||
|
||
root.assert = function (value, schema, message) { | ||
root.assert = function (...args) { | ||
|
||
this.attempt(value, schema, message); | ||
this.attempt(...args); | ||
}; | ||
|
||
root.attempt = function (value, schema, message) { | ||
root.attempt = function (value, schema, ...args/* [message], [options]*/) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thought: how about making it an What do you think about that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I implemented it in a way that As far as following the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :) |
||
|
||
const result = this.validate(value, schema); | ||
const first = args[0]; | ||
const message = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be easier to just check if Like: const firstArgAsOptions = typeof args[0] === 'object';
const message = firstArgAsOptions ? null : args[0];
const options = firstArgAsOptions ? args[0] : args[1]; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One of the possible values of Unfortunately, that has the type > typeof (new Error())
'object' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I missed that case, you're right! |
||
first instanceof Error || | ||
typeof first === 'string' | ||
) ? first : null; | ||
|
||
const options = message ? args[1] : args[0]; | ||
const result = this.validate(value, schema, options); | ||
const error = result.error; | ||
if (error) { | ||
if (!message) { | ||
|
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.
I think this is not clear that
options
can come in place ofmessage
. There is a notation for this case but I can't recall it right now.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.
I was just following the formatting for
validate
which has a similar flexible contract withoptions
andcallback
both being optional.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.
True. Then I guess that consistency is a better option here, let's leave it as is 😃