Skip to content

Commit

Permalink
fix iban when account number has letters
Browse files Browse the repository at this point in the history
  • Loading branch information
vinyll committed Jan 5, 2021
1 parent c27f460 commit b2cb90b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ function isEmailValid(email) {
return email.match(/[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@[a-z0-9-]+(\.[a-z0-9-]+)*/)
}

export function isIBANValid(IBAN) {
return IBAN.match(/^[0-9]{19}$/) &&
(98 - bigInt(`${IBAN.slice(2)}121700`).mod(97).toJSNumber()) === parseInt(IBAN.slice(0, 2))
export function isIBANValid(iban) {
const matches = iban.match(/^CH(\d{2})(\d{5})([A-Z0-9]{12})$/)
return Boolean(matches)

}

function isRCCValid(RCC) {
Expand Down
18 changes: 16 additions & 2 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getDurationLabel,
getServiceLightLabel,
isAuthorValid,
isIBANValid,
isPatientValid,
isPreferredServicesValid,
isServicePriceValid,
Expand Down Expand Up @@ -39,7 +40,7 @@ describe('utils', () => {
city: 'San Antonio',
email: 'gaery0@over-blog.com',
phone: '5164459701',
iban: '5131234567890123456',
iban: 'CH5131234567890123456',
rcc: 'V123123'
}

Expand Down Expand Up @@ -193,8 +194,22 @@ describe('utils', () => {
})
})

describe('#isIBANValid()', () => {
it('should match a valid Swiss IBAN', () => {
assert.strictEqual(isIBANValid('CH100023000A109822346'), true)
assert.strictEqual(isIBANValid('AB100023000A109822346'), false, "must start with CH")
assert.strictEqual(isIBANValid('CH100023000A10982234'), false, "must be 21 chars")
assert.strictEqual(isIBANValid('CH1X0023000A109822346'), false, "control digit must be 2 digits")
assert.strictEqual(isIBANValid('CH1000X3000A109822346'), false, "Bank Clearing must be 5 digits")
assert.strictEqual(isIBANValid('CH100023000A109-82234'), false, "Account Number must be numbers and letters")
// TODO
// assert.strictEqual(isIBANValid('CH100023000A109822344'), false, "Account number must with validation code")
})
})

describe('#isPatientValid()', () => {
const patient = {
id: 3,
firstname: 'Gordan',
lastname: 'Aery',
street: '92320 Glacier Hill Terrace',
Expand All @@ -210,7 +225,6 @@ describe('utils', () => {
assert.equal(isPatientValid(patient), true)
assert.equal(isPatientValid({ ...patient, gender: 'woman' }), true)
assert.equal(isPatientValid({ ...patient, canton: 'BE' }), true)
assert.equal(isPatientValid({ ...patient }), true)
})
it('should invalidate an empty object', () => {
assert.equal(isPatientValid({}), false)
Expand Down

0 comments on commit b2cb90b

Please sign in to comment.