Skip to content

Commit

Permalink
feat(validation): Add .skipValidation() method (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
elas7 authored and bcoe committed Apr 25, 2016
1 parent 6700cfd commit d72badb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -1142,6 +1142,7 @@ Valid `opt` keys include:
- `normalize`: boolean, apply `path.normalize()` to the option, see [`normalize()`](#normalize)
- `number`: boolean, interpret option as a number, [`number()`](#number)
- `requiresArg`: boolean, require the option be specified with a value, see [`requiresArg()`](#requiresArg)
- `skipValidation`: boolean, skips validation if the option is present, see [`skipValidation()`](#skipValidation)
- `string`: boolean, interpret option as a string, see [`string()`](#string)
- `type`: one of the following strings
- `'array'`: synonymous for `array: true`, see [`array()`](#array)
Expand Down Expand Up @@ -1282,6 +1283,12 @@ Missing argument value: f
Specify --help for available options
```

<a name="skipValidation"></a>.skipValidation(key)
-----------------

Specifies either a single option key (string), or an array of options.
If any of the options is present, yargs validation is skipped.

.strict()
---------

Expand Down
38 changes: 38 additions & 0 deletions test/yargs.js
Expand Up @@ -232,6 +232,7 @@ describe('yargs dsl tests', function () {
defaultDescription: {},
choices: {},
requiresArg: [],
skipValidation: [],
count: [],
normalize: [],
number: [],
Expand Down Expand Up @@ -886,6 +887,43 @@ describe('yargs dsl tests', function () {
argv.dotNotation.should.equal(false)
})
})

describe('skipValidation', function () {
it('skips validation if an option with skipValidation is present', function () {
var argv = yargs(['--koala', '--skip'])
.demand(1)
.fail(function (msg) {
expect.fail()
})
.skipValidation(['skip', 'reallySkip'])
.argv
argv.koala.should.equal(true)
})

it('does not skip validation if no option with skipValidation is present', function (done) {
var argv = yargs(['--koala'])
.demand(1)
.fail(function (msg) {
return done()
})
.skipValidation(['skip', 'reallySkip'])
.argv
argv.koala.should.equal(true)
})

it('allows key to be specified with option shorthand', function () {
var argv = yargs(['--koala', '--skip'])
.demand(1)
.fail(function (msg) {
expect.fail()
})
.option('skip', {
skipValidation: true
})
.argv
argv.koala.should.equal(true)
})
})
})

describe('yargs context', function () {
Expand Down
28 changes: 22 additions & 6 deletions yargs.js
Expand Up @@ -92,7 +92,7 @@ function Yargs (processArgs, cwd, parentRequire) {
groups = {}

var arrayOptions = [
'array', 'boolean', 'string', 'requiresArg',
'array', 'boolean', 'string', 'requiresArg', 'skipValidation',
'count', 'normalize', 'number'
]

Expand Down Expand Up @@ -279,6 +279,11 @@ function Yargs (processArgs, cwd, parentRequire) {
return self
}

self.skipValidation = function (skipValidations) {
options.skipValidation.push.apply(options.skipValidation, [].concat(skipValidations))
return self
}

self.implies = function (key, value) {
validation.implies(key, value)
return self
Expand Down Expand Up @@ -390,6 +395,8 @@ function Yargs (processArgs, cwd, parentRequire) {
self.count(key)
} if (opt.defaultDescription) {
options.defaultDescription[key] = opt.defaultDescription
} if (opt.skipValidation) {
self.skipValidation(key)
}

var desc = opt.describe || opt.description || opt.desc
Expand Down Expand Up @@ -657,26 +664,35 @@ function Yargs (processArgs, cwd, parentRequire) {
return
}

var helpOrVersion = false
var skipValidation = false

// Handle 'help' and 'version' options
Object.keys(argv).forEach(function (key) {
if (key === helpOpt && argv[key]) {
helpOrVersion = true
skipValidation = true
self.showHelp('log')
if (exitProcess) {
process.exit(0)
}
} else if (key === versionOpt && argv[key]) {
helpOrVersion = true
skipValidation = true
usage.showVersion()
if (exitProcess) {
process.exit(0)
}
}
})

// Check if any of the options to skip validation were provided
if (!skipValidation && options.skipValidation.length > 0) {
skipValidation = Object.keys(argv).some(function (key) {
return options.skipValidation.indexOf(key) >= 0
})
}

// If the help or version options where used and exitProcess is false,
// we won't run validations
if (!helpOrVersion) {
// or if explicitly skipped, we won't run validations
if (!skipValidation) {
if (parsed.error) throw parsed.error

// if we're executed via bash completion, don't
Expand Down

0 comments on commit d72badb

Please sign in to comment.