UNPKG

validator.js-asserts

Version:
64 lines (47 loc) 1.15 kB
'use strict'; /** * Module dependencies. */ const { Violation } = require('validator.js'); let BigNumber; /** * Optional peer dependencies. */ try { BigNumber = require('bignumber.js'); // eslint-disable-next-line no-empty } catch {} /** * Export `BigNumberAssert`. */ module.exports = function bigNumberAssert({ validateSignificantDigits = true } = {}) { if (!BigNumber) { throw new Error('BigNumber is not installed'); } /** * Class name. */ this.__class__ = 'BigNumber'; /** * Validation algorithm. */ this.validate = value => { const originalDebug = BigNumber.DEBUG; BigNumber.DEBUG = !!validateSignificantDigits; try { const number = new BigNumber(value); if (Number.isNaN(number.toNumber())) { throw new Error(`[BigNumber Error] Not a number: ${value.toString()}`); } } catch (e) { if (e.message.startsWith('[BigNumber Error]')) { throw new Violation(this, value, { message: e.message }); } throw new Violation(this, value); } finally { BigNumber.DEBUG = originalDebug; } return true; }; return this; };