card-validator
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -1,9 +0,9 @@ | ||
# 0.2.0 | ||
2.0.0 | ||
===== | ||
- `expirationYear` | ||
- Year maxes out at 19 years in the future | ||
- The returned value for `card.length` is now an `Array` and called `card.lengths` to account for variable-length cards such as UnionPay. | ||
# 0.1.0 | ||
1.0.0 | ||
===== | ||
- `expirationDate` | ||
- Add `month:` and `year:` to return object. Strings if both valid, `null` otherwise. | ||
- Initial release |
{ | ||
"name": "card-validator", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "A library for validating credit card fields", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:braintree/card-validator" | ||
}, | ||
"homepage": "https://github.com/braintree/card-validator", | ||
"scripts": { | ||
@@ -37,3 +42,3 @@ "test": "gulp lint && mocha test/**/*.js", | ||
"lodash.isnumber": "^3.0.1", | ||
"credit-card-type": "^1.0.0", | ||
"credit-card-type": "^2.0.0", | ||
"lodash.assign": "^3.0.0", | ||
@@ -40,0 +45,0 @@ "lodash.isstring": "^3.0.1" |
@@ -1,4 +0,4 @@ | ||
# Credit Card Validator | ||
# Credit Card Validator [![Build Status](https://travis-ci.org/braintree/card-validator.svg)](https://travis-ci.org/braintree/card-validator) [![npm version](https://badge.fury.io/js/card-validator.svg)](http://badge.fury.io/js/card-validator) | ||
Credit Card Validator provides validation utilities for credit card data inputs. It is designed as a CommonJS module for use in Node, io.js, or the [browser](http://browserify.org/). It includes first class support for 'potential' validity so you can use it to present appropriate UI to your user as they type. | ||
Credit Card Validator provides validation utilities for credit card data inputs. It is designed as a CommonJS module for use in Node.js, io.js, or the [browser](http://browserify.org/). It includes first class support for 'potential' validity so you can use it to present appropriate UI to your user as they type. | ||
@@ -41,3 +41,3 @@ A typical use case in a credit card form is to notify the user if the data they are entering is invalid. In a credit card field, entering “411” is not necessarily valid for submission, but it is still potentially valid. Conversely, if a user enters “41x” that value can no longer pass strict validation and you can provide a response immediately. | ||
gaps: [4, 10], | ||
length: 15, | ||
lengths: [15], | ||
code: {name: 'CID', size: 4} | ||
@@ -250,4 +250,14 @@ }, | ||
- The maximum expiration year is 19 years from now. ([source](src/expiration-year.js)) | ||
- The maximum expiration year is 19 years from now. ([view in source](src/expiration-year.js)) | ||
- `valid.expirationDate` will only return `month:` and `year:` as strings if the two are valid, otherwise they will be `null`. | ||
## Development | ||
We use `nvm` for managing our node versions, but you do not have to. Replace any `nvm` references with the tool of your choice below. | ||
```sh | ||
nvm install | ||
npm install | ||
``` | ||
All testing dependencies will be installed upon `npm install`. Run the test suite with `npm test`. |
@@ -22,3 +22,3 @@ var isString = require('lodash.isstring'); | ||
function cardNumber(value) { | ||
var cardType; | ||
var cardType, valid, i; | ||
@@ -37,30 +37,26 @@ if (isNumber(value)) { | ||
// TODO: Just letting these go here as validPartials | ||
// The optimal solution would be to have a separate | ||
// set of regexes for partial validations | ||
cardType = getCardType(value); | ||
if (isEmptyObject(cardType)) { return verification(null, false, false); } | ||
// Discover cannot be determined until we have 4 digits | ||
if (value.length <= 3 && value[0] === '6') { | ||
return verification(null, true, false); | ||
if (cardType.type === 'unionpay') { // UnionPay is not Luhn 10 compliant | ||
valid = true; | ||
} else { | ||
valid = luhn10(value); | ||
} | ||
// Non-discover cards | ||
if (value.length <= 1 && /^(5|4|3)/.test(value)) { return verification(null, true, false); } | ||
for (i = 0; i < cardType.lengths.length; i++) { | ||
if (cardType.lengths[i] === value.length) { | ||
return verification(cardType, valid, valid); | ||
} | ||
} | ||
cardType = getCardType(value); | ||
if (isEmptyObject(cardType)) { return verification(null, false, false); } | ||
maxLength = Math.max.apply(null, cardType.lengths); | ||
// Recognized as card but not long enough yet: validPartial | ||
if (value.length < cardType.length) { | ||
if (value.length < maxLength) { | ||
return verification(cardType, true, false); | ||
} | ||
if (value.length > cardType.length) { | ||
return verification(cardType, false, false); | ||
} | ||
var valid = luhn10(value); | ||
return verification(cardType, valid, valid); | ||
return verification(cardType, false, false); | ||
} | ||
module.exports = cardNumber; |
@@ -11,11 +11,11 @@ var expect = require('chai').expect; | ||
['6', | ||
{card: null, isPotentiallyValid: true, isValid: false}], | ||
{card: 'maestro', isPotentiallyValid: true, isValid: false}], | ||
['60', | ||
{card: null, isPotentiallyValid: true, isValid: false}], | ||
{card: 'maestro', isPotentiallyValid: true, isValid: false}], | ||
['601', | ||
{card: null, isPotentiallyValid: true, isValid: false}], | ||
{card: 'maestro', isPotentiallyValid: true, isValid: false}], | ||
['6011', | ||
{card: 'discover', isPotentiallyValid: true, isValid: false}], | ||
['4', | ||
{card: null, isPotentiallyValid: true, isValid: false}], | ||
{card: 'visa', isPotentiallyValid: true, isValid: false}], | ||
['41', | ||
@@ -36,7 +36,19 @@ {card: 'visa', isPotentiallyValid: true, isValid: false}], | ||
{card: null, isPotentiallyValid: false, isValid: false}], | ||
['4012888888881881', // Valid Visa | ||
['4012888888881881', | ||
{card: 'visa', isPotentiallyValid: true, isValid: true}], | ||
['4111111111111111', // Valid Visa | ||
['6288997715452584', | ||
{card: 'unionpay', isPotentiallyValid: true, isValid: true}], | ||
['6282001509099283', | ||
{card: 'unionpay', isPotentiallyValid: true, isValid: true}], | ||
['6269992058134322', | ||
{card: 'unionpay', isPotentiallyValid: true, isValid: true}], | ||
['6240008631401148', | ||
{card: 'unionpay', isPotentiallyValid: true, isValid: true}], | ||
['6221558812340000', // UnionPay is not always Luhn10 valid | ||
{card: 'unionpay', isPotentiallyValid: true, isValid: true}], | ||
['4111111111111111', | ||
{card: 'visa', isPotentiallyValid: true, isValid: true}], | ||
['6011000990139424', // Valid Discover | ||
['4111111111111111', | ||
{card: 'visa', isPotentiallyValid: true, isValid: true}], | ||
['6011000990139424', | ||
{card: 'discover', isPotentiallyValid: true, isValid: true}], | ||
@@ -47,5 +59,3 @@ ['411111y', | ||
{card: 'visa', isPotentiallyValid: false, isValid: false}], | ||
['1111111111111111', // Right length but not luhn | ||
{card: null, isPotentiallyValid: false, isValid: false}], | ||
['4111111111111112', // Visa but no luhn | ||
['4111111111111112', // right lenght, not Luhn | ||
{card: 'visa', isPotentiallyValid: false, isValid: false}], | ||
@@ -52,0 +62,0 @@ ]); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
48325
27
0
262
0
805
+ Addedcredit-card-type@2.0.0(transitive)
- Removedcredit-card-type@1.0.0(transitive)
Updatedcredit-card-type@^2.0.0