refactor: reduce boilerplate in validation schemas

This commit is contained in:
José Oliveira 2021-02-24 21:50:54 +00:00 committed by Josh Harvey
parent 8d2c23ee87
commit 6e64a341de

View file

@ -10,6 +10,15 @@ import secretTest from './helper'
const isDefined = it => it && it.length const isDefined = it => it && it.length
const buildTestValidation = (id, passphrase) => {
return Yup.string()
.max(100, 'Too long')
.when(id, {
is: isDefined,
then: Yup.string().test(secretTest(passphrase))
})
}
export default { export default {
code: 'bitgo', code: 'bitgo',
name: 'BitGo', name: 'BitGo',
@ -93,40 +102,30 @@ export default {
.max(100, 'Too long') .max(100, 'Too long')
.required(), .required(),
BTCWalletId: Yup.string().max(100, 'Too long'), BTCWalletId: Yup.string().max(100, 'Too long'),
BTCWalletPassphrase: Yup.string() BTCWalletPassphrase: buildTestValidation(
.max(100, 'Too long') 'BTCWalletId',
.when('BTCWalletId', { account?.BTCWalletPassphrase
is: isDefined, ),
then: Yup.string().test(secretTest(account?.BTCWalletPassphrase))
}),
LTCWalletId: Yup.string().max(100, 'Too long'), LTCWalletId: Yup.string().max(100, 'Too long'),
LTCWalletPassphrase: Yup.string() LTCWalletPassphrase: buildTestValidation(
.max(100, 'Too long') 'LTCWalletId',
.when('LTCWalletId', { account?.LTCWalletPassphrase
is: isDefined, ),
then: Yup.string().test(secretTest(account?.LTCWalletPassphrase))
}),
ZECWalletId: Yup.string().max(100, 'Too long'), ZECWalletId: Yup.string().max(100, 'Too long'),
ZECWalletPassphrase: Yup.string() ZECWalletPassphrase: buildTestValidation(
.max(100, 'Too long') 'ZECWalletId',
.when('ZECWalletId', { account?.ZECWalletPassphrase
is: isDefined, ),
then: Yup.string().test(secretTest(account?.ZECWalletPassphrase))
}),
BCHWalletId: Yup.string().max(100, 'Too long'), BCHWalletId: Yup.string().max(100, 'Too long'),
BCHWalletPassphrase: Yup.string() BCHWalletPassphrase: buildTestValidation(
.max(100, 'Too long') 'BCHWalletId',
.when('BCHWalletId', { account?.BCHWalletPassphrase
is: isDefined, ),
then: Yup.string().test(secretTest(account?.BCHWalletPassphrase))
}),
DASHWalletId: Yup.string().max(100, 'Too long'), DASHWalletId: Yup.string().max(100, 'Too long'),
DASHWalletPassphrase: Yup.string() DASHWalletPassphrase: buildTestValidation(
.max(100, 'Too long') 'DASHWalletId',
.when('DASHWalletId', { account?.DASHWalletPassphrase
is: isDefined, ),
then: Yup.string().test(secretTest(account?.DASHWalletPassphrase))
}),
environment: Yup.string() environment: Yup.string()
.matches(/(prod|test)/) .matches(/(prod|test)/)
.required() .required()