Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ Tests if the value is a valid hash.
### Integer
Tests if the value is an integer.

#### Arguments
- `allowString` (optional) - whether the assert accepts string inputs or not.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use strict instead?

- `unsigned` (optional) - whether the assert should consider only unsigned values or not.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should accept?


### InternationalBankAccountNumber (*IBAN*)
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.

Expand Down
15 changes: 11 additions & 4 deletions src/asserts/integer-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import { Validator, Violation } from 'validator.js';
* Int regex.
*/

const int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/;
const int = {
signed: /^(?:[-+]?(?:0|[1-9][0-9]*))$/,
unsigned: /^(?:\d+)$/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep same style? [0-9] instead of \d+?

};

/**
* Export `IntegerAssert`.
*/

export default function integerAssert() {
export default function integerAssert({ allowString = false, unsigned = false } = {}) {
/**
* Class name.
*/
Expand All @@ -27,11 +30,15 @@ export default function integerAssert() {
*/

this.validate = value => {
if (typeof value !== 'number') {
if (typeof value !== 'number' && !allowString) {
throw new Violation(this, value, { value: Validator.errorCode.must_be_a_number });
}

if (int.test(value) !== true) {
if (unsigned && int.unsigned.test(value) !== true) {
throw new Violation(this, value);
}

if (int.signed.test(value) !== true) {
throw new Violation(this, value);
}

Expand Down
26 changes: 25 additions & 1 deletion test/asserts/integer-assert_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,31 @@ describe('IntegerAssert', () => {
}
});

it('should throw an error if strings are allowed but input is not valid', () => {
try {
new Assert().Integer({ allowString: true }).validate(' 1');

should.fail();
} catch (e) {
e.show().assert.should.equal('Integer');
}
});

it('should throw an error if only unsigned integers are allowed but input is not valid', () => {
try {
new Assert().Integer({ unsigned: true }).validate(-1);

should.fail();
} catch (e) {
e.show().assert.should.equal('Integer');
}
});

it('should accept an unsigned integer as a string if strings are allowed', () => {
new Assert().Integer({ allowString: true, unsigned: true }).validate('1');
});

it('should accept an integer', () => {
new Assert().Integer().validate(1);
new Assert().Integer().validate(+1);
});
});