Skip to content

Commit a1b0b76

Browse files
feat: add returnValidation option to validate() (#66)
This allows programmatic usage of the validation library, for cases where you want to control the side effects of passing and failing validations * fix: dependency issue with npm-run-all and jenkins
1 parent 8b890f4 commit a1b0b76

File tree

6 files changed

+64
-20
lines changed

6 files changed

+64
-20
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ const config = { /* ... your webpack config */ }
7676
module.exports = validate(config, yourSchema)
7777
```
7878

79+
#### Advanced Usage
80+
If you need to access the validation results directly and want to control the side-effects (i.e. console.log output, `process.exit(1)` on fail behaviour) yourself, you can call the validation function like so: `validate(config, yourSchema, { returnValidation: true })`. This will make 1) the function return the validation results instead of your configuration and 2) not perform any side effects.
81+
7982
#### Support
8083
Because this module uses the amazing `Joi` validation library, this module only supports Node >=4.0.0.
8184

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"babel-preset-es2015": "6.6.0",
4747
"babel-preset-stage-2": "6.5.0",
4848
"babel-register": "6.7.2",
49+
"brace-expansion": "1.1.3",
4950
"codecov": "1.0.1",
5051
"commitizen": "^2.7.6",
5152
"compression-webpack-plugin": "0.3.1",

src/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,19 @@ const schema = Joi.object({
4848

4949

5050
// Easier consumability for require (default use case for non-transpiled webpack configs)
51-
module.exports = function validate(config, schema_ = schema) {
52-
Joi.assert(config, schema_)
51+
module.exports = function validate(config, schema_ = schema, options = {}) {
52+
const {
53+
// Don't return the config object and throw on error, but just return the validation result
54+
returnValidation, // bool
55+
} = options
56+
57+
const validationResult = Joi.validate(config, schema_, { abortEarly: false })
58+
if (returnValidation) return validationResult
59+
60+
if (validationResult.error) {
61+
console.error(validationResult.error.annotate())
62+
process.exit(1)
63+
}
5364
console.info(chalk.green('[webpack-validator] Config is valid.'))
5465
return config
5566
}

src/index.test.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,46 @@ import sinon from 'sinon'
22
import configs from '../test/passing-configs'
33
import validate from './'
44

5-
before(() => {
6-
!console.info.reset && sinon.stub(console, 'info')
7-
})
8-
95
describe('.', () => {
6+
let sandbox
7+
let processExitStub
8+
let consoleInfoStub
9+
let consoleErrorStub
10+
beforeEach(() => {
11+
sandbox = sinon.sandbox.create()
12+
consoleInfoStub = sandbox.stub(console, 'info')
13+
consoleErrorStub = sandbox.stub(console, 'error')
14+
processExitStub = sandbox.stub(process, 'exit')
15+
})
16+
afterEach(() => {
17+
sandbox.restore()
18+
})
19+
1020
configs.forEach(({ config, name }) => {
1121
it(`validates ${name}`, () => {
1222
validate(config)
23+
24+
// The success message should have been printed
25+
assert(consoleInfoStub.callCount === 1)
26+
27+
// The error message should not have been printed
28+
if (consoleErrorStub.callCount !== 0) {
29+
throw new Error(consoleErrorStub.args[0])
30+
}
31+
// process.exit should not have been called
32+
assert(processExitStub.callCount === 0)
1333
})
1434
})
35+
36+
it('for an invalid config the joi validation result is printed to console.error and ' +
37+
'the process exits with exit code 1', () => {
38+
const invalidConfig = { resolvee: 'bar' }
39+
validate(invalidConfig)
40+
41+
// The error message should have been printed
42+
assert(consoleErrorStub.callCount === 1)
43+
44+
// process.exit should have been called
45+
assert(processExitStub.callCount === 1)
46+
})
1547
})

test/utils/allInvalid.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,26 @@ export default (configs, schema) => {
1111
throw new Error('Pass data as `input` property')
1212
}
1313

14-
let result
15-
try {
16-
validate(invalidConfig, schema)
17-
} catch (e) {
18-
result = e
19-
}
14+
const result = validate(invalidConfig, schema, { returnValidation: true })
15+
assert(result.error)
16+
const { error } = result
17+
2018
if (throwError) {
21-
throw result
19+
throw error
2220
}
2321

24-
assert(result)
22+
assert(error)
2523
if (expectedError) {
2624
if (expectedError.path) {
27-
assert(result.details[0].path === expectedError.path)
25+
assert(error.details[0].path === expectedError.path)
2826
}
2927

3028
if (expectedError.type) {
31-
assert(result.details[0].type === expectedError.type)
29+
assert(error.details[0].type === expectedError.type)
3230
}
3331

3432
if (expectedError.message) {
35-
// console.log(result.details[0].message)
36-
// console.log(expectedError.message)
37-
assert(result.details[0].message === expectedError.message)
33+
assert(error.details[0].message === expectedError.message)
3834
}
3935
}
4036
})

test/utils/allValid.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import validate from '../../src/index'
66
export default (configs, schema) => {
77
configs.forEach((validConfig, n) => {
88
it(`valid #${n} should be valid`, () => {
9-
validate(validConfig, schema)
9+
const result = validate(validConfig, schema, { returnValidation: true })
10+
assert(result.error === null)
1011
})
1112
})
1213
}

0 commit comments

Comments
 (0)