From 0035dae54b76c18c387e8f7ae2eb36da6e59fdbe Mon Sep 17 00:00:00 2001 From: Jenni Nurmi Date: Mon, 10 Dec 2018 11:27:43 +0200 Subject: [PATCH] Add ID number field with validators to US and HK --- src/util/validators.js | 3 ++- src/util/validators.test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/util/validators.js b/src/util/validators.js index 2c6ecffa..d8240dcc 100644 --- a/src/util/validators.js +++ b/src/util/validators.js @@ -194,7 +194,8 @@ export const validHKID = message => value => { // Verify the check digit const remaining = checkSum % 11; - const verify = remaining === 0 ? 0 : 11 - remaining; + let verify = remaining === 0 ? 0 : 11 - remaining; + verify = verify.toString(); const isValid = verify === checkDigit || (verify === 10 && checkDigit === 'A'); return isValid ? VALID : message; diff --git a/src/util/validators.test.js b/src/util/validators.test.js index 56c286bd..08e8d23a 100644 --- a/src/util/validators.test.js +++ b/src/util/validators.test.js @@ -6,6 +6,7 @@ import { maxLength, moneySubUnitAmountAtLeast, composeValidators, + validHKID, } from './validators'; const { Money } = sdkTypes; @@ -184,4 +185,33 @@ describe('validators', () => { expect(validateLength('')).toEqual('minLength'); }); }); + describe('validHKID()', () => { + it('should fail on too short HKID', () => { + expect(validHKID('fail')('AB987')).toEqual('fail'); + }); + it('should fail if value is in wrong format: no letters', () => { + expect(validHKID('fail')('13278240')).toEqual('fail'); + }); + it('should fail if value is in wrong format: too many letters', () => { + expect(validHKID('fail')('ABE9876543')).toEqual('fail'); + }); + it('should fail if value is in wrong format: too many numbers', () => { + expect(validHKID('fail')('E13278240')).toEqual('fail'); + }); + it('should fail if check digit is wrong', () => { + expect(validHKID('fail')('E327824(3)')).toEqual('fail'); + }); + it('should allow string of nine digits with all zeros', () => { + expect(validHKID('pass')('000000000')).toBeUndefined(); + }); + it('should allow string with valid HKID using two letters', () => { + expect(validHKID('pass')('AB9876543')).toBeUndefined(); + }); + it('should allow string with valid HKID using one letter', () => { + expect(validHKID('pass')('E3278240')).toBeUndefined(); + }); + it('should allow string with valid HKID using brackets in check digit', () => { + expect(validHKID('pass')('E327824(0)')).toBeUndefined(); + }); + }); });