diff options
Diffstat (limited to 'packages/assert/src/index.ts')
-rw-r--r-- | packages/assert/src/index.ts | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/packages/assert/src/index.ts b/packages/assert/src/index.ts index 92bcf6f03..7ad574ec7 100644 --- a/packages/assert/src/index.ts +++ b/packages/assert/src/index.ts @@ -1,9 +1,5 @@ -import { - Schema, - SchemaValidator, -} from '@0xproject/json-schemas'; -import BigNumber from 'bignumber.js'; -import * as ethereum_address from 'ethereum-address'; +import { Schema, SchemaValidator } from '@0xproject/json-schemas'; +import { addressUtils, BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import * as validUrl from 'valid-url'; @@ -16,14 +12,14 @@ export const assert = { }, isValidBaseUnitAmount(variableName: string, value: BigNumber) { assert.isBigNumber(variableName, value); + const isNegative = value.lessThan(0); + this.assert(!isNegative, `${variableName} cannot be a negative number, found value: ${value.toNumber()}`); const hasDecimals = value.decimalPlaces() !== 0; this.assert( - !hasDecimals, `${variableName} should be in baseUnits (no decimals), found value: ${value.toNumber()}`, + !hasDecimals, + `${variableName} should be in baseUnits (no decimals), found value: ${value.toNumber()}`, ); }, - isUndefined(value: any, variableName?: string): void { - this.assert(_.isUndefined(value), this.typeAssertionMessage(variableName, 'undefined', value)); - }, isString(variableName: string, value: string): void { this.assert(_.isString(value), this.typeAssertionMessage(variableName, 'string', value)); }, @@ -31,18 +27,23 @@ export const assert = { this.assert(_.isFunction(value), this.typeAssertionMessage(variableName, 'function', value)); }, isHexString(variableName: string, value: string): void { - this.assert(_.isString(value) && HEX_REGEX.test(value), - this.typeAssertionMessage(variableName, 'HexString', value)); + this.assert( + _.isString(value) && HEX_REGEX.test(value), + this.typeAssertionMessage(variableName, 'HexString', value), + ); }, isETHAddressHex(variableName: string, value: string): void { - this.assert(ethereum_address.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value)); + this.assert(addressUtils.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value)); this.assert( - ethereum_address.isAddress(value) && value.toLowerCase() === value, + addressUtils.isAddress(value) && value.toLowerCase() === value, `Checksummed addresses are not supported. Convert ${variableName} to lower case before passing`, ); }, - doesBelongToStringEnum(variableName: string, value: string, - stringEnum: any /* There is no base type for every string enum */): void { + doesBelongToStringEnum( + variableName: string, + value: string, + stringEnum: any /* There is no base type for every string enum */, + ): void { const doesBelongToStringEnum = !_.isUndefined(stringEnum[value]); const enumValues = _.keys(stringEnum); const enumValuesAsStrings = _.map(enumValues, enumValue => `'${enumValue}'`); @@ -62,7 +63,7 @@ export const assert = { this.assert(_.isBoolean(value), this.typeAssertionMessage(variableName, 'boolean', value)); }, isWeb3Provider(variableName: string, value: any): void { - const isWeb3Provider = _.isFunction((value).send) || _.isFunction((value).sendAsync); + const isWeb3Provider = _.isFunction(value.send) || _.isFunction(value.sendAsync); this.assert(isWeb3Provider, this.typeAssertionMessage(variableName, 'Web3.Provider', value)); }, doesConformToSchema(variableName: string, value: any, schema: Schema): void { @@ -75,11 +76,11 @@ Validation errors: ${validationResult.errors.join(', ')}`; this.assert(!hasValidationErrors, msg); }, isHttpUrl(variableName: string, value: any): void { - const isValidUrl = validUrl.isWebUri(value); + const isValidUrl = !_.isUndefined(validUrl.isWebUri(value)); this.assert(isValidUrl, this.typeAssertionMessage(variableName, 'http url', value)); }, isUri(variableName: string, value: any): void { - const isValidUri = validUrl.isUri(value); + const isValidUri = !_.isUndefined(validUrl.isUri(value)); this.assert(isValidUri, this.typeAssertionMessage(variableName, 'uri', value)); }, assert(condition: boolean, message: string): void { |