A clean and robust API for validating card numbers using the Luhn algorithm, built with NestJS and TypeScript.
- Luhn Algorithm Validation: Accurate validation of credit/debit card numbers.
- Card Type Detection: Basic identification of Visa, MasterCard, Amex, and Discover.
- Swagger Documentation: Interactive API docs available at
/api/docs. - Strict TypeScript: Configured with
strict: truefor maximum type safety. - HNG Standard Response Format: All responses are wrapped in a consistent
{ message, data }structure. - Global Error Handling: Standardized error responses with appropriate HTTP status codes.
- DTO Validation: Input validation using
class-validatorandclass-transformer.
The project follows a modular structure inspired by clean architecture principles:
- Modules: Logic is encapsulated into feature-specific modules (e.g.,
CardValidatorModule). - Service Layer: Orchestrates business logic (Luhn validation).
- Controller Layer: Handles HTTP requests and uses DTOs for input validation.
- Common Layer: Contains cross-cutting concerns like Interceptors and Decorators.
- Interceptors:
ResponseTransformInterceptorensures all responses follow the mandatory HNG SDK format.
- Framework: NestJS (Node.js)
- Language: TypeScript
- Validation:
class-validator - Testing: Jest
- Node.js (v18 or higher)
- npm or yarn
# Clone the repository
git clone <repository-url>
# Navigate to project directory
cd card-validator-api
# Install dependencies
npm install# Development mode
npm run start:dev
# Production mode
npm run start:prod# Unit tests
npm run test- API Root:
http://localhost:3000 - API Version 1:
http://localhost:3000/api/v1 - Interactive Documentation (Swagger):
http://localhost:3000/api/docs
You can use the following numbers to test the API's validation logic:
| Status | Card Type | Card Number |
|---|---|---|
| Valid | Visa | 4242424242424242 |
| Valid | Mastercard | 5105105105105100 |
| Valid | Discover | 6011111111111117 |
| Valid | Amex | 378282246310005 |
| Invalid | Any | 4242424242424241 (Fails Luhn) |
| Invalid | Too Short | 12345678901 (Less than 12 digits) |
Endpoint: POST /api/v1/validate-card
Request Body:
{
"card_number": "799273987130"
}Success Response (Valid Card):
- Status Code: 201 Created
{
"message": "Card number is valid",
"data": {
"is_valid": true,
"card_type": "unknown"
}
}Success Response (Invalid Card):
- Status Code: 201 Created
{
"message": "Card number is invalid",
"data": {
"is_valid": false,
"card_type": "visa"
}
}Error Response (Bad Request):
- Status Code: 400 Bad Request
{
"statusCode": 400,
"message": [
"Card number must be at least 12 digits and Card number must not exceed 19 digits"
],
"error": "Bad Request"
}- NestJS: Chosen for its robust architectural patterns, dependency injection, and excellent TypeScript support, which aligns with the requirement for
strictmode. - Response Wrapping: Implemented via a Global Interceptor to ensure consistency across the entire API without cluttering controllers.
- Luhn Algorithm: Used as the primary validation logic as it is the industry standard for verifying the checksum of identification numbers.
- Validation Pipes: Used
ValidationPipewithwhitelist: trueto prevent unwanted properties and ensure data integrity.
- Chris
This project is licensed under the MIT License.