From 0e28156923a26a71c15e0d7bb26f18ede3dc5728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Tue, 23 Mar 2021 11:13:05 +0000 Subject: [PATCH 1/5] fix: error messages compliance triggers --- .../src/pages/Triggers/helper.js | 97 ++++++++++++------- 1 file changed, 64 insertions(+), 33 deletions(-) diff --git a/new-lamassu-admin/src/pages/Triggers/helper.js b/new-lamassu-admin/src/pages/Triggers/helper.js index be00ec90..be1958de 100644 --- a/new-lamassu-admin/src/pages/Triggers/helper.js +++ b/new-lamassu-admin/src/pages/Triggers/helper.js @@ -5,11 +5,7 @@ import * as R from 'ramda' import React, { memo } from 'react' import * as Yup from 'yup' -import { - NumberInput, - TextInput, - RadioGroup -} from 'src/components/inputs/formik' +import { NumberInput, RadioGroup } from 'src/components/inputs/formik' import { H4, Label2, Label1, Info1, Info2 } from 'src/components/typography' import { errorColor } from 'src/styling/variables' import { transformNumber } from 'src/utils/number' @@ -100,16 +96,9 @@ const threshold = Yup.object().shape({ const requirement = Yup.object().shape({ requirement: Yup.string().required(), - suspensionDays: Yup.number().when('requirement', { - is: 'suspend', - then: Yup.number() - .required() - .min(1) - .label('Invalid value'), - otherwise: Yup.number() - .nullable() - .transform(() => null) - }) + suspensionDays: Yup.number() + .transform(transformNumber) + .nullable() }) const Schema = Yup.object() @@ -119,24 +108,66 @@ const Schema = Yup.object() threshold // direction }) - .test( - 'are-fields-set', - 'Invalid values', - ({ threshold, triggerType }, context) => { - const validator = { - txAmount: threshold => threshold.threshold >= 0, - txVolume: threshold => - threshold.threshold >= 0 && threshold.thresholdDays > 0, - txVelocity: threshold => - threshold.threshold > 0 && threshold.thresholdDays > 0, - consecutiveDays: threshold => threshold.thresholdDays > 0 - } - return ( - (triggerType && validator?.[triggerType](threshold)) || - context.createError({ path: 'threshold' }) - ) + .test(({ threshold, triggerType }, context) => { + const errorMessages = { + txAmount: threshold => 'Amount must be non negative', + txVolume: threshold => { + const thresholdMessage = 'Volume must be non negative' + const thresholdDaysMessage = 'Days must be positive' + let message = '' + if (threshold.threshold < 0) message = message.concat(thresholdMessage) + if (threshold.thresholdDays <= 0) + message = message + ? message.concat(', ' + thresholdDaysMessage) + : message.concat(thresholdDaysMessage) + console.log(message) + return message + }, + txVelocity: threshold => { + const thresholdMessage = 'Transactions must be non negative' + const thresholdDaysMessage = 'Days must be positive' + let message = '' + if (threshold.threshold <= 0) message = message.concat(thresholdMessage) + if (threshold.thresholdDays <= 0) + message = message + ? message.concat(', ' + thresholdDaysMessage) + : message.concat(thresholdDaysMessage) + console.log(message) + return message + }, + consecutiveDays: threshold => 'Days must be non negative' } - ) + const thresholdValidator = { + txAmount: threshold => threshold.threshold >= 0, + txVolume: threshold => + threshold.threshold >= 0 && threshold.thresholdDays > 0, + txVelocity: threshold => + threshold.threshold > 0 && threshold.thresholdDays > 0, + consecutiveDays: threshold => threshold.thresholdDays > 0 + } + + return ( + (triggerType && thresholdValidator?.[triggerType](threshold)) || + context.createError({ + path: 'threshold', + message: errorMessages?.[triggerType](threshold) + }) + ) + }) + .test(({ requirement }, context) => { + const requirementValidator = requirement => + requirement.requirement === 'suspend' + ? requirement.suspensionDays > 0 + : true + + return ( + (requirement && requirementValidator(requirement)) || + context.createError({ + path: 'requirement', + message: 'Suspension days must be positive' + }) + ) + }) // Direction V2 only // const directionSchema = Yup.object().shape({ direction }) @@ -504,7 +535,7 @@ const RequirementInput = () => { bold className={classes.suspensionDays} name="requirement.suspensionDays" - component={TextInput} + component={NumberInput} textAlign="center" /> )} From 6558f7b6840f7cdcb983b9b8dd0d2f1904f3f8ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Tue, 23 Mar 2021 18:28:35 +0000 Subject: [PATCH 2/5] fix: yup error messages --- new-lamassu-admin/src/pages/Triggers/helper.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/new-lamassu-admin/src/pages/Triggers/helper.js b/new-lamassu-admin/src/pages/Triggers/helper.js index be1958de..c5243553 100644 --- a/new-lamassu-admin/src/pages/Triggers/helper.js +++ b/new-lamassu-admin/src/pages/Triggers/helper.js @@ -110,10 +110,10 @@ const Schema = Yup.object() }) .test(({ threshold, triggerType }, context) => { const errorMessages = { - txAmount: threshold => 'Amount must be non negative', + txAmount: threshold => 'Amount must be greater than or equal to 0', txVolume: threshold => { - const thresholdMessage = 'Volume must be non negative' - const thresholdDaysMessage = 'Days must be positive' + const thresholdMessage = 'Volume must be greater than or equal to 0' + const thresholdDaysMessage = 'Days must be greater than 0' let message = '' if (threshold.threshold < 0) message = message.concat(thresholdMessage) if (threshold.thresholdDays <= 0) @@ -124,8 +124,9 @@ const Schema = Yup.object() return message }, txVelocity: threshold => { - const thresholdMessage = 'Transactions must be non negative' - const thresholdDaysMessage = 'Days must be positive' + const thresholdMessage = + 'Transactions must be greater than or equal to 0' + const thresholdDaysMessage = 'Days must be greater than 0' let message = '' if (threshold.threshold <= 0) message = message.concat(thresholdMessage) if (threshold.thresholdDays <= 0) @@ -135,7 +136,7 @@ const Schema = Yup.object() console.log(message) return message }, - consecutiveDays: threshold => 'Days must be non negative' + consecutiveDays: threshold => 'Days must be greater than or equal to 0' } const thresholdValidator = { txAmount: threshold => threshold.threshold >= 0, @@ -164,7 +165,7 @@ const Schema = Yup.object() (requirement && requirementValidator(requirement)) || context.createError({ path: 'requirement', - message: 'Suspension days must be positive' + message: 'Suspension days must be greater than 0' }) ) }) From c6bc9d81b2e7e167847ea23dfe739db490834568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Tue, 23 Mar 2021 18:38:10 +0000 Subject: [PATCH 3/5] fix: error messages dont match validation --- new-lamassu-admin/src/pages/Triggers/helper.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/new-lamassu-admin/src/pages/Triggers/helper.js b/new-lamassu-admin/src/pages/Triggers/helper.js index c5243553..75f5f552 100644 --- a/new-lamassu-admin/src/pages/Triggers/helper.js +++ b/new-lamassu-admin/src/pages/Triggers/helper.js @@ -124,8 +124,7 @@ const Schema = Yup.object() return message }, txVelocity: threshold => { - const thresholdMessage = - 'Transactions must be greater than or equal to 0' + const thresholdMessage = 'Transactions must be greater than 0' const thresholdDaysMessage = 'Days must be greater than 0' let message = '' if (threshold.threshold <= 0) message = message.concat(thresholdMessage) @@ -136,7 +135,7 @@ const Schema = Yup.object() console.log(message) return message }, - consecutiveDays: threshold => 'Days must be greater than or equal to 0' + consecutiveDays: threshold => 'Days must be greater than 0' } const thresholdValidator = { txAmount: threshold => threshold.threshold >= 0, From d612260becbbb402cdce480d1d295e14df29a198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Mon, 29 Mar 2021 11:48:29 +0100 Subject: [PATCH 4/5] refactor: return statement and error message building --- .../src/pages/Triggers/helper.js | 51 ++++++++----------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/new-lamassu-admin/src/pages/Triggers/helper.js b/new-lamassu-admin/src/pages/Triggers/helper.js index 75f5f552..4a26a357 100644 --- a/new-lamassu-admin/src/pages/Triggers/helper.js +++ b/new-lamassu-admin/src/pages/Triggers/helper.js @@ -114,25 +114,20 @@ const Schema = Yup.object() txVolume: threshold => { const thresholdMessage = 'Volume must be greater than or equal to 0' const thresholdDaysMessage = 'Days must be greater than 0' - let message = '' - if (threshold.threshold < 0) message = message.concat(thresholdMessage) - if (threshold.thresholdDays <= 0) - message = message - ? message.concat(', ' + thresholdDaysMessage) - : message.concat(thresholdDaysMessage) - console.log(message) - return message + const message = [thresholdMessage, thresholdDaysMessage] + if (threshold.threshold < 0 && threshold.thresholdDays <= 0) + return message.join(', ') + if (threshold.threshold < 0) return message[0] + if (threshold.thresholdDays <= 0) return message[1] }, txVelocity: threshold => { const thresholdMessage = 'Transactions must be greater than 0' const thresholdDaysMessage = 'Days must be greater than 0' - let message = '' - if (threshold.threshold <= 0) message = message.concat(thresholdMessage) - if (threshold.thresholdDays <= 0) - message = message - ? message.concat(', ' + thresholdDaysMessage) - : message.concat(thresholdDaysMessage) - console.log(message) + const message = [thresholdMessage, thresholdDaysMessage] + if (threshold.threshold <= 0 && threshold.thresholdDays <= 0) + return message.join(', ') + if (threshold.threshold <= 0) return message[0] + if (threshold.thresholdDays <= 0) return message[1] return message }, consecutiveDays: threshold => 'Days must be greater than 0' @@ -146,13 +141,12 @@ const Schema = Yup.object() consecutiveDays: threshold => threshold.thresholdDays > 0 } - return ( - (triggerType && thresholdValidator?.[triggerType](threshold)) || - context.createError({ - path: 'threshold', - message: errorMessages?.[triggerType](threshold) - }) - ) + if (triggerType && thresholdValidator[triggerType](threshold)) return + + return context.createError({ + path: 'threshold', + message: errorMessages[triggerType](threshold) + }) }) .test(({ requirement }, context) => { const requirementValidator = requirement => @@ -160,13 +154,12 @@ const Schema = Yup.object() ? requirement.suspensionDays > 0 : true - return ( - (requirement && requirementValidator(requirement)) || - context.createError({ - path: 'requirement', - message: 'Suspension days must be greater than 0' - }) - ) + if (requirement && requirementValidator(requirement)) return + + return context.createError({ + path: 'requirement', + message: 'Suspension days must be greater than 0' + }) }) // Direction V2 only From 2e4cfd20fb8b93e239e07df2150c7be0523a2595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Mon, 29 Mar 2021 14:32:11 +0100 Subject: [PATCH 5/5] refactor: error message building --- .../src/pages/Triggers/helper.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/new-lamassu-admin/src/pages/Triggers/helper.js b/new-lamassu-admin/src/pages/Triggers/helper.js index 4a26a357..ec9994ae 100644 --- a/new-lamassu-admin/src/pages/Triggers/helper.js +++ b/new-lamassu-admin/src/pages/Triggers/helper.js @@ -114,21 +114,18 @@ const Schema = Yup.object() txVolume: threshold => { const thresholdMessage = 'Volume must be greater than or equal to 0' const thresholdDaysMessage = 'Days must be greater than 0' - const message = [thresholdMessage, thresholdDaysMessage] - if (threshold.threshold < 0 && threshold.thresholdDays <= 0) - return message.join(', ') - if (threshold.threshold < 0) return message[0] - if (threshold.thresholdDays <= 0) return message[1] + const message = [] + if (threshold.threshold < 0) message.push(thresholdMessage) + if (threshold.thresholdDays <= 0) message.push(thresholdDaysMessage) + return message.join(', ') }, txVelocity: threshold => { const thresholdMessage = 'Transactions must be greater than 0' const thresholdDaysMessage = 'Days must be greater than 0' - const message = [thresholdMessage, thresholdDaysMessage] - if (threshold.threshold <= 0 && threshold.thresholdDays <= 0) - return message.join(', ') - if (threshold.threshold <= 0) return message[0] - if (threshold.thresholdDays <= 0) return message[1] - return message + const message = [] + if (threshold.threshold <= 0) message.push(thresholdMessage) + if (threshold.thresholdDays <= 0) message.push(thresholdDaysMessage) + return message.join(', ') }, consecutiveDays: threshold => 'Days must be greater than 0' }