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: nicer error messages for duration min/max violations #287

Merged
merged 1 commit into from
May 4, 2023
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
8 changes: 8 additions & 0 deletions messages/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ The value must be an integer.

The value must be between %s and %s (inclusive).

# errors.DurationBoundsMin

The value must be at least %s.

# errors.DurationBoundsMax

The value must be no more than %s.

# warning.prefix

Warning:
Expand Down
10 changes: 6 additions & 4 deletions src/flags/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ const validate = (input: string, config: DurationFlagConfig): Duration => {
throw messages.createError('errors.InvalidDuration');
}

if (min && parsedInput < min) {
throw messages.createError('errors.DurationBounds', [min, max]);
}
if (max && parsedInput > max) {
if (min && max && (parsedInput < min || parsedInput > max)) {
throw messages.createError('errors.DurationBounds', [min, max]);
} else if (min && parsedInput < min) {
throw messages.createError('errors.DurationBoundsMin', [min]);
} else if (max && parsedInput > max) {
throw messages.createError('errors.DurationBoundsMax', [max]);
}

return toDuration(parsedInput, unit);
};

Expand Down
67 changes: 67 additions & 0 deletions test/unit/flags/duration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,71 @@ describe('duration flag', () => {
});
});
});

describe('validation with min not max', () => {
const min = 1;
const buildProps = {
flags: {
wait: durationFlag({
min,
unit: 'minutes',
description: 'test',
char: 'w',
}),
},
};
it('passes', async () => {
const out = await Parser.parse(['--wait=10'], buildProps);
expect(out.flags.wait?.quantity).to.equal(10);
});
it('min passes', async () => {
const out = await Parser.parse([`--wait=${min}`], buildProps);
expect(out.flags.wait?.quantity).to.equal(min);
});
describe('failures', () => {
it('below min fails', async () => {
try {
const out = await Parser.parse([`--wait=${min - 1}`], buildProps);

throw new Error(`Should have thrown an error ${JSON.stringify(out)}`);
} catch (err) {
const error = err as Error;
expect(error.message).to.include(messages.getMessage('errors.DurationBoundsMin', [min]));
}
});
});
});

describe('validation with max not min', () => {
const max = 60;
const buildProps = {
flags: {
wait: durationFlag({
max,
unit: 'minutes',
description: 'test',
char: 'w',
}),
},
};
it('passes', async () => {
const out = await Parser.parse(['--wait=10'], buildProps);
expect(out.flags.wait?.quantity).to.equal(10);
});
it('max passes', async () => {
const out = await Parser.parse([`--wait=${max}`], buildProps);
expect(out.flags.wait?.quantity).to.equal(max);
});
describe('failures', () => {
it('above max fails', async () => {
try {
const out = await Parser.parse([`--wait=${max + 1}`], buildProps);
throw new Error(`Should have thrown an error ${JSON.stringify(out)}`);
} catch (err) {
const error = err as Error;
expect(error.message).to.include(messages.getMessage('errors.DurationBoundsMax', [max]));
}
});
});
});
});