-
-
Notifications
You must be signed in to change notification settings - Fork 105
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 the ability to provide custom messages #177
Conversation
source/predicates/predicate.ts
Outdated
@@ -164,6 +166,25 @@ export class Predicate<T = unknown> implements BasePredicate<T> { | |||
}); | |||
} | |||
|
|||
/** | |||
Override the message thrown |
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 and more verbose description.
source/predicates/predicate.ts
Outdated
/** | ||
Override the message thrown | ||
|
||
@param newMessage | Either a string containing the new message or a function returning the new 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.
This is not the correct doc comment formatting.
I think all comments are fixed accordingly |
source/predicates/predicate.ts
Outdated
Function executed when the provided validation fails. | ||
The first argument provided to the function is the provided `value` for the property, | ||
the second argument is the optional `label` for the property. | ||
The returned value will be the error 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.
JSDoc has special syntax for documenting parameters and return 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.
Used the @param
and @returns
syntax
source/predicates/predicate.ts
Outdated
@@ -164,6 +172,25 @@ export class Predicate<T = unknown> implements BasePredicate<T> { | |||
}); | |||
} | |||
|
|||
/** | |||
Provide an new error message to be thrown when the validation fails. |
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.
Typo
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.
Fixed
source/predicates/predicate.ts
Outdated
the second argument is the optional `label` for the property. | ||
The returned value will be the error message. | ||
*/ | ||
export type ValidatorMessage<T> = (value: T, label?: string) => string; |
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.
It's not a validator message though, it creates the validator message. I think the name here could be improved.
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.
Changed it to ValidatorMessageBuilder
, couldn't really think on anything better. Any suggestions perhaps ?
.url.message('This is no url') | ||
); | ||
//=> ArgumentError: This is no url | ||
``` |
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.
The readme and TS doc comments should be in sync as much as possible. These examples are missing from the TS doc comments.
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.
Added two examples to the message
function in source/predicates/predicate.ts
. Would you like me to add all
of the examples above ?
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, if they are in the readme, they should go in index.d.ts.
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.
Also make sure the formatting is the same. It's not now.
Co-authored-by: Sindre Sorhus <[email protected]>
This PR was highly inspired by #156. I hope this fixes #122 . Let me know if you have some feedback
IssueHunt Summary
Referenced issues
This pull request has been submitted to: