Skip to content

Commit

Permalink
fix: nicer error messages for duration min/max violations
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed May 3, 2023
1 parent 56d3d79 commit 9ec87e6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
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]));
}
});
});
});
});

0 comments on commit 9ec87e6

Please sign in to comment.