Skip to content

Commit

Permalink
feat: add sequential message in validators
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-lara committed Feb 3, 2022
1 parent 17152d6 commit 1ea147d
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 18 deletions.
7 changes: 3 additions & 4 deletions packages/bot/src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const {
validatorDate,
} = require('./validators');
const BotLocalization = require('./bot-localization');
const { getValidationMessage } = require('./helper');

const localeDangle = '_localization';

Expand Down Expand Up @@ -302,10 +303,8 @@ class Bot extends Clonable {
session.restartDialog(context);
}
} else {
await session.say(
context.validation.message || 'Invalid value',
context
);
const message = getValidationMessage(context.validation);
await session.say(message, context);
return false;
}
}
Expand Down
27 changes: 22 additions & 5 deletions packages/bot/src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,37 @@
// a number or an string are not json objects
const isJsonObject = (content) => {
try {
if (typeof content !== 'object' && (typeof JSON.parse(content) !== 'object')) {
if (
typeof content !== 'object' &&
typeof JSON.parse(content) !== 'object'
) {
return false;
}
return true;
} catch (error) {
return false;
}
};

const trimInput = (input = '') => {
return input.replace(/\t/g, ' ').replace(/^( )*/, '').replace(/( )*$/, '').replace(/ +/g, ' ');

const trimInput = (input = '') =>
input
.replace(/\t/g, ' ')
.replace(/^( )*/, '')
.replace(/( )*$/, '')
.replace(/ +/g, ' ');

const getValidationMessage = (validation) => {
let message = validation.message || 'Invalid value';
if (Array.isArray(message)) {
const minIndex = Math.min(validation.currentRetry, message.length) - 1;

message = message[minIndex];
}
return message;
};

module.exports = {
isJsonObject,
trimInput,
getValidationMessage,
};
10 changes: 6 additions & 4 deletions packages/bot/src/test-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class TestConnector extends Connector {
if (this.settings.settings.trimInput) {
botText = trimInput(botText);
}
this.messages.push(`${botName}> ` + botText);
this.messages.push(`${botName}> ${botText}`);
}

async hear(line) {
Expand All @@ -82,9 +82,11 @@ class TestConnector extends Connector {
if (bot) {
const session = this.createSession({
channelId: 'console',
...(isJsonObject(line) ? { value: JSON.parse(line) } : { text: line }),
...(isJsonObject(line)
? { value: JSON.parse(line) }
: { text: line }),
type: 'message',
address: { conversation: { id: 'console000-' + testId } },
address: { conversation: { id: `console000-${testId}` } },
});
await bot.process(session);
} else {
Expand Down Expand Up @@ -114,7 +116,7 @@ class TestConnector extends Connector {
.split(/\r?\n/)
.filter((x) => !x.startsWith('#'))
.filter((x) => x)
.map(x => this.settings.settings.trimInput ? trimInput(x) : x);
.map((x) => (this.settings.settings.trimInput ? trimInput(x) : x));
this.messages = [];
const userName = this.settings.userName || 'user';
for (let i = 0; i < this.expected.length; i += 1) {
Expand Down
72 changes: 67 additions & 5 deletions packages/bot/test/helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,79 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

const {
isJsonObject
} = require('../src/helper');
const { isJsonObject, getValidationMessage } = require('../src/helper');

describe('isJsonObject', () => {
test('It should be able to detect objects', async () => {
expect(isJsonObject("text")).toEqual(false);
expect(isJsonObject('text')).toEqual(false);
expect(isJsonObject(123)).toEqual(false);
expect(isJsonObject({})).toEqual(true);
expect(isJsonObject({"text": "sample"})).toEqual(true);
expect(isJsonObject({ text: 'sample' })).toEqual(true);
expect(isJsonObject('{"text": 123}')).toEqual(true);
expect(isJsonObject('{"text: 123}')).toEqual(false);
});
});

describe('getValidationMessage', () => {
test('It should be always the same', async () => {
const validationCase1 = {
currentRetry: 1,
message: 'wrong value, try again',
};
expect(validationCase1.message).toEqual(
getValidationMessage(validationCase1)
);
validationCase1.currentRetry += 1;
expect(validationCase1.message).toEqual(
getValidationMessage(validationCase1)
);
});

test('It should be always the same although is a list', async () => {
const validationCase1 = {
currentRetry: 1,
message: ['wrong value, try again'],
};
expect(validationCase1.message[0]).toEqual(
getValidationMessage(validationCase1)
);
validationCase1.currentRetry += 1;
expect(validationCase1.message[0]).toEqual(
getValidationMessage(validationCase1)
);
validationCase1.currentRetry += 1;
expect(validationCase1.message[0]).toEqual(
getValidationMessage(validationCase1)
);
});

test('It should return a different error message', async () => {
const validationCase1 = {
currentRetry: 1,
message: [
'wrong value, try again',
'again is a wrong value, try one time more',
],
};
expect(validationCase1.message[0]).toEqual(
getValidationMessage(validationCase1)
);
validationCase1.currentRetry += 1;
expect(validationCase1.message[1]).toEqual(
getValidationMessage(validationCase1)
);
});

test('It should return the last message if current retry exceeds index', async () => {
const validationCase1 = {
currentRetry: 4,
message: [
'wrong value, try again',
'again is a wrong value, try one time more',
],
};
expect(validationCase1.message[validationCase1.message.length - 1]).toEqual(
getValidationMessage(validationCase1)
);
});
});

0 comments on commit 1ea147d

Please sign in to comment.