refactor: return statement and error message building

This commit is contained in:
José Oliveira 2021-03-29 11:48:29 +01:00 committed by Josh Harvey
parent 17554f2847
commit 0379da13b7

View file

@ -114,25 +114,20 @@ const Schema = Yup.object()
txVolume: threshold => { txVolume: threshold => {
const thresholdMessage = 'Volume must be greater than or equal to 0' const thresholdMessage = 'Volume must be greater than or equal to 0'
const thresholdDaysMessage = 'Days must be greater than 0' const thresholdDaysMessage = 'Days must be greater than 0'
let message = '' const message = [thresholdMessage, thresholdDaysMessage]
if (threshold.threshold < 0) message = message.concat(thresholdMessage) if (threshold.threshold < 0 && threshold.thresholdDays <= 0)
if (threshold.thresholdDays <= 0) return message.join(', ')
message = message if (threshold.threshold < 0) return message[0]
? message.concat(', ' + thresholdDaysMessage) if (threshold.thresholdDays <= 0) return message[1]
: message.concat(thresholdDaysMessage)
console.log(message)
return message
}, },
txVelocity: threshold => { txVelocity: threshold => {
const thresholdMessage = 'Transactions must be greater than 0' const thresholdMessage = 'Transactions must be greater than 0'
const thresholdDaysMessage = 'Days must be greater than 0' const thresholdDaysMessage = 'Days must be greater than 0'
let message = '' const message = [thresholdMessage, thresholdDaysMessage]
if (threshold.threshold <= 0) message = message.concat(thresholdMessage) if (threshold.threshold <= 0 && threshold.thresholdDays <= 0)
if (threshold.thresholdDays <= 0) return message.join(', ')
message = message if (threshold.threshold <= 0) return message[0]
? message.concat(', ' + thresholdDaysMessage) if (threshold.thresholdDays <= 0) return message[1]
: message.concat(thresholdDaysMessage)
console.log(message)
return message return message
}, },
consecutiveDays: threshold => 'Days must be greater than 0' consecutiveDays: threshold => 'Days must be greater than 0'
@ -146,13 +141,12 @@ const Schema = Yup.object()
consecutiveDays: threshold => threshold.thresholdDays > 0 consecutiveDays: threshold => threshold.thresholdDays > 0
} }
return ( if (triggerType && thresholdValidator[triggerType](threshold)) return
(triggerType && thresholdValidator?.[triggerType](threshold)) ||
context.createError({ return context.createError({
path: 'threshold', path: 'threshold',
message: errorMessages?.[triggerType](threshold) message: errorMessages[triggerType](threshold)
}) })
)
}) })
.test(({ requirement }, context) => { .test(({ requirement }, context) => {
const requirementValidator = requirement => const requirementValidator = requirement =>
@ -160,13 +154,12 @@ const Schema = Yup.object()
? requirement.suspensionDays > 0 ? requirement.suspensionDays > 0
: true : true
return ( if (requirement && requirementValidator(requirement)) return
(requirement && requirementValidator(requirement)) ||
context.createError({ return context.createError({
path: 'requirement', path: 'requirement',
message: 'Suspension days must be greater than 0' message: 'Suspension days must be greater than 0'
}) })
)
}) })
// Direction V2 only // Direction V2 only