From a463598e00d34ea1b9cd95ca84fce02667db4c7f Mon Sep 17 00:00:00 2001 From: Luke Oliff Date: Mon, 21 Feb 2022 14:15:37 +0000 Subject: [PATCH 1/5] chore: add example for a simple regex match on an input --- .../inquirer/examples/regex-validate-input.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 packages/inquirer/examples/regex-validate-input.js diff --git a/packages/inquirer/examples/regex-validate-input.js b/packages/inquirer/examples/regex-validate-input.js new file mode 100644 index 000000000..4535d2044 --- /dev/null +++ b/packages/inquirer/examples/regex-validate-input.js @@ -0,0 +1,26 @@ +/** + * Filter and validate progress example + */ + +'use strict'; +const inquirer = require('..'); + +/* eslint-disable no-promise-executor-return */ +const questions = [ + { + type: 'input', + name: 'api_key', + message: 'Please enter a valid API key.', + validate(input) { + return Promise.resolve().then(() => { + if (!!input.match(/([a-f0-9]{40})/g)) { + return true; + } else throw "Please provide a valid API key."; + }); + }, + }, +]; + +inquirer.prompt(questions).then((answers) => { + console.log(JSON.stringify(answers, null, ' ')); +}); From d5e96695dcb42d7e1d92d4a26e8f32465a42c6ef Mon Sep 17 00:00:00 2001 From: Luke Oliff Date: Wed, 23 Feb 2022 07:18:18 +0000 Subject: [PATCH 2/5] Update packages/inquirer/examples/regex-validate-input.js Co-authored-by: Simon Boudrias --- packages/inquirer/examples/regex-validate-input.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/inquirer/examples/regex-validate-input.js b/packages/inquirer/examples/regex-validate-input.js index 4535d2044..b30f339c2 100644 --- a/packages/inquirer/examples/regex-validate-input.js +++ b/packages/inquirer/examples/regex-validate-input.js @@ -13,7 +13,7 @@ const questions = [ message: 'Please enter a valid API key.', validate(input) { return Promise.resolve().then(() => { - if (!!input.match(/([a-f0-9]{40})/g)) { + if (/([a-f0-9]{40})/g.test(input)) { return true; } else throw "Please provide a valid API key."; }); From cb293a6ae0c133dc89e8a6e3690fcba4799fbb84 Mon Sep 17 00:00:00 2001 From: Luke Oliff Date: Thu, 24 Feb 2022 05:57:35 +0000 Subject: [PATCH 3/5] fix: remove promise, makes it much more readible --- packages/inquirer/examples/regex-validate-input.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/inquirer/examples/regex-validate-input.js b/packages/inquirer/examples/regex-validate-input.js index 4535d2044..2116cd609 100644 --- a/packages/inquirer/examples/regex-validate-input.js +++ b/packages/inquirer/examples/regex-validate-input.js @@ -12,11 +12,9 @@ const questions = [ name: 'api_key', message: 'Please enter a valid API key.', validate(input) { - return Promise.resolve().then(() => { - if (!!input.match(/([a-f0-9]{40})/g)) { - return true; - } else throw "Please provide a valid API key."; - }); + if (!!input.match(/([a-f0-9]{40})/g)) { + return true; + } else throw 'Please provide a valid API key secret.'; }, }, ]; From af3c6e3e5dfd219897c7bf920a725cfb6308931f Mon Sep 17 00:00:00 2001 From: Luke Oliff Date: Thu, 24 Feb 2022 06:00:41 +0000 Subject: [PATCH 4/5] fix: reapply match change to test --- packages/inquirer/examples/regex-validate-input.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/inquirer/examples/regex-validate-input.js b/packages/inquirer/examples/regex-validate-input.js index 2116cd609..fad941397 100644 --- a/packages/inquirer/examples/regex-validate-input.js +++ b/packages/inquirer/examples/regex-validate-input.js @@ -12,7 +12,7 @@ const questions = [ name: 'api_key', message: 'Please enter a valid API key.', validate(input) { - if (!!input.match(/([a-f0-9]{40})/g)) { + if (/([a-f0-9]{40})/g.test(input)) { return true; } else throw 'Please provide a valid API key secret.'; }, From fe00620055f71f3a5c65e7b062aaa12141f82491 Mon Sep 17 00:00:00 2001 From: Luke Oliff Date: Fri, 25 Feb 2022 18:49:42 +0000 Subject: [PATCH 5/5] fix: linting errors --- packages/inquirer/examples/regex-validate-input.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/inquirer/examples/regex-validate-input.js b/packages/inquirer/examples/regex-validate-input.js index fad941397..56a7c2578 100644 --- a/packages/inquirer/examples/regex-validate-input.js +++ b/packages/inquirer/examples/regex-validate-input.js @@ -5,7 +5,6 @@ 'use strict'; const inquirer = require('..'); -/* eslint-disable no-promise-executor-return */ const questions = [ { type: 'input', @@ -14,7 +13,9 @@ const questions = [ validate(input) { if (/([a-f0-9]{40})/g.test(input)) { return true; - } else throw 'Please provide a valid API key secret.'; + } + + throw Error('Please provide a valid API key secret.'); }, }, ];