Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POS-1253 Allow Canadian routing numbers #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
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
Copy link

Choose a reason for hiding this comment

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

I added this, it matches the dockerfile

85 changes: 85 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,17 @@ 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 +967,77 @@ 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
Loading