Skip to content

Commit 0e9179a

Browse files
author
Joao Madeiras
committed
Add unsigned integer assert
1 parent 3be7105 commit 0e9179a

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ Tests if the value is a valid hash.
168168
### Integer
169169
Tests if the value is an integer.
170170

171+
#### Arguments
172+
- `allowString` (optional) - whether the assert accepts string inputs or not.
173+
- `unsigned` (optional) - whether the assert should consider only unsigned values or not.
174+
171175
### InternationalBankAccountNumber (*IBAN*)
172176
Tests if the value is a valid International Bank Account Number (_IBAN_) as defined in the [13616-1](http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=41031) standard.
173177

src/asserts/integer-assert.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import { Validator, Violation } from 'validator.js';
99
* Int regex.
1010
*/
1111

12-
const int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/;
12+
const int = {
13+
signed: /^(?:[-+]?(?:0|[1-9][0-9]*))$/,
14+
unsigned: /^(?:\d+)$/
15+
};
1316

1417
/**
1518
* Export `IntegerAssert`.
1619
*/
1720

18-
export default function integerAssert() {
21+
export default function integerAssert({ allowString = false, unsigned = false } = {}) {
1922
/**
2023
* Class name.
2124
*/
@@ -27,11 +30,15 @@ export default function integerAssert() {
2730
*/
2831

2932
this.validate = value => {
30-
if (typeof value !== 'number') {
33+
if (typeof value !== 'number' && !allowString) {
3134
throw new Violation(this, value, { value: Validator.errorCode.must_be_a_number });
3235
}
3336

34-
if (int.test(value) !== true) {
37+
if (unsigned && int.unsigned.test(value) !== true) {
38+
throw new Violation(this, value);
39+
}
40+
41+
if (int.signed.test(value) !== true) {
3542
throw new Violation(this, value);
3643
}
3744

test/asserts/integer-assert_test.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const Assert = BaseAssert.extend({
1919
* Test `IntegerAssert`.
2020
*/
2121

22-
describe('IntegerAssert', () => {
22+
describe.only('IntegerAssert', () => {
2323
it('should throw an error if the input value is not a number', () => {
2424
const choices = [{}, 'foo', '', [], 1.01, '2'];
2525

@@ -44,7 +44,31 @@ describe('IntegerAssert', () => {
4444
}
4545
});
4646

47+
it('should throw an error if strings are allowed but input is not valid', () => {
48+
try {
49+
new Assert().Integer({ allowString: true }).validate(' 1');
50+
51+
should.fail();
52+
} catch (e) {
53+
e.show().assert.should.equal('Integer');
54+
}
55+
});
56+
57+
it('should throw an error if only unsigned integers are allowed but input is not valid', () => {
58+
try {
59+
new Assert().Integer({ unsigned: true }).validate(-1);
60+
61+
should.fail();
62+
} catch (e) {
63+
e.show().assert.should.equal('Integer');
64+
}
65+
});
66+
67+
it('should accept an unsigned integer as a string if strings are allowed', () => {
68+
new Assert().Integer({ allowString: true, unsigned: true }).validate('1');
69+
});
70+
4771
it('should accept an integer', () => {
48-
new Assert().Integer().validate(1);
72+
new Assert().Integer().validate(+1);
4973
});
5074
});

0 commit comments

Comments
 (0)