Merge pull request #657 from josepfo/fix/error-messages-editing-triggers

Fix error messages compliance triggers
This commit is contained in:
Rafael Taranto 2021-04-30 17:55:19 +01:00 committed by GitHub
commit 8ddd8e9a2d

View file

@ -5,11 +5,7 @@ import * as R from 'ramda'
import React, { memo } from 'react' import React, { memo } from 'react'
import * as Yup from 'yup' import * as Yup from 'yup'
import { import { NumberInput, RadioGroup } from 'src/components/inputs/formik'
NumberInput,
TextInput,
RadioGroup
} from 'src/components/inputs/formik'
import { H4, Label2, Label1, Info1, Info2 } from 'src/components/typography' import { H4, Label2, Label1, Info1, Info2 } from 'src/components/typography'
import { errorColor } from 'src/styling/variables' import { errorColor } from 'src/styling/variables'
import { transformNumber } from 'src/utils/number' import { transformNumber } from 'src/utils/number'
@ -100,16 +96,9 @@ const threshold = Yup.object().shape({
const requirement = Yup.object().shape({ const requirement = Yup.object().shape({
requirement: Yup.string().required(), requirement: Yup.string().required(),
suspensionDays: Yup.number().when('requirement', { suspensionDays: Yup.number()
is: 'suspend', .transform(transformNumber)
then: Yup.number()
.required()
.min(1)
.label('Invalid value'),
otherwise: Yup.number()
.nullable() .nullable()
.transform(() => null)
})
}) })
const Schema = Yup.object() const Schema = Yup.object()
@ -119,11 +108,28 @@ const Schema = Yup.object()
threshold threshold
// direction // direction
}) })
.test( .test(({ threshold, triggerType }, context) => {
'are-fields-set', const errorMessages = {
'Invalid values', txAmount: threshold => 'Amount must be greater than or equal to 0',
({ threshold, triggerType }, context) => { txVolume: threshold => {
const validator = { const thresholdMessage = 'Volume must be greater than or equal to 0'
const thresholdDaysMessage = 'Days must be greater than 0'
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 = []
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'
}
const thresholdValidator = {
txAmount: threshold => threshold.threshold >= 0, txAmount: threshold => threshold.threshold >= 0,
txVolume: threshold => txVolume: threshold =>
threshold.threshold >= 0 && threshold.thresholdDays > 0, threshold.threshold >= 0 && threshold.thresholdDays > 0,
@ -131,12 +137,27 @@ const Schema = Yup.object()
threshold.threshold > 0 && threshold.thresholdDays > 0, threshold.threshold > 0 && threshold.thresholdDays > 0,
consecutiveDays: threshold => threshold.thresholdDays > 0 consecutiveDays: threshold => threshold.thresholdDays > 0
} }
return (
(triggerType && validator?.[triggerType](threshold)) || if (triggerType && thresholdValidator[triggerType](threshold)) return
context.createError({ path: 'threshold' })
) return context.createError({
} path: 'threshold',
) message: errorMessages[triggerType](threshold)
})
})
.test(({ requirement }, context) => {
const requirementValidator = requirement =>
requirement.requirement === 'suspend'
? requirement.suspensionDays > 0
: true
if (requirement && requirementValidator(requirement)) return
return context.createError({
path: 'requirement',
message: 'Suspension days must be greater than 0'
})
})
// Direction V2 only // Direction V2 only
// const directionSchema = Yup.object().shape({ direction }) // const directionSchema = Yup.object().shape({ direction })
@ -504,7 +525,7 @@ const RequirementInput = () => {
bold bold
className={classes.suspensionDays} className={classes.suspensionDays}
name="requirement.suspensionDays" name="requirement.suspensionDays"
component={TextInput} component={NumberInput}
textAlign="center" textAlign="center"
/> />
)} )}