diff options
author | Fabio Berger <me@fabioberger.com> | 2017-11-14 06:42:42 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-11-14 06:42:42 +0800 |
commit | 3e8e3478a3d770b16a5e6fec1fbd43a3d7967450 (patch) | |
tree | d291bf183c343b864369e4ef204c5637d3168a21 | |
parent | cf29530dd0303e5ef1f49c1ce889bafc62921cf2 (diff) | |
download | dexon-0x-contracts-3e8e3478a3d770b16a5e6fec1fbd43a3d7967450.tar.gz dexon-0x-contracts-3e8e3478a3d770b16a5e6fec1fbd43a3d7967450.tar.zst dexon-0x-contracts-3e8e3478a3d770b16a5e6fec1fbd43a3d7967450.zip |
Rename assert sub-package and removed duplicate methods from 0x.js's assert module
-rw-r--r-- | packages/0x-assert/README.md (renamed from packages/assert/README.md) | 0 | ||||
-rw-r--r-- | packages/0x-assert/circle.yml (renamed from packages/assert/circle.yml) | 0 | ||||
-rw-r--r-- | packages/0x-assert/package.json (renamed from packages/assert/package.json) | 4 | ||||
-rw-r--r-- | packages/0x-assert/src/globals.d.ts (renamed from packages/assert/src/globals.d.ts) | 0 | ||||
-rw-r--r-- | packages/0x-assert/src/index.ts (renamed from packages/assert/src/index.ts) | 7 | ||||
-rw-r--r-- | packages/0x-assert/test/assert_test.ts (renamed from packages/assert/test/assert_test.ts) | 0 | ||||
-rw-r--r-- | packages/0x-assert/tsconfig.json (renamed from packages/assert/tsconfig.json) | 0 | ||||
-rw-r--r-- | packages/0x-assert/tslint.json (renamed from packages/assert/tslint.json) | 0 | ||||
-rw-r--r-- | packages/0x.js/src/utils/assert.ts | 82 |
9 files changed, 15 insertions, 78 deletions
diff --git a/packages/assert/README.md b/packages/0x-assert/README.md index 4e72d5bcb..4e72d5bcb 100644 --- a/packages/assert/README.md +++ b/packages/0x-assert/README.md diff --git a/packages/assert/circle.yml b/packages/0x-assert/circle.yml index 32b9e7315..32b9e7315 100644 --- a/packages/assert/circle.yml +++ b/packages/0x-assert/circle.yml diff --git a/packages/assert/package.json b/packages/0x-assert/package.json index 43ecf5371..c560ca5dc 100644 --- a/packages/assert/package.json +++ b/packages/0x-assert/package.json @@ -1,4 +1,5 @@ { + "private": true, "name": "0x-assert", "version": "0.0.3", "description": "Provides a standard way of performing type and schema validation across 0x projects", @@ -40,6 +41,7 @@ "bignumber.js": "~4.1.0", "ethereum-address": "^0.0.4", "lodash": "^4.17.4", - "valid-url": "^1.0.9" + "valid-url": "^1.0.9", + "0x-assert": "0.0.3" } } diff --git a/packages/assert/src/globals.d.ts b/packages/0x-assert/src/globals.d.ts index cc47f3113..cc47f3113 100644 --- a/packages/assert/src/globals.d.ts +++ b/packages/0x-assert/src/globals.d.ts diff --git a/packages/assert/src/index.ts b/packages/0x-assert/src/index.ts index b391a8bbb..bfbb56b23 100644 --- a/packages/assert/src/index.ts +++ b/packages/0x-assert/src/index.ts @@ -12,6 +12,13 @@ export const assert = { const isBigNumber = _.isObject(value) && (value as any).isBigNumber; this.assert(isBigNumber, this.typeAssertionMessage(variableName, 'BigNumber', value)); }, + isValidBaseUnitAmount(variableName: string, value: BigNumber) { + assert.isBigNumber(variableName, value); + const hasDecimals = value.decimalPlaces() !== 0; + this.assert( + !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)); }, diff --git a/packages/assert/test/assert_test.ts b/packages/0x-assert/test/assert_test.ts index 0e35f7f50..0e35f7f50 100644 --- a/packages/assert/test/assert_test.ts +++ b/packages/0x-assert/test/assert_test.ts diff --git a/packages/assert/tsconfig.json b/packages/0x-assert/tsconfig.json index 709e20154..709e20154 100644 --- a/packages/assert/tsconfig.json +++ b/packages/0x-assert/tsconfig.json diff --git a/packages/assert/tslint.json b/packages/0x-assert/tslint.json index 5842a872a..5842a872a 100644 --- a/packages/assert/tslint.json +++ b/packages/0x-assert/tslint.json diff --git a/packages/0x.js/src/utils/assert.ts b/packages/0x.js/src/utils/assert.ts index e5c9439f3..b6f6c5c95 100644 --- a/packages/0x.js/src/utils/assert.ts +++ b/packages/0x.js/src/utils/assert.ts @@ -5,62 +5,20 @@ import {SchemaValidator, Schema} from '0x-json-schemas'; import {Web3Wrapper} from '../web3_wrapper'; import {signatureUtils} from '../utils/signature_utils'; import {ECSignature} from '../types'; +import {assert as sharedAssert} from '0x-assert'; const HEX_REGEX = /^0x[0-9A-F]*$/i; -export const assert = { - isBigNumber(variableName: string, value: BigNumber): void { - const isBigNumber = _.isObject(value) && (value as any).isBigNumber; - this.assert(isBigNumber, this.typeAssertionMessage(variableName, 'BigNumber', value)); - }, - isValidBaseUnitAmount(variableName: string, value: BigNumber) { - assert.isBigNumber(variableName, value); - const hasDecimals = value.decimalPlaces() !== 0; - this.assert( - !hasDecimals, `${variableName} should be in baseUnits (no decimals), found value: ${value.toNumber()}`, - ); - }, +export const assert = _.extend({}, sharedAssert, { isValidSignature(orderHash: string, ecSignature: ECSignature, signerAddress: string) { const isValidSignature = signatureUtils.isValidSignature(orderHash, ecSignature, signerAddress); this.assert(isValidSignature, `Expected order with hash '${orderHash}' to have a valid signature`); }, - 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)); - }, - isFunction(variableName: string, value: any): void { - 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)); - }, - isETHAddressHex(variableName: string, value: string): void { - const web3 = new Web3(); - this.assert(web3.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value)); - this.assert( - web3.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 { - const doesBelongToStringEnum = !_.isUndefined(stringEnum[value]); - const enumValues = _.keys(stringEnum); - const enumValuesAsStrings = _.map(enumValues, enumValue => `'${enumValue}'`); - const enumValuesAsString = enumValuesAsStrings.join(', '); - assert.assert( - doesBelongToStringEnum, - `Expected ${variableName} to be one of: ${enumValuesAsString}, encountered: ${value}`, - ); - }, async isSenderAddressAsync(variableName: string, senderAddressHex: string, web3Wrapper: Web3Wrapper): Promise<void> { - assert.isETHAddressHex(variableName, senderAddressHex); + sharedAssert.isETHAddressHex(variableName, senderAddressHex); const isSenderAddressAvailable = await web3Wrapper.isSenderAddressAvailableAsync(senderAddressHex); - assert.assert(isSenderAddressAvailable, + sharedAssert.assert(isSenderAddressAvailable, `Specified ${variableName} ${senderAddressHex} isn't available through the supplied web3 provider`, ); }, @@ -68,34 +26,4 @@ export const assert = { const availableAddresses = await web3Wrapper.getAvailableAddressesAsync(); this.assert(!_.isEmpty(availableAddresses), 'No addresses were available on the provided web3 provider'); }, - hasAtMostOneUniqueValue(value: any[], errMsg: string): void { - this.assert(_.uniq(value).length <= 1, errMsg); - }, - isNumber(variableName: string, value: number): void { - this.assert(_.isFinite(value), this.typeAssertionMessage(variableName, 'number', value)); - }, - isBoolean(variableName: string, value: boolean): void { - this.assert(_.isBoolean(value), this.typeAssertionMessage(variableName, 'boolean', value)); - }, - isWeb3Provider(variableName: string, value: Web3.Provider): void { - const isWeb3Provider = _.isFunction((value as any).send) || _.isFunction((value as any).sendAsync); - this.assert(isWeb3Provider, this.typeAssertionMessage(variableName, 'Web3.Provider', value)); - }, - doesConformToSchema(variableName: string, value: any, schema: Schema): void { - const schemaValidator = new SchemaValidator(); - const validationResult = schemaValidator.validate(value, schema); - const hasValidationErrors = validationResult.errors.length > 0; - const msg = `Expected ${variableName} to conform to schema ${schema.id} -Encountered: ${JSON.stringify(value, null, '\t')} -Validation errors: ${validationResult.errors.join(', ')}`; - this.assert(!hasValidationErrors, msg); - }, - assert(condition: boolean, message: string): void { - if (!condition) { - throw new Error(message); - } - }, - typeAssertionMessage(variableName: string, type: string, value: any): string { - return `Expected ${variableName} to be of type ${type}, encountered: ${value}`; - }, -}; +}); |