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

middleware applied before validation is called multiple times #2223

Open
josephperrott opened this issue Aug 11, 2022 · 3 comments
Open

middleware applied before validation is called multiple times #2223

josephperrott opened this issue Aug 11, 2022 · 3 comments

Comments

@josephperrott
Copy link

When a middleware is defined to be run before validation, it appears that it is called multiple times.

See example in this stackblitz by running node index.js not-test test in the terminal.

It looks like this has been brought up before in different ways and solved, but it appears that it is till occuring.

@shadowspawn
Copy link
Member

Thanks for the example code. In case it becomes unavailable:

const yargs = require('yargs');

const CommandModule = {
  builder: (argv) => {
    argv.middleware(async () => {
      console.log('command middleware');
    }, true);
  },
  handler: () => {
    console.log('command');
  },
  command: 'test',
};

function buildParser(localYargs) {
  return localYargs.command(CommandModule);
}

yargs()
  .middleware(async () => {
    console.log('global middleware');
  }, true)
  .command('not-test', false, buildParser)
  .parse(process.argv.slice(2), () => {
    console.log('complete');
  });
~/projects/node-t7j85c
❯ node index.js not-test test
global middleware
command middleware
command
global middleware
command middleware
complete

@shadowspawn
Copy link
Member

Related: #1802 #1966 #1978

@shadowspawn
Copy link
Member

(Reproduced with example code using Yargs v17.7.2.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants