Skip to content

Commit

Permalink
Merge pull request #1539 from stackernews/fix-1538
Browse files Browse the repository at this point in the history
fix #1538
  • Loading branch information
huumn authored Nov 5, 2024
2 parents 3112fc3 + db55cc7 commit 7efd089
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
24 changes: 13 additions & 11 deletions wallets/blink/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const fields = [
validate: string()
.matches(/^blink_[A-Za-z0-9]+$/, { message: 'must match pattern blink_A-Za-z0-9' }),
help: `you can get an API key from [Blink Dashboard](${galoyBlinkDashboardUrl}).\nPlease make sure to select ONLY the 'Read' and 'Write' scopes when generating this API key.`,
optional: 'for sending',
requiredWithout: 'apiKeyRecv'
requiredWithout: 'apiKeyRecv',
optional: 'for sending'
},
{
name: 'currency',
Expand All @@ -30,33 +30,35 @@ export const fields = [
validate: string()
.transform(value => value ? value.toUpperCase() : 'BTC')
.oneOf(['USD', 'BTC'], 'must be BTC or USD'),
optional: 'for sending'
optional: 'for sending',
requiredWithout: 'currencyRecv'
},
{
name: 'apiKeyRecv',
label: 'api key',
label: 'receive api key',
type: 'password',
help: `you can get an API key from [Blink Dashboard](${galoyBlinkDashboardUrl}).\nPlease make sure to select ONLY the 'Read' and 'Receive' scopes when generating this API key.`,
placeholder: 'blink_...',
optional: 'for receiving',
serverOnly: true,
requiredWithout: 'apiKey',
validate: string()
.matches(/^blink_[A-Za-z0-9]+$/, { message: 'must match pattern blink_A-Za-z0-9' })
.matches(/^blink_[A-Za-z0-9]+$/, { message: 'must match pattern blink_A-Za-z0-9' }),
optional: 'for receiving',
requiredWithout: 'apiKey'
},
{
name: 'currencyRecv',
label: 'wallet type',
label: 'receive wallet type',
type: 'text',
help: 'the blink wallet to use for receiving (only BTC available)',
value: 'BTC',
clear: true,
autoComplete: 'off',
optional: 'for receiving',
placeholder: 'BTC',
serverOnly: true,
validate: string()
.transform(value => value ? value.toUpperCase() : 'BTC')
.oneOf(['BTC'], 'must be BTC')
.oneOf(['BTC'], 'must be BTC'),
optional: 'for receiving',
requiredWithout: 'currency'
}
]

Expand Down
13 changes: 10 additions & 3 deletions wallets/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function composeWalletSchema (walletDef, serverSide, skipGenerated) {
const { fields } = walletDef

const vaultEntrySchemas = { required: [], optional: [] }
const cycleBreaker = []
const schemaShape = fields.reduce((acc, field) => {
const { name, validate, optional, generated, clientOnly, requiredWithout } = field

Expand All @@ -78,8 +79,14 @@ function composeWalletSchema (walletDef, serverSide, skipGenerated) {
if (!optional) {
acc[name] = acc[name].required('required')
} else if (requiredWithout) {
const myName = serverSide ? 'vaultEntries' : name
const partnerName = serverSide ? 'vaultEntries' : requiredWithout
// if a cycle breaker between this pair hasn't been added yet, add it
if (!cycleBreaker.some(pair => pair[1] === myName)) {
cycleBreaker.push([myName, partnerName])
}
// if we are the server, the pairSetting will be in the vaultEntries array
acc[name] = acc[name].when([serverSide ? 'vaultEntries' : requiredWithout], ([pairSetting], schema) => {
acc[name] = acc[name].when([partnerName], ([pairSetting], schema) => {
if (!pairSetting || (serverSide && !pairSetting.some(v => v.key === requiredWithout))) {
return schema.required(`required if ${requiredWithout} not set`)
}
Expand All @@ -99,9 +106,9 @@ function composeWalletSchema (walletDef, serverSide, skipGenerated) {
schemaShape.vaultEntries = Yup.array().equalto(vaultEntrySchemas)
}

// we use Object.keys(schemaShape).reverse() to avoid cyclic dependencies in Yup schema
// we use cycleBreaker to avoid cyclic dependencies in Yup schema
// see https://github.com/jquense/yup/issues/176#issuecomment-367352042
const composedSchema = Yup.object().shape(schemaShape, Object.keys(schemaShape).reverse()).concat(Yup.object({
const composedSchema = Yup.object().shape(schemaShape, cycleBreaker).concat(Yup.object({
enabled: Yup.boolean(),
priority: Yup.number().min(0, 'must be at least 0').max(100, 'must be at most 100')
}))
Expand Down

0 comments on commit 7efd089

Please sign in to comment.