aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeonid <logvinov.leon@gmail.com>2017-06-06 19:59:25 +0800
committerGitHub <noreply@github.com>2017-06-06 19:59:25 +0800
commitbf66a43ca81f799446fe031a972721f4352470b5 (patch)
tree602ec3bcdf65fbff84ccc35455653d1c95fd8829
parentb1ecb45f1bf5fed0762e7f17b00f46ae6fdc6b56 (diff)
parent1c7229d7837e92dc89769bd876dffdecc5669eaa (diff)
downloaddexon-sol-tools-bf66a43ca81f799446fe031a972721f4352470b5.tar
dexon-sol-tools-bf66a43ca81f799446fe031a972721f4352470b5.tar.gz
dexon-sol-tools-bf66a43ca81f799446fe031a972721f4352470b5.tar.bz2
dexon-sol-tools-bf66a43ca81f799446fe031a972721f4352470b5.tar.lz
dexon-sol-tools-bf66a43ca81f799446fe031a972721f4352470b5.tar.xz
dexon-sol-tools-bf66a43ca81f799446fe031a972721f4352470b5.tar.zst
dexon-sol-tools-bf66a43ca81f799446fe031a972721f4352470b5.zip
Merge pull request #34 from 0xProject/number-regex
Add tests to number regex and BigNumber serialization & fix bugs
-rw-r--r--src/schemas/order_schemas.ts2
-rw-r--r--test/schema_test.ts46
2 files changed, 47 insertions, 1 deletions
diff --git a/src/schemas/order_schemas.ts b/src/schemas/order_schemas.ts
index 72012dc26..4999f3e9d 100644
--- a/src/schemas/order_schemas.ts
+++ b/src/schemas/order_schemas.ts
@@ -7,7 +7,7 @@ export const addressSchema = {
export const numberSchema = {
id: '/numberSchema',
type: 'string',
- format: '\d+(\.\d+)?',
+ pattern: '^\\d+(\\.\\d+)?$',
};
export const orderSchema = {
diff --git a/test/schema_test.ts b/test/schema_test.ts
new file mode 100644
index 000000000..133914e6d
--- /dev/null
+++ b/test/schema_test.ts
@@ -0,0 +1,46 @@
+import 'mocha';
+import * as _ from 'lodash';
+import * as chai from 'chai';
+import * as BigNumber from 'bignumber.js';
+import promisify = require('es6-promisify');
+import {numberSchema} from '../src/schemas/order_schemas';
+import {SchemaValidator} from '../src/utils/schema_validator';
+
+chai.config.includeStack = true;
+const expect = chai.expect;
+
+describe('Schema', () => {
+ const validator = new SchemaValidator();
+ describe('#numberSchema', () => {
+ describe('number regex', () => {
+ it('should validate valid numbers', () => {
+ const testCases = ['42', '0', '1.3', '0.2', '00.00'];
+ _.forEach(testCases, (testCase: string) => {
+ expect(validator.validate(testCase as any, numberSchema).errors).to.be.lengthOf(0);
+ });
+ });
+ it('should fail for invalid numbers', () => {
+ const testCases = ['.3', '1.', 'abacaba', 'и', '1..0'];
+ _.forEach(testCases, (testCase: string) => {
+ expect(validator.validate(testCase as any, numberSchema).errors).to.be.lengthOf(1);
+ });
+ });
+ });
+ });
+ describe('BigNumber serialization', () => {
+ it.only('should correctly serialize BigNumbers', () => {
+ const testCases = {
+ '42': '42',
+ '0': '0',
+ '1.3': '1.3',
+ '0.2': '0.2',
+ '00.00': '0',
+ '.3': '0.3',
+ };
+ _.forEach(testCases, (serialized: string, input: string) => {
+ expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber(input)))
+ .to.be.equal(serialized);
+ });
+ });
+ });
+});