From 878ac3c9378b4307d60a6215909a44f28b944fcb Mon Sep 17 00:00:00 2001 From: likun7981 Date: Mon, 26 Dec 2016 14:12:36 +0800 Subject: [PATCH 1/2] add name validate function for user custom and add test for it --- lib/options.js | 5 ++++- test/e2e/mock-metadata-repo-js/meta.js | 8 ++++++++ test/e2e/test.js | 11 ++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/options.js b/lib/options.js index a71bfb1b7d..376a6cacea 100644 --- a/lib/options.js +++ b/lib/options.js @@ -75,12 +75,15 @@ function setDefault (opts, key, val) { } function setValidateName (opts) { - opts.prompts.name.validate = function (name) { + var name = opts.prompts.name + var customValidate = name.validate + name.validate = function (name) { var its = validateName(name) if (!its.validForNewPackages) { var errors = (its.errors || []).concat(its.warnings || []) return 'Sorry, ' + errors.join(' and ') + '.' } + if (typeof customValidate === 'function') return customValidate(name) return true } } diff --git a/test/e2e/mock-metadata-repo-js/meta.js b/test/e2e/mock-metadata-repo-js/meta.js index f9478c27f1..8da267a811 100644 --- a/test/e2e/mock-metadata-repo-js/meta.js +++ b/test/e2e/mock-metadata-repo-js/meta.js @@ -4,6 +4,14 @@ module.exports = { type: 'string', required: true, message: 'Project description' + }, + name: { + type: 'string', + required: true, + label: 'Project name', + validate: function (input) { + return input === 'custom' ? 'can not input `custom`' : true + } } }, helpers: { diff --git a/test/e2e/test.js b/test/e2e/test.js index 2722002ff9..fdac7726ad 100644 --- a/test/e2e/test.js +++ b/test/e2e/test.js @@ -157,11 +157,20 @@ describe('vue-cli', () => { it('validate input value', done => { // deep copy - var invalidName = extend({}, answers, {name: 'INVALID-NAME'}) + var invalidName = extend({}, answers, { name: 'INVALID-NAME' }) monkeyPatchInquirer(invalidName) generate('INVALID-NAME', MOCK_TEMPLATE_REPO_PATH, MOCK_TEMPLATE_BUILD_PATH, err => { expect(err).to.be.an('error') done() }) }) + + it.only('custom validate', done => { + var invalidName = extend({}, answers, { name: 'custom' }) + monkeyPatchInquirer(invalidName) + generate('test', MOCK_METADATA_REPO_JS_PATH, MOCK_TEMPLATE_BUILD_PATH, err => { + expect(err).to.be.an('error') + done() + }) + }) }) From 0073ef4eb29d7dc9f92c7de75391aab2830439c2 Mon Sep 17 00:00:00 2001 From: likun7981 Date: Mon, 26 Dec 2016 14:14:31 +0800 Subject: [PATCH 2/2] remove test only --- test/e2e/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/test.js b/test/e2e/test.js index fdac7726ad..278d15e445 100644 --- a/test/e2e/test.js +++ b/test/e2e/test.js @@ -165,7 +165,7 @@ describe('vue-cli', () => { }) }) - it.only('custom validate', done => { + it('custom validate', done => { var invalidName = extend({}, answers, { name: 'custom' }) monkeyPatchInquirer(invalidName) generate('test', MOCK_METADATA_REPO_JS_PATH, MOCK_TEMPLATE_BUILD_PATH, err => {