diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-10-27 01:11:42 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-10-27 01:24:50 +0800 |
commit | 3206e1528b1f9924f3bfc191268304731448557f (patch) | |
tree | 96664f38e5456dc037f283faf9941759729e4842 | |
parent | 27d9e516e1cf6a8bea469998bc92513bec3d91c5 (diff) | |
download | dexon-sol-tools-3206e1528b1f9924f3bfc191268304731448557f.tar dexon-sol-tools-3206e1528b1f9924f3bfc191268304731448557f.tar.gz dexon-sol-tools-3206e1528b1f9924f3bfc191268304731448557f.tar.bz2 dexon-sol-tools-3206e1528b1f9924f3bfc191268304731448557f.tar.lz dexon-sol-tools-3206e1528b1f9924f3bfc191268304731448557f.tar.xz dexon-sol-tools-3206e1528b1f9924f3bfc191268304731448557f.tar.zst dexon-sol-tools-3206e1528b1f9924f3bfc191268304731448557f.zip |
feat: add assertions to render method
-rw-r--r-- | packages/instant/package.json | 2 | ||||
-rw-r--r-- | packages/instant/src/index.umd.ts | 13 | ||||
-rw-r--r-- | packages/instant/src/types.ts | 2 | ||||
-rw-r--r-- | packages/instant/src/util/assert.ts | 44 |
4 files changed, 60 insertions, 1 deletions
diff --git a/packages/instant/package.json b/packages/instant/package.json index 421802530..4a3add2ec 100644 --- a/packages/instant/package.json +++ b/packages/instant/package.json @@ -44,7 +44,9 @@ }, "homepage": "https://github.com/0xProject/0x-monorepo/packages/instant/README.md", "dependencies": { + "@0x/assert": "^1.0.14", "@0x/asset-buyer": "^2.1.0", + "@0x/json-schemas": "^2.0.0", "@0x/order-utils": "^2.0.0", "@0x/types": "^1.2.0", "@0x/typescript-typings": "^3.0.3", diff --git a/packages/instant/src/index.umd.ts b/packages/instant/src/index.umd.ts index f648b37f2..dabd45cae 100644 --- a/packages/instant/src/index.umd.ts +++ b/packages/instant/src/index.umd.ts @@ -1,9 +1,22 @@ +import * as _ from 'lodash'; import * as React from 'react'; import * as ReactDOM from 'react-dom'; import { DEFAULT_ZERO_EX_CONTAINER_SELECTOR } from './constants'; import { ZeroExInstant, ZeroExInstantProps } from './index'; +import { assert } from './util/assert'; export const render = (props: ZeroExInstantProps, selector: string = DEFAULT_ZERO_EX_CONTAINER_SELECTOR) => { + assert.isHexString('assetData', props.assetData); + assert.isValidLiquiditySource('liquiditySource', props.liquiditySource); + if (!_.isUndefined(props.additionalAssetMetaDataMap)) { + assert.isValidAssetMetaDataMap('additionalAssetMetaDataMap', props.additionalAssetMetaDataMap); + } + if (!_.isUndefined(props.defaultAssetBuyAmount)) { + assert.isNumber('defaultAssetBuyAmount', props.defaultAssetBuyAmount); + } + if (!_.isUndefined(props.networkId)) { + assert.isNumber('networkId', props.networkId); + } ReactDOM.render(React.createElement(ZeroExInstant, props), document.querySelector(selector)); }; diff --git a/packages/instant/src/types.ts b/packages/instant/src/types.ts index c63371fb4..e02a815f9 100644 --- a/packages/instant/src/types.ts +++ b/packages/instant/src/types.ts @@ -44,7 +44,7 @@ export interface ERC20AssetMetaData { export interface ERC721AssetMetaData { assetProxyId: AssetProxyId.ERC721; name: string; - representationUrl?: string; + imageUrl?: string; primaryColor?: string; } diff --git a/packages/instant/src/util/assert.ts b/packages/instant/src/util/assert.ts new file mode 100644 index 000000000..584d3d4b1 --- /dev/null +++ b/packages/instant/src/util/assert.ts @@ -0,0 +1,44 @@ +import { assert as sharedAssert } from '@0x/assert'; +import { schemas } from '@0x/json-schemas'; +import { assetDataUtils } from '@0x/order-utils'; +import { AssetProxyId, ObjectMap, SignedOrder } from '@0x/types'; +import * as _ from 'lodash'; + +import { AssetMetaData } from '../types'; + +export const assert = { + ...sharedAssert, + isValidLiquiditySource(variableName: string, liquiditySource: string | SignedOrder[]): void { + if (_.isString(liquiditySource)) { + sharedAssert.isUri(variableName, liquiditySource); + return; + } + sharedAssert.doesConformToSchema(variableName, liquiditySource, schemas.signedOrdersSchema); + }, + isValidAssetMetaDataMap(variableName: string, metaDataMap: ObjectMap<AssetMetaData>): void { + _.forEach(metaDataMap, (metaData, assetData) => { + assert.isHexString(`key ${assetData} of ${variableName}`, assetData); + assert.isValidAssetMetaData(`${variableName}.${assetData}`, metaData); + const assetDataProxyId = assetDataUtils.decodeAssetProxyId(assetData); + assert.assert( + metaData.assetProxyId === assetDataProxyId, + `Expected meta data for assetData ${assetData} to have asset proxy id of ${assetDataProxyId}, but instead got ${ + metaData.assetProxyId + }`, + ); + }); + }, + isValidAssetMetaData(variableName: string, metaData: AssetMetaData): void { + assert.isHexString(`${variableName}.assetProxyId`, metaData.assetProxyId); + if (!_.isUndefined(metaData.primaryColor)) { + assert.isString(`${variableName}.primaryColor`, metaData.primaryColor); + } + if (metaData.assetProxyId === AssetProxyId.ERC20) { + assert.isNumber(`${variableName}.decimals`, metaData.decimals); + assert.isString(`${variableName}.symbol`, metaData.symbol); + } else if (metaData.assetProxyId === AssetProxyId.ERC721) { + assert.isString(`${variableName}.name`, metaData.name); + assert.isUri(`${variableName}.imageUrl`, metaData.imageUrl); + } + }, +}; |