-
-
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
Changes from 2 commits
29319c5
b8e3231
3b24988
d94b4f0
7c3cef5
48b38cc
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 |
---|---|---|
|
@@ -4,6 +4,11 @@ import {not} from '../operators/not'; | |
import {BasePredicate, testSymbol} from './base-predicate'; | ||
import {Main} from '..'; | ||
|
||
/** | ||
@hidden | ||
*/ | ||
GentileFulvio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export type ValidatorMessage<T> = (value: T, label?: string) => string; | ||
sindresorhus marked this conversation as resolved.
Show resolved
Hide resolved
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. 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 commentThe reason will be displayed to describe this comment to others. Learn more. Changed it to |
||
|
||
/** | ||
@hidden | ||
*/ | ||
|
@@ -164,6 +169,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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
|
||
@param newMessage - Either a string containing the new message or a function returning the new message | ||
GentileFulvio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
message(newMessage: string | ValidatorMessage<T>) { | ||
const {validators} = this.context; | ||
|
||
validators[validators.length - 1].message = (value, label) => { | ||
if (typeof newMessage === 'function') { | ||
return newMessage(value, label); | ||
} | ||
|
||
return newMessage; | ||
}; | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
Register a new validator. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import test from 'ava'; | ||
import {default as ow, Predicate} from '../source'; | ||
|
||
class CustomPredicate extends Predicate<any> { | ||
constructor() { | ||
super('string'); | ||
} | ||
|
||
get unicorn() { | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to be \`🦄\`, got \`${value}\``, | ||
validator: value => value === '🦄' | ||
}); | ||
} | ||
} | ||
|
||
test('custom validate message', t => { | ||
t.throws(() => { | ||
ow('🌈', 'unicorn', new CustomPredicate().unicorn.message('Expect unicorn, got rainbow')); | ||
}, 'Expect unicorn, got rainbow'); | ||
|
||
t.throws(() => { | ||
ow('🌈', 'unicorn', new CustomPredicate().unicorn.message((value, label) => `Expected ${label}, to be \`🦄\` got \`${value}\``)); | ||
}, 'Expected string `unicorn`, to be `🦄` got `🌈`'); | ||
|
||
t.throws(() => { | ||
ow('🌈', ow.string.minLength(5).message((value, label) => `Expected ${label}, to be have a minimum length of 5, got \`${value}\``)); | ||
}, 'Expected string, to be have a minimum length of 5, got `🌈`'); | ||
|
||
t.throws(() => { | ||
ow('1234', ow.string.minLength(5).message((value, label) => `Expected ${label}, to be have a minimum length of 5, got \`${value}\``).url.message('This is no url')); | ||
}, 'Expected string, to be have a minimum length of 5, got `1234`'); | ||
|
||
t.throws(() => { | ||
ow('12345', ow.string.minLength(5).message((value, label) => `Expected ${label}, to be have a minimum length of 5, got \`${value}\``).url.message('This is no url')); | ||
}, '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 insource/predicates/predicate.ts
. Would you like me to addall
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.