Skip to content

Commit

Permalink
fixes yup currency validation with min (#3688)
Browse files Browse the repository at this point in the history
* fixes yup currency validation with min

when using our custom 'currency' yup schema with a minimum value we do not catch
errors from 'CurrencyUtils.decode'. this results in a non-yup error during
validation if the value cannot be decoded.

an error other than 'yup.ValidationError' in an RPC endpoint crashes the node.

see #3662

* restores unique error message for min

updates tests expecting validation error message
  • Loading branch information
hughy authored Mar 23, 2023
1 parent e90d4a0 commit 4d33cf5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
8 changes: 6 additions & 2 deletions ironfish/src/rpc/routes/wallet/burnAsset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ describe('burnAsset', () => {
fee: '0',
value: '100',
}),
).rejects.toThrow('value must be equal to or greater than 1')
).rejects.toThrow(
'Request failed (400) validation: value must be equal to or greater than 1',
)
})
})

Expand All @@ -39,7 +41,9 @@ describe('burnAsset', () => {
fee: '1',
value: '-1',
}),
).rejects.toThrow('value must be equal to or greater than 1')
).rejects.toThrow(
'Request failed (400) validation: value must be equal to or greater than 1',
)
})
})

Expand Down
8 changes: 6 additions & 2 deletions ironfish/src/rpc/routes/wallet/mintAsset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ describe('mint', () => {
name: 'fake-coin',
value: '100',
}),
).rejects.toThrow('value must be equal to or greater than 1')
).rejects.toThrow(
'Request failed (400) validation: value must be equal to or greater than 1',
)
})
})

Expand All @@ -37,7 +39,9 @@ describe('mint', () => {
name: 'fake-coin',
value: '-1',
}),
).rejects.toThrow('value must be equal to or greater than 1')
).rejects.toThrow(
'Request failed (400) validation: value must be equal to or greater than 1',
)
})
})

Expand Down
1 change: 1 addition & 0 deletions ironfish/src/utils/yup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('YupUtils', () => {
it('currency', () => {
expect(YupUtils.currency().isValidSync(CurrencyUtils.encode(6n))).toBe(true)
expect(YupUtils.currency({ min: 0n }).isValidSync(CurrencyUtils.encode(-1n))).toBe(false)
expect(YupUtils.currency({ min: 0n }).isValidSync('0.1')).toBe(false)
expect(YupUtils.currency().isValidSync('hello world')).toBe(false)
expect(YupUtils.currency().isValidSync(0.00046)).toBe(false)
})
Expand Down
11 changes: 9 additions & 2 deletions ironfish/src/utils/yup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ export class YupUtils {
const min = options?.min

schema = schema.test(
`min`,
'min',
`value must be equal to or greater than ${min.toString()}`,
(val) => val == null || CurrencyUtils.decode(val) >= min,
(val) => {
if (val == null) {
return true
}

const [value] = CurrencyUtils.decodeTry(val)
return value != null && value >= min
},
)
}

Expand Down

0 comments on commit 4d33cf5

Please sign in to comment.