Skip to content

Commit 71c819c

Browse files
afsampaiofranciscocardoso
authored andcommitted
1 parent 6ff9c79 commit 71c819c

12 files changed

+46
-32
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
},
4545
"devDependencies": {
4646
"abavalidator": "^2.0.1",
47-
"bignumber.js": "^8.0.2",
47+
"bignumber.js": "^9.0.0",
4848
"creditcard": "^0.1.2",
4949
"eslint": "^6.5.1",
5050
"eslint-config-uphold": "^1.0.1",
@@ -65,7 +65,7 @@
6565
},
6666
"optionalPeerDependencies": {
6767
"abavalidator": ">=2 <3",
68-
"bignumber.js": ">=7 <9",
68+
"bignumber.js": ">=7 <=9.0.0",
6969
"creditcard": ">=0.0.1 <1.0.0",
7070
"google-libphonenumber": ">=1 <4",
7171
"iban": ">=0.0.6 <1.0.0",

src/asserts/big-number-equal-to-assert.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Module dependencies.
55
*/
66

7+
const _ = require('lodash');
78
const { Assert: BaseAssert, Violation } = require('validator.js');
89
const BigNumberAssert = require('./big-number-assert');
910

@@ -45,19 +46,20 @@ module.exports = function bigNumberEqualToAssert(value, { validateSignificantDig
4546
*/
4647

4748
this.validate = value => {
48-
Assert.bigNumber({ validateSignificantDigits }).validate(value);
49-
5049
try {
50+
Assert.bigNumber({ validateSignificantDigits }).validate(value);
51+
5152
const number = new BigNumber(value);
5253

5354
if (!number.isEqualTo(this.value)) {
5455
throw new Error();
5556
}
5657
} catch (e) {
5758
const context = { value: this.value.toString() };
59+
const message = e.message || _.get(e, 'violation.message');
5860

59-
if (e.message.startsWith('[BigNumber Error]')) {
60-
context.message = e.message;
61+
if (message && message.startsWith('[BigNumber Error]')) {
62+
context.message = message;
6163
}
6264

6365
throw new Violation(this, value, context);

src/asserts/big-number-greater-than-assert.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Module dependencies.
55
*/
66

7+
const _ = require('lodash');
78
const { Assert: BaseAssert, Violation } = require('validator.js');
89
const BigNumberAssert = require('./big-number-assert');
910

@@ -45,19 +46,20 @@ module.exports = function bigNumberGreaterThanAssert(threshold, { validateSignif
4546
*/
4647

4748
this.validate = value => {
48-
Assert.bigNumber({ validateSignificantDigits }).validate(value);
49-
5049
try {
50+
Assert.bigNumber({ validateSignificantDigits }).validate(value);
51+
5152
const number = new BigNumber(value);
5253

5354
if (!number.isGreaterThan(this.threshold)) {
5455
throw new Error();
5556
}
5657
} catch (e) {
5758
const context = { threshold: this.threshold.toString() };
59+
const message = e.message || _.get(e, 'violation.message');
5860

59-
if (e.message.startsWith('[BigNumber Error]')) {
60-
context.message = e.message;
61+
if (message && message.startsWith('[BigNumber Error]')) {
62+
context.message = message;
6163
}
6264

6365
throw new Violation(this, value, context);

src/asserts/big-number-greater-than-or-equal-to-assert.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Module dependencies.
55
*/
66

7+
const _ = require('lodash');
78
const { Assert: BaseAssert, Violation } = require('validator.js');
89
const BigNumberAssert = require('./big-number-assert');
910

@@ -37,27 +38,27 @@ module.exports = function bigNumberGreaterThanOrEqualToAssert(threshold, { valid
3738
}
3839

3940
Assert.bigNumber({ validateSignificantDigits }).validate(threshold);
40-
4141
this.threshold = new BigNumber(threshold);
4242

4343
/**
4444
* Validation algorithm.
4545
*/
4646

4747
this.validate = value => {
48-
Assert.bigNumber({ validateSignificantDigits }).validate(value);
49-
5048
try {
49+
Assert.bigNumber({ validateSignificantDigits }).validate(value);
50+
5151
const number = new BigNumber(value);
5252

5353
if (!number.isGreaterThanOrEqualTo(this.threshold)) {
5454
throw new Error();
5555
}
5656
} catch (e) {
5757
const context = { threshold: this.threshold.toString() };
58+
const message = e.message || _.get(e, 'violation.message');
5859

59-
if (e.message.startsWith('[BigNumber Error]')) {
60-
context.message = e.message;
60+
if (message && message.startsWith('[BigNumber Error]')) {
61+
context.message = message;
6162
}
6263

6364
throw new Violation(this, value, context);

src/asserts/big-number-less-than-assert.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Module dependencies.
55
*/
66

7+
const _ = require('lodash');
78
const { Assert: BaseAssert, Violation } = require('validator.js');
89
const BigNumberAssert = require('./big-number-assert');
910

@@ -45,19 +46,20 @@ module.exports = function bigNumberLessThan(threshold, { validateSignificantDigi
4546
*/
4647

4748
this.validate = value => {
48-
Assert.bigNumber({ validateSignificantDigits }).validate(value);
49-
5049
try {
50+
Assert.bigNumber({ validateSignificantDigits }).validate(value);
51+
5152
const number = new BigNumber(value);
5253

5354
if (!number.isLessThan(this.threshold)) {
5455
throw new Error();
5556
}
5657
} catch (e) {
5758
const context = { threshold: this.threshold.toString() };
59+
const message = e.message || _.get(e, 'violation.message');
5860

59-
if (e.message.startsWith('[BigNumber Error]')) {
60-
context.message = e.message;
61+
if (message && message.startsWith('[BigNumber Error]')) {
62+
context.message = message;
6163
}
6264

6365
throw new Violation(this, value, context);

src/asserts/big-number-less-than-or-equal-to-assert.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Module dependencies.
55
*/
66

7+
const _ = require('lodash');
78
const { Assert: BaseAssert, Violation } = require('validator.js');
89
const BigNumberAssert = require('./big-number-assert');
910

@@ -45,19 +46,20 @@ module.exports = function bigNumberLessThanOrEqualToAssert(threshold, { validate
4546
*/
4647

4748
this.validate = value => {
48-
Assert.bigNumber({ validateSignificantDigits }).validate(value);
49-
5049
try {
50+
Assert.bigNumber({ validateSignificantDigits }).validate(value);
51+
5152
const number = new BigNumber(value);
5253

5354
if (!number.isLessThanOrEqualTo(this.threshold)) {
5455
throw new Error();
5556
}
5657
} catch (e) {
5758
const context = { threshold: this.threshold.toString() };
59+
const message = e.message || _.get(e, 'violation.message');
5860

59-
if (e.message.startsWith('[BigNumber Error]')) {
60-
context.message = e.message;
61+
if (message && message.startsWith('[BigNumber Error]')) {
62+
context.message = message;
6163
}
6264

6365
throw new Violation(this, value, context);

test/asserts/big-number-equal-to-assert.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ describe('BigNumberEqualToAssert', () => {
9090
}
9191
});
9292

93-
it('should expose `message` on the violation if the input value is not a number', () => {
93+
it('should expose `assert` equal to `BigNumberEqualTo` and `message` on the violation if the input value is not a number', () => {
9494
try {
9595
Assert.bigNumberEqualTo(10, option).validate({});
9696

9797
fail();
9898
} catch (e) {
99+
expect(e.show().assert).toBe('BigNumberEqualTo');
99100
expect(e.show().violation.message).toMatch(/Not a number/);
100101
}
101102
});

test/asserts/big-number-greater-than-assert.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ describe('BigNumberGreaterThanAssert', () => {
9090
}
9191
});
9292

93-
it('should expose `message` on the violation if the input value is not a number', () => {
93+
it('should expose `assert` equal to `BigNumberGreaterThan` and `message` on the violation if the input value is not a number', () => {
9494
try {
9595
Assert.bigNumberGreaterThan(10, option).validate({});
9696

9797
fail();
9898
} catch (e) {
99+
expect(e.show().assert).toBe('BigNumberGreaterThan');
99100
expect(e.show().violation.message).toMatch(/Not a number/);
100101
}
101102
});

test/asserts/big-number-greater-than-or-equal-to-assert.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ describe('BigNumberGreaterThanOrEqualToAssert', () => {
8080
}
8181
});
8282

83-
it('should expose `message` on the violation if the input value is not a number', () => {
83+
it('should expose `assert` equal to `BigNumberGreaterThanOrEqualTo` and `message` on the violation if the input value is not a number', () => {
8484
try {
8585
Assert.bigNumberGreaterThanOrEqualTo(10, option).validate({});
8686

8787
fail();
8888
} catch (e) {
89+
expect(e.show().assert).toBe('BigNumberGreaterThanOrEqualTo');
8990
expect(e.show().violation.message).toMatch(/Not a number/);
9091
}
9192
});

test/asserts/big-number-less-than-assert.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ describe('BigNumberLessThanAssert', () => {
9090
}
9191
});
9292

93-
it('should expose `message` on the violation if the input value is not a number', () => {
93+
it('should expose `assert` equal to `BigNumberLessThan` and `message` on the violation if the input value is not a number', () => {
9494
try {
9595
Assert.bigNumberLessThan(10, option).validate({});
9696

9797
fail();
9898
} catch (e) {
99+
expect(e.show().assert).toBe('BigNumberLessThan');
99100
expect(e.show().violation.message).toMatch(/Not a number/);
100101
}
101102
});

0 commit comments

Comments
 (0)