Merge pull request #19731 from ChanceM/feature/card-validator

Add card-validator definitions
This commit is contained in:
Arthur Ozga
2017-09-14 11:40:14 -07:00
committed by GitHub
4 changed files with 101 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
import valid = require('card-validator');
const numberValidation = valid.number('4111');
if (!numberValidation.isPotentiallyValid && numberValidation.card) {
numberValidation.card.type;
}
valid.expirationDate('10/19');
const dateTest2 = valid.expirationDate({month: '1', year: '2019'});
if (dateTest2.isPotentiallyValid) {
dateTest2.month;
}
const expirationMonthCheck = valid.expirationMonth('10');
if (expirationMonthCheck.isPotentiallyValid) {
expirationMonthCheck.isValidForThisYear;
}
const expirationYearCheck = valid.expirationYear('10');
if (expirationYearCheck.isPotentiallyValid) {
expirationYearCheck.isCurrentYear;
}
let postalCodeCheck = valid.postalCode('123');
if (postalCodeCheck.isValid) {
postalCodeCheck = valid.postalCode('123', {});
postalCodeCheck = valid.postalCode('123', {minLength: 5});
}
const cvvCeck = valid.cvv('1234');
if (cvvCeck.isValid) {
valid.cvv('12345', 4);
}

43
types/card-validator/index.d.ts vendored Normal file
View File

@@ -0,0 +1,43 @@
// Type definitions for card-validator 4.1
// Project: https://github.com/braintree/card-validator
// Definitions by: Gregory Moore <https://github.com/ChanceM>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export interface Card {
niceType: string;
type: string;
pattern: string;
isAmex: boolean;
gaps: number[];
lengths: number[];
code: {name: string, size: number};
}
export interface valid {
isPotentiallyValid: boolean;
isValid: boolean;
}
export interface validNumber extends valid {
card: Card | null;
}
export interface validExpirationDate extends valid {
month: string | null;
year: string | null;
}
export interface validExpirationMonth extends valid {
isValidForThisYear: boolean;
}
export interface validExpirationYear extends valid {
isCurrentYear: boolean;
}
export function number(value: string): validNumber;
export function expirationDate(value: string | {month: string, year: string}): validExpirationDate;
export function expirationMonth(value: string): validExpirationMonth;
export function expirationYear(value: string): validExpirationYear;
export function cvv(value: string, maxLength?: number): valid;
export function postalCode(value: string, options?: {minLength?: number}): valid;

View File

@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"card-validator-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }