Skip to content

Instantly share code, notes, and snippets.

@heri16
Last active Jun 29, 2021
Embed
What would you like to do?
Yup validate input for too many decimals to prevent ethers.js BigNumber / BigInt underflow
let decimals = 4;
const regexpDecimals = new RegExp(`^\\d*\\.{0,1}\\d{0,${decimals}}$`);
const SignupSchema = Yup.object().shape({
decimal: Yup.number().test(
'is-decimal',
'too many decimals',
value => (value + "").match(regexpDecimals),
),
});
// Test cases
"424324.123456".match(new RegExp(`^\\d*\\.{0,1}\\d{0,6}$`));
"424324".match(new RegExp(`^\\d*\\.{0,1}\\d{0,6}$`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment