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(validator): Improve mandatory option values validation error mess… #139

Merged
merged 2 commits into from
May 3, 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
5 changes: 3 additions & 2 deletions lib/error/invalid-option-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const c = require('colorette');

class InvalidOptionValueError extends BaseError {
constructor(option, value, command, originalError, program) {
let msg = `Invalid value '${value}' for option ${c.italic(getDashedOption(option))}.\n ${originalError.meta.originalError}`;

const displayedValue = typeof value === "boolean" && value === true ? '(empty)' : `'${value}'`
const originalMessage = originalError.meta.error ? originalError.meta.error.message : ""
let msg = `Invalid value ${displayedValue} for option ${c.italic(getDashedOption(option))}. ${originalMessage}`;
super(msg, {option, command, originalError}, program);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Validator {
const throwAsInvalid = (_value, _e) => {
throw new ValidationError(
"Function validation failed",
{validator: this._validator, _value, originalError: _e},
{validator: this._validator, _value, error: _e},
this._program
);
}
Expand Down
5 changes: 2 additions & 3 deletions tests/option-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Passing --option invalid-value', () => {
program.option('-t, --time <time-in-secs>', 'Time in seconds, superior to zero', function(val) {
const o = parseInt(val);
if (isNaN(o) || o <= 0) {
throw new Error("FOOOO")
throw new Error("'time' must be a valid number")
}
return o;
});
Expand Down Expand Up @@ -236,7 +236,7 @@ describe('Setting up an option with a default value', () => {
});
});

describe('Setting up an option with an optionnal value', () => {
describe('Setting up an option with an optional value', () => {
it(`should work when no value is passed`, () => {

program
Expand Down Expand Up @@ -339,7 +339,6 @@ describe('Setting up a just one short option', () => {

program.parse(makeArgv(['foo', '-t', '2']));
should(action.called).be.ok();
console.dir(action.args[0]);
should(action.args[0][1]).eql({t:'2'});
program.reset();
});
Expand Down