Skip to content

Instantly share code, notes, and snippets.

@heri16
Last active June 29, 2021 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heri16/6ae175a14f0b8363bbcb9bac68132d8a to your computer and use it in GitHub Desktop.
Save heri16/6ae175a14f0b8363bbcb9bac68132d8a to your computer and use it in GitHub Desktop.
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