-
-
Notifications
You must be signed in to change notification settings - Fork 106
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 method to return the error without throwing #176
Conversation
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 have one suggestion. Not sure how @sindresorhus sees this. About the code coverage it seems like you haven't covered all cases on this line.
I think it should be enough to test the createValidate
without a label as well. Something like
const checkNickname = ow.createValidate(ow.string.minLength(3));
|
||
t.notThrows(() => { | ||
const {error, value} = ow.validate(valueCauseError, ow.string); | ||
t.true(error instanceof Error); |
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.
Not sure but wouldn't it make more sense for error
to be of type string
and just contain the message of the error ?
@@ -107,6 +107,30 @@ checkPassword('foo'); | |||
//=> ArgumentError: Expected string `password` to have a minimum length of `6`, got `foo` | |||
``` | |||
|
|||
### ow.validate(predicate) |
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.
This needs a better name. Creating a "validator" is what ow.create
does. So this creates confusion.
|
||
### ow.createValidate(label, predicate) | ||
|
||
Create a reusable validator with a specific `label`. It returns error and value. |
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.
This description is the same as ow.validate
. I think you need to make all of this much clearer.
@param value - Value to test. | ||
@param predicate - Predicate to test against. | ||
*/ | ||
validate<T>(value: T, predicate: BasePredicate<T>): {error: Error | null; value: T}; |
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.
validate<T>(value: T, predicate: BasePredicate<T>): {error: Error | null; value: T}; | |
validate<T>(value: T, predicate: BasePredicate<T>): {error: Error?; value: T}; |
test('reusable validator', t => { | ||
const checkUsername = ow.create(ow.string.minLength(3)); | ||
const checkNickname = ow.createValidate('nickname', ow.string.minLength(3)); |
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.
This should be in a separate test.
|
||
t.notThrows(() => { | ||
const {error, value} = checkNickname('foo'); | ||
t.true(error === null); |
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.
Use t.is
@@ -108,6 +140,40 @@ Object.defineProperties(ow, { | |||
|
|||
test(value, label ?? (labelOrPredicate as string), predicate as BasePredicate<T>); | |||
} | |||
}, | |||
validate: { | |||
value: <T>(value: T, labelOrPredicate: unknown, predicate?: BasePredicate<T>) => { |
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.
labelOrPredicate
should not be unknown
.
@hilezir Bump |
Closing for lack of response. |
Hi, the project joi has API validate it returns {error, value} such as:
Or
.error(new Error(...))
So I make API
validate
andcreateValidate
it also returns {error, value}.What do you think?
Fixes #169