Skip to content

Commit

Permalink
POS-1253 Create new validator for both Canadian and U.S. routing numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
citybasecnocon authored and mattr-cb committed Dec 9, 2024
1 parent 2fdfb2e commit c19b85c
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
yarn 1.22.19
yarn 1.22.19
nodejs 20.17.0
80 changes: 80 additions & 0 deletions __tests__/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ import {
isRoutingNumber,
IS_ROUTING_NUMBER,
IS_ROUTING_NUMBER_ERROR,
isAmericanOrCanadianRoutingNumber,
IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER,
IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER_ERROR,
validateWhen,
VALIDATE_WHEN,
VALIDATE_WHEN_ERROR,
Expand Down Expand Up @@ -159,6 +162,18 @@ test('isRoutingNumber validator produces correct validator object', () => {
});
});

test('isAmericanOrCanadianRoutingNumber validator produces correct validator object', () => {
expect(
isAmericanOrCanadianRoutingNumber.error).toBe(
IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER_ERROR
);
expect(isAmericanOrCanadianRoutingNumber()).toEqual({
type: IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER,
args: [],
error: IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER_ERROR,
});
});

test('numberGreaterThan validator produces correct validator object', () => {
expect(numberGreaterThan.error).toBe(NUMBER_GREATER_THAN_ERROR);
expect(numberGreaterThan('0')).toEqual({
Expand Down Expand Up @@ -953,6 +968,71 @@ test('isRoutingNumber validated on empty string', () => {
expect(validatorFns[IS_ROUTING_NUMBER]('', [], {})).toBe(true);
});

/**
* isAmericanOrCanadianRoutingNumber Validator
*/
test.prop(
'isAmericanOrCanadianRoutingNumber validates 8 digit Canadian routing numbers',
[
fc.integer(0, 9),
fc.integer(0, 9),
fc.integer(0, 9),
fc.integer(0, 9),
fc.integer(0, 9),
fc.integer(0, 9),
fc.integer(0, 9),
fc.integer(0, 9),
],
(...first8Digits) => {
const validRoutingNumber = first8Digits.join('');
expect(
validatorFns[IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER](
validRoutingNumber,
[],
{}
)
).toBe(true);
}
);

test.prop(
'isAmericanOrCanadianRoutingNumber validates 9 digit U.S. routing numbers with a valid checksum',
[
fc.integer(1, 9),
fc.integer(0, 9),
fc.integer(0, 9),
fc.integer(0, 9),
fc.integer(0, 9),
fc.integer(0, 9),
fc.integer(0, 9),
fc.integer(0, 9),
],
(...first8Digits) => {
const validRoutingNumber = `${first8Digits.join('')}${calcCheckSum(
...first8Digits
)}`;
expect(
validatorFns[IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER](
validRoutingNumber,
[],
{}
)
).toBe(true);
}
);

test('isAmericanOrCanadianRoutingNumber does not validate 9 digit U.S. routing numbers without a valid checksum', () => {
expect(validatorFns[IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER]('123456789', [], {})).toBe(false);
});

test('isAmericanOrCanadianRoutingNumber does not validate strings of lengths less than 8', () => {
expect(validatorFns[IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER]('1234567', [], {})).toBe(false);
});

test('isAmericanOrCanadianRoutingNumber does not validate strings of lengths greater than 9', () => {
expect(validatorFns[IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER]('1234567891', [], {})).toBe(false);
});

test('runValidator returns null when validator accepts', () => {
expect(runValidator({ type: REQUIRED, args: [] }, 'foo', {})).toBe(null);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-freeform",
"version": "6.1.0",
"version": "6.1.1",
"description": "A small Redux form library that supports purely functional apps, without magic",
"main": "dist/redux-freeform.cjs.js",
"module": "dist/redux-freeform.esm.js",
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
hasLength,
matchesRegex,
isRoutingNumber,
isAmericanOrCanadianRoutingNumber,
validateWhen,
validateSum,
numberGreaterThan,
Expand Down
22 changes: 22 additions & 0 deletions src/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,28 @@ validatorFns[MATCHES_REGEX] = (value, args, form) => {
return new RegExp(args[0]).test(value); // new RexExp never throws an error, no matter the input
};

export const IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER =
'validator/IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER';
export const IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER_ERROR =
'validator/IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER_ERROR';
export const isAmericanOrCanadianRoutingNumber = createValidator(
IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER,
IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER_ERROR
);
validatorFns[IS_AMERICAN_OR_CANADIAN_ROUTING_NUMBER] = (value, args, form) => {
if (value === '') {
return true;
} else if (value.length === 8) {
const regExp = /^\d{8}$/;
return regExp.test(value);
} else if (value.length === 9) {
const regExp = /^\d{9}$/;
return validatorFns[IS_ROUTING_NUMBER](value, args, form) && regExp.test(value);
} else {
return false;
}
};

// based on http://www.brainjar.com/js/validation/
export const IS_ROUTING_NUMBER = 'validator/IS_ROUTING_NUMBER';
export const IS_ROUTING_NUMBER_ERROR = 'error/IS_ROUTING_NUMBER';
Expand Down

0 comments on commit c19b85c

Please sign in to comment.