Skip to content

King-Austin/card-validator

Repository files navigation

Card Number Validation API & Demo

This project implements a production-grade backend API and a clean web UI for validating credit card numbers using the Luhn algorithm. Built with Node.js, TypeScript, and NestJS.

Features

  • Full-Stack: Built-in interactive demo UI.
  • Luhn Algorithm: Robust checksum validation.
  • Intelligent Response: Detects card types (Visa, Mastercard) and provide human-friendly messages.
  • Production Ready: Fully Dockerized and works on Vercel, Railway, or Render.

Tech Stack

  • Framework: NestJS (Node.js)
  • Frontend: Vanilla HTML/CSS/JS (served via @nestjs/serve-static)
  • Validation: class-validator
  • Testing: Jest (Unit & E2E)

Setup Instructions

  1. Install dependencies:

    npm install
  2. Run the application locally:

    npm run start:dev
  3. Run tests:

    npm test

API Usage

POST /validate-card

Validates a given credit card number.

Request Body:

{
  "cardNumber": "4111 1111 1111 1111"
}

Response:

{
  "isValid": true,
  "cardType": "Visa",
  "message": "This looks like a valid Visa card."
}

Deployment

Docker

The project includes a multi-stage Dockerfile optimized for production.

docker build -t card-validator .
docker run -p 3001:3001 card-validator

Vercel

Configuration is provided via vercel.json. Simply run vercel --prod.

Example Request (using curl):

curl -X POST http://localhost:3000/validate-card \
-H "Content-Type: application/json" \
-d 

Example Valid Response:

{
  "valid": true,
  "type": "Visa"
}

Example Invalid Response (Luhn algorithm fails):

{
  "valid": false,
  "type": "Unknown"
}

Example Error Response (Invalid input):

{
  "statusCode": 400,
  "message": [
    "Card number must be between 12 and 19 characters"
  ],
  "error": "Bad Request"
}

Luhn Algorithm Explanation

The Luhn algorithm (also known as the Mod 10 algorithm) is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, and Canadian Social Insurance Numbers. It is not intended to be a cryptographically secure hash function; it was designed to protect against accidental errors, not malicious attacks.

The algorithm works as follows:

  1. Starting from the rightmost digit, and moving left, double the value of every second digit.
  2. If the result of this doubling is greater than 9 (e.g., 7 doubled is 14), subtract 9 from the result (e.g., 14 becomes 14 - 9 = 5).
  3. Sum all the digits (including those that were not doubled and those that had 9 subtracted).
  4. If the total sum is a multiple of 10 (i.e., sum % 10 === 0), then the number is considered valid.

Design Decisions

  • NestJS Framework: Chosen for its robust, modular structure, and excellent TypeScript support, which aligns with the requirement for a production-grade backend.
  • Clean Modular Structure: The project adheres to the standard NestJS architecture, separating concerns into Controller (HTTP layer), Service (business logic), and DTO (data transfer objects for validation). This promotes maintainability and scalability.
  • class-validator for DTO Validation: Provides a declarative and powerful way to validate incoming request bodies, ensuring that input adheres to specified constraints (string, digits only, length between 12-19 characters). This is integrated globally using NestJS's ValidationPipe.
  • Global Validation Pipe: Configured in main.ts to automatically validate all incoming requests against their respective DTOs. whitelist: true and forbidNonWhitelisted: true are used to prevent unexpected fields in the request body, enhancing security and data integrity.
  • Luhn Algorithm Implementation: Encapsulated within the AppService to keep business logic separate from the HTTP layer. The implementation is straightforward, following the algorithm's steps precisely.
  • Card Type Detection: An optional feature implemented cleanly within the AppService to identify Visa and Mastercard based on their starting digits. This adds value without overengineering.
  • Error Handling: Invalid input is handled gracefully by the global ValidationPipe, returning a 400 Bad Request with clear error messages. Internal errors would be handled by NestJS's default exception filters or custom ones if needed for more complex scenarios.
  • Unit Testing with Jest: Comprehensive unit tests are provided for both the AppService (Luhn algorithm and card type detection) and AppController (endpoint integration), ensuring the core logic functions as expected.
  • tsconfig.json strict: true: Enforced to leverage TypeScript's full type-checking capabilities, leading to more robust and error-free code.

Commit Structure

This project follows a conventional commit structure, simulating the following:

  • feat: initial project setup
  • feat: implement validation endpoint
  • feat: add Luhn algorithm logic
  • test: add unit tests
  • docs: add README

About

This project implements a production-grade backend API for validating credit card numbers using the Luhn algorithm. The API is built with Node.js, TypeScript, and the NestJS framework, adhering to clean architecture principles.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors