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: re-add options to check callback #2079

Merged
merged 4 commits into from Nov 30, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/yargs-factory.ts
Expand Up @@ -293,7 +293,10 @@ export class YargsInstance {
this[kTrackManuallySetKeys](keys);
return this;
}
check(f: (argv: Arguments) => any, global?: boolean): YargsInstance {
check(
f: (argv: Arguments, options: Options) => any,
global?: boolean
): YargsInstance {
argsert('<function> [boolean]', [f, global], arguments.length);
this.middleware(
(
Expand All @@ -304,7 +307,7 @@ export class YargsInstance {
Partial<Arguments> | Promise<Partial<Arguments>> | any
>(
() => {
return f(argv);
return f(argv, _yargs.getOptions());
},
(result: any): Partial<Arguments> | Promise<Partial<Arguments>> => {
if (!result) {
Expand Down
90 changes: 90 additions & 0 deletions test/validation.cjs
Expand Up @@ -14,6 +14,96 @@ describe('validation tests', () => {
yargs.getInternalMethods().reset();
});

describe('check', () => {
it('fails if error is thrown in check callback', done => {
yargs
.command(
'$0',
'default command desc',
yargs =>
yargs
.option('name', {
describe: 'name desc',
type: 'string',
alias: 'n',
})
.check(argv => {
const {name} = argv;
if (typeof name !== 'string' || !name.length) {
throw new Error('Option "name" must be a non-empty string');
}
return true;
}),
() => {
expect.fail();
}
)
.fail(() => {
return done();
})
.parse('--name');
});

it('does not fail if error is not thrown in check callback, and true is returned', () => {
yargs
.command(
'$0',
'default command desc',
yargs =>
yargs
.option('name', {
describe: 'version desc',
type: 'string',
alias: 'n',
})
.check(argv => {
const {name} = argv;
if (typeof name !== 'string' || !name.length) {
throw new Error('Option "name" must be a non-empty string');
}
return true;
}),
argv => argv
)
.fail(() => {
expect.fail();
})
.parse('--name Itachi');
});

it('callback has access to options', () => {
yargs
.command(
'$0',
'default command desc',
yargs =>
yargs
.option('name', {
describe: 'name desc',
type: 'string',
alias: 'n',
})
.check((_, options) => {
if (
typeof options !== 'object' ||
!Object.prototype.hasOwnProperty.call(options, 'string') ||
!options.string.includes('name')
) {
throw new Error(
'Check callback should have access to options'
);
}
return true;
}),
argv => argv
)
.fail(() => {
expect.fail();
})
.parse('--name Itachi');
});
});

describe('implies', () => {
const implicationsFailedPattern = new RegExp(
english['Implications failed:']
Expand Down