Add ID number field with validators to US and HK

This commit is contained in:
Jenni Nurmi 2018-12-10 11:27:43 +02:00
parent 0cc6cf9663
commit 0035dae54b
2 changed files with 32 additions and 1 deletions

View file

@ -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;

View file

@ -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();
});
});
});