aboutsummaryrefslogtreecommitdiffstats
path: root/packages/assert
diff options
context:
space:
mode:
Diffstat (limited to 'packages/assert')
-rw-r--r--packages/assert/CHANGELOG.md4
-rw-r--r--packages/assert/README.md53
-rw-r--r--packages/assert/package.json87
-rw-r--r--packages/assert/scripts/postpublish.js5
-rw-r--r--packages/assert/src/globals.d.ts4
-rw-r--r--packages/assert/src/index.ts39
-rw-r--r--packages/assert/test/assert_test.ts162
-rw-r--r--packages/assert/tsconfig.json21
-rw-r--r--packages/assert/tslint.json4
9 files changed, 161 insertions, 218 deletions
diff --git a/packages/assert/CHANGELOG.md b/packages/assert/CHANGELOG.md
index fd6bec3f4..cd04130de 100644
--- a/packages/assert/CHANGELOG.md
+++ b/packages/assert/CHANGELOG.md
@@ -1,6 +1,6 @@
# CHANGELOG
-v0.0.4 - _Nov. 14, 2017_
-------------------------
+## v0.0.4 - _Nov. 14, 2017_
+
* Re-publish Assert previously published under NPM package @0xproject/0x-assert
* Added assertion isValidBaseUnitAmount which checks both that the value is a valid bigNumber and that it does not contain decimals.
diff --git a/packages/assert/README.md b/packages/assert/README.md
index b0dc9a451..59ab63e91 100644
--- a/packages/assert/README.md
+++ b/packages/assert/README.md
@@ -1,10 +1,55 @@
-assert
-------
+## @0xproject/assert
Standard type and schema assertions to be used across all 0x projects and packages
-## Install
+## Installation
```bash
-npm install @0xproject/assert --save
+yarn add @0xproject/assert
+```
+
+## Usage
+
+```typescript
+import { assert } from '@0xproject/assert';
+
+assert.isValidBaseUnitAmount('baseUnitAmount', baseUnitAmount);
+```
+
+## Contributing
+
+We strongly encourage that the community help us make improvements and determine the future direction of the protocol. To report bugs within this package, please create an issue in this repository.
+
+Please read our [contribution guidelines](../../CONTRIBUTING.md) before getting started.
+
+### Install Dependencies
+
+If you don't have yarn workspaces enabled (Yarn < v1.0) - enable them:
+
+```bash
+yarn config set workspaces-experimental true
+```
+
+Then install dependencies
+
+```bash
+yarn install
+```
+
+### Build
+
+```bash
+yarn build
+```
+
+### Lint
+
+```bash
+yarn lint
+```
+
+### Run Tests
+
+```bash
+yarn test
```
diff --git a/packages/assert/package.json b/packages/assert/package.json
index 68d1ebb05..1c25b4e40 100644
--- a/packages/assert/package.json
+++ b/packages/assert/package.json
@@ -1,46 +1,45 @@
{
- "name": "@0xproject/assert",
- "version": "0.0.6",
- "description": "Provides a standard way of performing type and schema validation across 0x projects",
- "main": "lib/src/index.js",
- "types": "lib/src/index.d.ts",
- "scripts": {
- "build": "tsc",
- "clean": "shx rm -rf _bundles lib test_temp",
- "lint": "tslint --project . 'src/**/*.ts' 'test/**/*.ts'",
- "run_mocha": "mocha lib/test/**/*_test.js",
- "prepublishOnly": "run-p build",
- "test": "run-s clean build run_mocha",
- "test:circleci": "yarn test"
- },
- "license": "Apache-2.0",
- "repository": {
- "type": "git",
- "url": "https://github.com/0xProject/0x.js.git"
- },
- "bugs": {
- "url": "https://github.com/0xProject/0x.js/issues"
- },
- "homepage": "https://github.com/0xProject/0x.js/packages/assert/README.md",
- "devDependencies": {
- "@0xproject/tslint-config": "^0.2.0",
- "@types/lodash": "^4.14.86",
- "@types/mocha": "^2.2.42",
- "@types/valid-url": "^1.0.2",
- "chai": "^4.0.1",
- "chai-typescript-typings": "^0.0.1",
- "dirty-chai": "^2.0.1",
- "mocha": "^4.0.1",
- "npm-run-all": "^4.1.2",
- "shx": "^0.2.2",
- "tslint": "5.8.0",
- "typescript": "~2.6.1"
- },
- "dependencies": {
- "@0xproject/json-schemas": "^0.6.9",
- "bignumber.js": "~4.1.0",
- "ethereum-address": "^0.0.4",
- "lodash": "^4.17.4",
- "valid-url": "^1.0.9"
- }
+ "name": "@0xproject/assert",
+ "version": "0.0.12",
+ "description": "Provides a standard way of performing type and schema validation across 0x projects",
+ "main": "lib/src/index.js",
+ "types": "lib/src/index.d.ts",
+ "scripts": {
+ "build": "tsc",
+ "clean": "shx rm -rf _bundles lib test_temp",
+ "lint": "tslint --project . 'src/**/*.ts' 'test/**/*.ts'",
+ "run_mocha": "mocha lib/test/**/*_test.js",
+ "prepublishOnly": "run-p build",
+ "test": "run-s clean build run_mocha",
+ "test:circleci": "yarn test"
+ },
+ "license": "Apache-2.0",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/0xProject/0x.js.git"
+ },
+ "bugs": {
+ "url": "https://github.com/0xProject/0x.js/issues"
+ },
+ "homepage": "https://github.com/0xProject/0x.js/packages/assert/README.md",
+ "devDependencies": {
+ "@0xproject/tslint-config": "^0.4.3",
+ "@types/lodash": "^4.14.86",
+ "@types/mocha": "^2.2.42",
+ "@types/valid-url": "^1.0.2",
+ "chai": "^4.0.1",
+ "chai-typescript-typings": "^0.0.2",
+ "dirty-chai": "^2.0.1",
+ "mocha": "^4.0.1",
+ "npm-run-all": "^4.1.2",
+ "shx": "^0.2.2",
+ "tslint": "5.8.0",
+ "typescript": "~2.6.1"
+ },
+ "dependencies": {
+ "@0xproject/json-schemas": "^0.7.4",
+ "@0xproject/utils": "^0.2.1",
+ "lodash": "^4.17.4",
+ "valid-url": "^1.0.9"
+ }
}
diff --git a/packages/assert/scripts/postpublish.js b/packages/assert/scripts/postpublish.js
index 7fa452b08..16d67e03f 100644
--- a/packages/assert/scripts/postpublish.js
+++ b/packages/assert/scripts/postpublish.js
@@ -3,12 +3,13 @@ const packageJSON = require('../package.json');
const subPackageName = packageJSON.name;
-postpublish_utils.getLatestTagAndVersionAsync(subPackageName)
+postpublish_utils
+ .getLatestTagAndVersionAsync(subPackageName)
.then(function(result) {
const releaseName = postpublish_utils.getReleaseName(subPackageName, result.version);
const assets = [];
return postpublish_utils.publishReleaseNotes(result.tag, releaseName, assets);
})
- .catch (function(err) {
+ .catch(function(err) {
throw err;
});
diff --git a/packages/assert/src/globals.d.ts b/packages/assert/src/globals.d.ts
index cc47f3113..91ed2021e 100644
--- a/packages/assert/src/globals.d.ts
+++ b/packages/assert/src/globals.d.ts
@@ -1,5 +1 @@
declare module 'dirty-chai';
-
-declare module 'ethereum-address' {
- const isAddress: (arg: any) => boolean;
-}
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 {
diff --git a/packages/assert/test/assert_test.ts b/packages/assert/test/assert_test.ts
index 5fa96e49a..b0fa398d6 100644
--- a/packages/assert/test/assert_test.ts
+++ b/packages/assert/test/assert_test.ts
@@ -1,10 +1,10 @@
-import {schemas} from '@0xproject/json-schemas';
-import {BigNumber} from 'bignumber.js';
+import { schemas } from '@0xproject/json-schemas';
+import { BigNumber } from '@0xproject/utils';
import * as chai from 'chai';
import * as dirtyChai from 'dirty-chai';
import 'mocha';
-import {assert} from '../src/index';
+import { assert } from '../src/index';
chai.config.includeStack = true;
chai.use(dirtyChai);
@@ -14,75 +14,45 @@ describe('Assertions', () => {
const variableName = 'variable';
describe('#isBigNumber', () => {
it('should not throw for valid input', () => {
- const validInputs = [
- new BigNumber(23),
- new BigNumber('45'),
- ];
+ const validInputs = [new BigNumber(23), new BigNumber('45')];
validInputs.forEach(input => expect(assert.isBigNumber.bind(assert, variableName, input)).to.not.throw());
});
it('should throw for invalid input', () => {
- const invalidInputs = [
- 'test',
- 42,
- false,
- { random: 'test' },
- undefined,
- ];
+ const invalidInputs = ['test', 42, false, { random: 'test' }, undefined];
invalidInputs.forEach(input => expect(assert.isBigNumber.bind(assert, variableName, input)).to.throw());
});
});
- describe('#isUndefined', () => {
+ describe('#isValidBaseUnitAmount', () => {
it('should not throw for valid input', () => {
- const validInputs = [
- undefined,
- ];
- validInputs.forEach(input => expect(assert.isUndefined.bind(assert, input, variableName)).to.not.throw());
+ const validInputs = [new BigNumber(23), new BigNumber('45000000')];
+ validInputs.forEach(input =>
+ expect(assert.isValidBaseUnitAmount.bind(assert, variableName, input)).to.not.throw(),
+ );
});
it('should throw for invalid input', () => {
- const invalidInputs = [
- 'test',
- 42,
- false,
- { random: 'test' },
- ];
- invalidInputs.forEach(input => expect(assert.isUndefined.bind(assert, input, variableName)).to.throw());
+ const invalidInputs = [0, undefined, new BigNumber(3.145), 3.145, new BigNumber(-400)];
+ invalidInputs.forEach(input =>
+ expect(assert.isValidBaseUnitAmount.bind(assert, variableName, input)).to.throw(),
+ );
});
});
describe('#isString', () => {
it('should not throw for valid input', () => {
- const validInputs = [
- 'hello',
- 'goodbye',
- ];
+ const validInputs = ['hello', 'goodbye'];
validInputs.forEach(input => expect(assert.isString.bind(assert, variableName, input)).to.not.throw());
});
it('should throw for invalid input', () => {
- const invalidInputs = [
- 42,
- false,
- { random: 'test' },
- undefined,
- new BigNumber(45),
- ];
+ const invalidInputs = [42, false, { random: 'test' }, undefined, new BigNumber(45)];
invalidInputs.forEach(input => expect(assert.isString.bind(assert, variableName, input)).to.throw());
});
});
describe('#isFunction', () => {
it('should not throw for valid input', () => {
- const validInputs = [
- BigNumber,
- assert.isString.bind(this),
- ];
+ const validInputs = [BigNumber, assert.isString];
validInputs.forEach(input => expect(assert.isFunction.bind(assert, variableName, input)).to.not.throw());
});
it('should throw for invalid input', () => {
- const invalidInputs = [
- 42,
- false,
- { random: 'test' },
- undefined,
- new BigNumber(45),
- ];
+ const invalidInputs = [42, false, { random: 'test' }, undefined, new BigNumber(45)];
invalidInputs.forEach(input => expect(assert.isFunction.bind(assert, variableName, input)).to.throw());
});
});
@@ -127,9 +97,7 @@ describe('Assertions', () => {
'0x6FFFd0ae3f7d88c9b4925323f54c6e4b2918c5fd',
'0x6FFFd0ae3f7d88c9b4925323f54c6e4',
];
- invalidInputs.forEach(input =>
- expect(assert.isETHAddressHex.bind(assert, variableName, input)).to.throw(),
- );
+ invalidInputs.forEach(input => expect(assert.isETHAddressHex.bind(assert, variableName, input)).to.throw());
});
});
describe('#doesBelongToStringEnum', () => {
@@ -138,22 +106,13 @@ describe('Assertions', () => {
Test2 = 'Test2',
}
it('should not throw for valid input', () => {
- const validInputs = [
- TestEnums.Test1,
- TestEnums.Test2,
- ];
+ const validInputs = [TestEnums.Test1, TestEnums.Test2];
validInputs.forEach(input =>
expect(assert.doesBelongToStringEnum.bind(assert, variableName, input, TestEnums)).to.not.throw(),
);
});
it('should throw for invalid input', () => {
- const invalidInputs = [
- 42,
- false,
- { random: 'test' },
- undefined,
- new BigNumber(45),
- ];
+ const invalidInputs = [42, false, { random: 'test' }, undefined, new BigNumber(45)];
invalidInputs.forEach(input =>
expect(assert.doesBelongToStringEnum.bind(assert, variableName, input, TestEnums)).to.throw(),
);
@@ -162,19 +121,13 @@ describe('Assertions', () => {
describe('#hasAtMostOneUniqueValue', () => {
const errorMsg = 'more than one unique value';
it('should not throw for valid input', () => {
- const validInputs = [
- ['hello'],
- ['goodbye', 'goodbye', 'goodbye'],
- ];
+ const validInputs = [['hello'], ['goodbye', 'goodbye', 'goodbye']];
validInputs.forEach(input =>
expect(assert.hasAtMostOneUniqueValue.bind(assert, input, errorMsg)).to.not.throw(),
);
});
it('should throw for invalid input', () => {
- const invalidInputs = [
- ['hello', 'goodbye'],
- ['goodbye', 42, false, false],
- ];
+ const invalidInputs = [['hello', 'goodbye'], ['goodbye', 42, false, false]];
invalidInputs.forEach(input =>
expect(assert.hasAtMostOneUniqueValue.bind(assert, input, errorMsg)).to.throw(),
);
@@ -182,61 +135,34 @@ describe('Assertions', () => {
});
describe('#isNumber', () => {
it('should not throw for valid input', () => {
- const validInputs = [
- 42,
- 0,
- 21e+42,
- ];
+ const validInputs = [42, 0, 21e42];
validInputs.forEach(input => expect(assert.isNumber.bind(assert, variableName, input)).to.not.throw());
});
it('should throw for invalid input', () => {
- const invalidInputs = [
- false,
- { random: 'test' },
- undefined,
- new BigNumber(45),
- ];
+ const invalidInputs = [false, { random: 'test' }, undefined, new BigNumber(45)];
invalidInputs.forEach(input => expect(assert.isNumber.bind(assert, variableName, input)).to.throw());
});
});
describe('#isBoolean', () => {
it('should not throw for valid input', () => {
- const validInputs = [
- true,
- false,
- ];
+ const validInputs = [true, false];
validInputs.forEach(input => expect(assert.isBoolean.bind(assert, variableName, input)).to.not.throw());
});
it('should throw for invalid input', () => {
- const invalidInputs = [
- 42,
- { random: 'test' },
- undefined,
- new BigNumber(45),
- ];
+ const invalidInputs = [42, { random: 'test' }, undefined, new BigNumber(45)];
invalidInputs.forEach(input => expect(assert.isBoolean.bind(assert, variableName, input)).to.throw());
});
});
describe('#isWeb3Provider', () => {
it('should not throw for valid input', () => {
- const validInputs = [
- { send: () => 45 },
- { sendAsync: () => 45 },
- ];
+ const validInputs = [{ send: () => 45 }, { sendAsync: () => 45 }];
validInputs.forEach(input =>
expect(assert.isWeb3Provider.bind(assert, variableName, input)).to.not.throw(),
);
});
it('should throw for invalid input', () => {
- const invalidInputs = [
- 42,
- { random: 'test' },
- undefined,
- new BigNumber(45),
- ];
- invalidInputs.forEach(input =>
- expect(assert.isWeb3Provider.bind(assert, variableName, input)).to.throw(),
- );
+ const invalidInputs = [42, { random: 'test' }, undefined, new BigNumber(45)];
+ invalidInputs.forEach(input => expect(assert.isWeb3Provider.bind(assert, variableName, input)).to.throw());
});
});
describe('#doesConformToSchema', () => {
@@ -251,12 +177,7 @@ describe('Assertions', () => {
);
});
it('should throw for invalid input', () => {
- const invalidInputs = [
- 42,
- { random: 'test' },
- undefined,
- new BigNumber(45),
- ];
+ const invalidInputs = [42, { random: 'test' }, undefined, new BigNumber(45)];
invalidInputs.forEach(input =>
expect(assert.doesConformToSchema.bind(assert, variableName, input, schema)).to.throw(),
);
@@ -270,9 +191,7 @@ describe('Assertions', () => {
'https://api.radarrelay.com/0x/v0/',
'https://zeroex.beta.radarrelay.com:8000/0x/v0/',
];
- validInputs.forEach(input =>
- expect(assert.isHttpUrl.bind(assert, variableName, input)).to.not.throw(),
- );
+ validInputs.forEach(input => expect(assert.isHttpUrl.bind(assert, variableName, input)).to.not.throw());
});
it('should throw for invalid input', () => {
const invalidInputs = [
@@ -286,9 +205,7 @@ describe('Assertions', () => {
'user:password@api.example-relayer.net',
'//api.example-relayer.net',
];
- invalidInputs.forEach(input =>
- expect(assert.isHttpUrl.bind(assert, variableName, input)).to.throw(),
- );
+ invalidInputs.forEach(input => expect(assert.isHttpUrl.bind(assert, variableName, input)).to.throw());
});
});
describe('#isUri', () => {
@@ -302,9 +219,7 @@ describe('Assertions', () => {
'wss://www.api.example-relayer.net',
'user:password@api.example-relayer.net',
];
- validInputs.forEach(input =>
- expect(assert.isUri.bind(assert, variableName, input)).to.not.throw(),
- );
+ validInputs.forEach(input => expect(assert.isUri.bind(assert, variableName, input)).to.not.throw());
});
it('should throw for invalid input', () => {
const invalidInputs = [
@@ -316,9 +231,7 @@ describe('Assertions', () => {
'api.example-relayer.net',
'//api.example-relayer.net',
];
- invalidInputs.forEach(input =>
- expect(assert.isUri.bind(assert, variableName, input)).to.throw(),
- );
+ invalidInputs.forEach(input => expect(assert.isUri.bind(assert, variableName, input)).to.throw());
});
});
describe('#assert', () => {
@@ -332,8 +245,9 @@ describe('Assertions', () => {
});
describe('#typeAssertionMessage', () => {
it('should render correct message', () => {
- expect(assert.typeAssertionMessage('variable', 'string', 'number'))
- .to.equal(`Expected variable to be of type string, encountered: number`);
+ expect(assert.typeAssertionMessage('variable', 'string', 'number')).to.equal(
+ `Expected variable to be of type string, encountered: number`,
+ );
});
});
});
diff --git a/packages/assert/tsconfig.json b/packages/assert/tsconfig.json
index 709e20154..88a467ccb 100644
--- a/packages/assert/tsconfig.json
+++ b/packages/assert/tsconfig.json
@@ -1,18 +1,7 @@
{
- "compilerOptions": {
- "module": "commonjs",
- "target": "es5",
- "lib": [ "es2017", "dom"],
- "outDir": "lib",
- "sourceMap": true,
- "declaration": true,
- "noImplicitAny": true,
- "strictNullChecks": true
- },
- "include": [
- "./src/**/*",
- "./test/**/*",
- "../../node_modules/chai-typescript-typings/index.d.ts",
- "../../node_modules/web3-typescript-typings/index.d.ts"
- ]
+ "extends": "../../tsconfig",
+ "compilerOptions": {
+ "outDir": "lib"
+ },
+ "include": ["./src/**/*", "./test/**/*", "../../node_modules/chai-typescript-typings/index.d.ts"]
}
diff --git a/packages/assert/tslint.json b/packages/assert/tslint.json
index a07795151..ffaefe83a 100644
--- a/packages/assert/tslint.json
+++ b/packages/assert/tslint.json
@@ -1,5 +1,3 @@
{
- "extends": [
- "@0xproject/tslint-config"
- ]
+ "extends": ["@0xproject/tslint-config"]
}