-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* luhn constraint * luhn constraint
- Loading branch information
Showing
7 changed files
with
160 additions
and
732 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Luhn | ||
This constraint is used to ensure that a credit card number passes the [Luhn algorithm](https://en.wikipedia.org/wiki/Luhn_algorithm). It is useful as a first step to validating a credit card: before communicating with a payment gateway. | ||
|
||
## Options | ||
|
||
##### message | ||
type: `string` default: `Invalid card number.` | ||
The default message supplied when the value does not pass the Luhn check. | ||
|
||
| Parameter | Description | | ||
|---|---| | ||
| {{ value }} | The current (invalid) value | ||
|
||
##### trim | ||
type: `boolean` default: `true` | ||
Trim provided value. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import AbstractConstraint from './AbstractConstraint'; | ||
import { trim } from '../Utils/functions'; | ||
|
||
const MESSAGE_INVALID = 'Invalid card number.'; | ||
|
||
export default class Luhn extends AbstractConstraint { | ||
/** | ||
* @param {{message: string, trim: boolean}} [options] | ||
*/ | ||
constructor(options) { | ||
super(options); | ||
} | ||
|
||
/** | ||
* @return {{message: string, trim: boolean}} | ||
*/ | ||
getDefaultOptions() { | ||
return { | ||
'trim': true, | ||
'message': MESSAGE_INVALID, | ||
}; | ||
} | ||
|
||
/** | ||
* Test provided value and return Error if occurs | ||
* | ||
* @param value | ||
* | ||
* @return {Error|undefined} | ||
*/ | ||
validate(value) { | ||
if (this.options.trim) { | ||
value = trim(value); | ||
} | ||
|
||
if (this.isEmptyValue(value)) { | ||
return; | ||
} | ||
|
||
if (/[^0-9-\s]+/.test(value)) { | ||
return this | ||
.getViolationBuilder() | ||
.setParameter('value', value) | ||
.build(this.options.message); | ||
} | ||
|
||
let canonical = value.toString().replace(/\D/g, ''); | ||
let checkSum = 0; | ||
|
||
for (let i = canonical.length - 1; i >= 0; i -= 2) { | ||
checkSum += parseInt(canonical.charAt(i)); | ||
} | ||
|
||
for (let i = canonical.length - 2; i >= 0; i -= 2) { | ||
// checkSum += array_sum(str_split((int) $value[$i] * 2)); | ||
checkSum += (parseInt(canonical.charAt(i)) * 2) | ||
.toString() | ||
.split('') | ||
.reduce((a, b) => { | ||
return parseInt(a) + parseInt(b); | ||
}, 0); | ||
} | ||
|
||
if (0 === checkSum || 0 !== checkSum % 10) { | ||
return this | ||
.getViolationBuilder() | ||
.setParameter('value', value) | ||
.build(this.options.message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Luhn } from '../../src/validator'; | ||
|
||
const assert = require('assert'); | ||
|
||
describe('Luhn', function () { | ||
describe('#constructor()', function () { | ||
it('empty configuration', function () { | ||
new Luhn(); | ||
}); | ||
}); | ||
|
||
describe('#validate()', function () { | ||
it('Valid', function () { | ||
const object = new Luhn(); | ||
|
||
[ | ||
'42424242424242424242', | ||
'378282246310005', | ||
'371449635398431', | ||
'378734493671000', | ||
'5610591081018250', | ||
'30569309025904', | ||
'38520000023237', | ||
'6011111111111117', | ||
'6011000990139424', | ||
'3530111333300000', | ||
'3566002020360505', | ||
'5555555555554444', | ||
'3530-1113-3330-0000', | ||
'3566002020360505', | ||
'3566 0020 2036 0505', | ||
'5555555555554444', | ||
'5105105105105100', | ||
'4111111111111111', | ||
'4012888888881881', | ||
'4222222222222', | ||
'5019717010103742', | ||
'6331101999990016', | ||
].forEach((code) => { | ||
const e = object.validate(code); | ||
|
||
assert.ok(typeof e === 'undefined', e); | ||
}); | ||
}); | ||
|
||
it('Invalid', function () { | ||
const object = new Luhn(); | ||
|
||
[ | ||
'foobar', 1, 100, 804, '804', | ||
0, | ||
'0', | ||
'0000000000', | ||
'00', | ||
'1234567812345678', | ||
'4222222222222222', | ||
'0000000000000000', | ||
'42-22222222222222' | ||
] | ||
.forEach((code) => { | ||
const e = object.validate(code); | ||
|
||
assert.strictEqual(e.message, 'Invalid card number.'); | ||
}); | ||
}); | ||
}); | ||
}); |