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

fix: print falsy value in error messages #824

Merged
merged 1 commit into from
Dec 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { message } from '../../message';
import { message } from '../message';

describe('message util', () => {
test('interpolates correctly', () => {
Expand All @@ -13,6 +13,18 @@ describe('message util', () => {
).toEqual('oops... "description" is missing;error: expected property to be truthy');
});

test.each([0, false, null, undefined])('interpolates %s value correctly', value => {
const template = 'Value must not equal {{value}}';
expect(
message(template, {
property: 'description',
error: 'expected property to be truthy',
path: '',
value,
}),
).toEqual(`Value must not equal ${value}`);
});

test('handles siblings', () => {
const template = '{{error}}{{error}}{{property}}{{bar}}{{error}}{{error}}';
expect(
Expand Down
2 changes: 1 addition & 1 deletion src/rulesets/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const message: MessageInterpolator = (str, values) => {

// tslint:disable-next-line:no-conditional-assignment
while ((result = BRACES.exec(str))) {
const newValue = String(values[result[1]] || '');
const newValue = result[1] in values ? String(values[result[1]]) : '';
str = `${str.slice(0, result.index)}${newValue}${str.slice(BRACES.lastIndex)}`;
BRACES.lastIndex = result.index + newValue.length;
}
Expand Down