Search code examples
javascriptarraysvorpal.js

Validating a command in Vorpal.js


When using the Vorpal.js code library, if I have created a command that looks like:

command-name [strategy]

and I have set the[strategy] command to accept only the values"insert", "update", and "upsert", how do I validate my code within the Vorpal.js framework? I'm guessing I would need to use some sort of validation function to parse the index and log an error message to the console if the index entry is not found. Or I could parse the index for each of the three strings instead. This would require marginally more code but I wonder, which is the most efficient way? Or perhaps people could suggest an implementation that is quicker still? Any suggestions of alternative methodologies would be great.

I am using the current build found at:

Vorpal.js code repository, Github


Solution

  • Vorpal doesn't have any custom validation method, so you can just manually validate it without too much trouble. Something like this would work:

    const valids = ['insert', 'update', 'upsert'];
    if (valids.indexOf(args.strategy) === -1) {
      this.log('Please enter a valid strategy');
      cb();
      return;
    }
    

    Update

    Adding a validation method is now on the roadmap for Vorpal.