-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): validate config after load_config (#4381)
* feat(config): validate config after load_config * feat(config): update validation message * refactor(config): validate config before modify it * test(config): add should#fail for test cases * refactor(vlidata_config): update error message
- Loading branch information
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
module.exports = ctx => { | ||
const { config, log } = ctx; | ||
|
||
log.info('Validating config'); | ||
|
||
// Validation for config.url && config.root | ||
if (typeof config.url !== 'string') { | ||
throw new TypeError(`Invalid config detected: "url" should be string, not ${typeof config.url}!`); | ||
} | ||
if (config.url.trim().length <= 0) { | ||
throw new TypeError('Invalid config detected: "url" should not be empty!'); | ||
} | ||
|
||
if (typeof config.root !== 'string') { | ||
throw new TypeError(`Invalid config detected: "root" should be string, not ${typeof config.root}!`); | ||
} | ||
if (config.root.trim().length <= 0) { | ||
throw new TypeError('Invalid config detected: "root" should not be empty!'); | ||
} | ||
|
||
// Soft deprecate use_date_for_updated | ||
if (typeof config.use_date_for_updated === 'boolean') { | ||
log.warn('Deprecated config detected: "use_date_for_updated" is deprecated, please use "updated_option" instead. See https://hexo.io/docs/configuration for more details.'); | ||
} | ||
|
||
// Soft deprecate external_link Boolean | ||
if (typeof config.external_link === 'boolean') { | ||
log.warn('Deprecated config detected: "external_link" with a Boolean value is deprecated. See https://hexo.io/docs/configuration for more details.'); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
'use strict'; | ||
|
||
const { spy } = require('sinon'); | ||
|
||
describe('Validate config', () => { | ||
const Hexo = require('../../../lib/hexo'); | ||
const hexo = new Hexo(); | ||
const validateConfig = require('../../../lib/hexo/validate_config'); | ||
const defaultConfig = require('../../../lib/hexo/default_config'); | ||
let logSpy; | ||
|
||
beforeEach(() => { | ||
logSpy = spy(); | ||
hexo.config = JSON.parse(JSON.stringify(defaultConfig)); | ||
hexo.log.warn = logSpy; | ||
hexo.log.info = spy(); | ||
}); | ||
|
||
it('config.url - undefined', () => { | ||
delete hexo.config.url; | ||
|
||
try { | ||
validateConfig(hexo); | ||
should.fail(); | ||
} catch (e) { | ||
e.name.should.eql('TypeError'); | ||
e.message.should.eql('Invalid config detected: "url" should be string, not undefined!'); | ||
} | ||
}); | ||
|
||
it('config.url - wrong type', () => { | ||
hexo.config.url = true; | ||
|
||
try { | ||
validateConfig(hexo); | ||
should.fail(); | ||
} catch (e) { | ||
e.name.should.eql('TypeError'); | ||
e.message.should.eql('Invalid config detected: "url" should be string, not boolean!'); | ||
} | ||
}); | ||
|
||
it('config.url - empty', () => { | ||
hexo.config.url = ' '; | ||
|
||
try { | ||
validateConfig(hexo); | ||
should.fail(); | ||
} catch (e) { | ||
e.name.should.eql('TypeError'); | ||
e.message.should.eql('Invalid config detected: "url" should not be empty!'); | ||
} | ||
}); | ||
|
||
it('config.root - undefined', () => { | ||
delete hexo.config.root; | ||
|
||
try { | ||
validateConfig(hexo); | ||
should.fail(); | ||
} catch (e) { | ||
e.name.should.eql('TypeError'); | ||
e.message.should.eql('Invalid config detected: "root" should be string, not undefined!'); | ||
} | ||
}); | ||
|
||
it('config.root - wrong type', () => { | ||
hexo.config.root = true; | ||
|
||
try { | ||
validateConfig(hexo); | ||
should.fail(); | ||
} catch (e) { | ||
e.name.should.eql('TypeError'); | ||
e.message.should.eql('Invalid config detected: "root" should be string, not boolean!'); | ||
} | ||
}); | ||
|
||
it('config.root - empty', () => { | ||
hexo.config.root = ' '; | ||
|
||
try { | ||
validateConfig(hexo); | ||
should.fail(); | ||
} catch (e) { | ||
e.name.should.eql('TypeError'); | ||
e.message.should.eql('Invalid config detected: "root" should not be empty!'); | ||
} | ||
}); | ||
|
||
it('config.use_date_for_updated - depreacte', () => { | ||
hexo.config.use_date_for_updated = true; | ||
|
||
validateConfig(hexo); | ||
|
||
logSpy.calledOnce.should.be.true; | ||
logSpy.calledWith('Deprecated config detected: "use_date_for_updated" is deprecated, please use "updated_option" instead. See https://hexo.io/docs/configuration for more details.').should.be.true; | ||
}); | ||
|
||
it('config.external_link - depreacte Boolean value', () => { | ||
hexo.config.external_link = false; | ||
|
||
validateConfig(hexo); | ||
|
||
logSpy.calledOnce.should.be.true; | ||
logSpy.calledWith('Deprecated config detected: "external_link" with a Boolean value is deprecated. See https://hexo.io/docs/configuration for more details.').should.be.true; | ||
}); | ||
}); |