Skip to content

Processor663/Card-number-validation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Card Number Validation API

A REST API endpoint that validates credit/debit card numbers using the Luhn algorithm (mod 10).

Features

  • Card Number Validation: Validates card numbers using the Luhn algorithm
  • Card Type Detection: Identifies card types (Visa, Mastercard, American Express, Discover)
  • Input Sanitization: Handles card numbers with spaces and dashes
  • Input Validation: Proper error handling for invalid inputs

Technology Stack

  • Node.js with TypeScript
  • Express.js
  • Jest for testing

Project Structure

card-number-validation-api/
├── src/
│   ├── index.ts           # Express app and API endpoint
│   ├── cardValidator.ts   # Card validation logic (Luhn algorithm)
│   ├── cardValidator.test.ts # Unit tests
│   └── types.ts           # TypeScript interfaces
├── package.json
├── tsconfig.json
├── jest.config.js
└── README.md

Installation

  1. Install dependencies:
npm install

Running the Project

Development Mode

npm run dev

The server will start on http://localhost:3000

Production Mode

npm run build
npm start

Running Tests

npm test

API Endpoint

POST /api/validate-card

Validates a card number and returns the validation result.

Request

{
  "cardNumber": "4111111111111111"
}

Success Response (200)

{
  "valid": true,
  "cardNumber": "4111111111111111",
  "message": "Valid card number",
  "cardType": "Visa"
}

Invalid Card Response (422)

{
  "valid": false,
  "cardNumber": "4111111111111112",
  "message": "Invalid card number (failed Luhn check)",
  "cardType": "Visa"
}

Bad Request Response (400)

{
  "error": "Bad Request",
  "message": "Card number is required"
}

Design Decisions

Validation Algorithm

The API uses the Luhn algorithm (also known as mod 10) to validate card numbers. This is the industry-standard algorithm used by most payment processors to verify card numbers before processing.

Input Handling

  • Card numbers can be submitted with or without spaces (e.g., 4111 1111 1111 1111)
  • Card numbers can include dashes (e.g., 4111-1111-1111-1111)
  • The API automatically sanitizes input before validation

HTTP Status Codes

  • 200 OK: Card number is valid
  • 400 Bad Request: Missing or invalid input (e.g., missing card number field)
  • 422 Unprocessable Entity: Card number is present but invalid (fails validation)
  • 500 Internal Server Error: Unexpected server error

Card Type Detection

The API can identify the following card types based on the card number prefix:

  • Visa: Starts with 4
  • Mastercard: Starts with 51-55 or 2221-2720
  • American Express: Starts with 34 or 37
  • Discover: Starts with 6011, 644-649, or 65

Length Validation

Card numbers must be between 13 and 19 digits (standard for most card types).

Testing

The project includes unit tests for the CardValidator class covering:

  • Valid card numbers (Visa, Mastercard, American Express)
  • Invalid card numbers (failed Luhn check)
  • Non-digit characters
  • Invalid lengths (too short or too long)
  • Card type detection

Running the Project

Install dependencies

npm install

Build

npm run build

Run tests

npm test

Start development server

npm run dev

About

A REST API endpoint that validates credit/debit card numbers using the Luhn algorithm.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors