diff --git a/server.js b/server.js index 137dd90..5c25908 100644 --- a/server.js +++ b/server.js @@ -1132,4 +1132,35 @@ app.post('/api/isLatLong', (req, res) => { res.json({ result }); }); +/** + * POST /api/isDigit + * @summary Returns true if the input string contains only digits (0-9), otherwise false + * @description This endpoint validates if the input string consists exclusively of numeric digits. + * @param {BasicRequest} request.body.required - Request body containing the input string + * @return {BasicResponse} 200 - Success response + * @return {BadRequestResponse} 400 - Bad request response + * @example request - test + * { + * "inputString": "123" + * } + * @example response - 200 - example payload + * { + * "result": true + * } + * @example response - 400 - example + * { + * "error": "Input string required as a parameter." + * } + */ +app.post('/api/isDigit', (req, res) => { + const { inputString } = req.body; + + if (!inputString) { + return res.status(400).json({ error: requiredParameterResponse }); + } + + const result = ValidationFunctions.isDigit(inputString); + res.json({ result }); +}); + module.exports = app; \ No newline at end of file diff --git a/test/integration/isDigit.test.js b/test/integration/isDigit.test.js new file mode 100644 index 0000000..7eabdde --- /dev/null +++ b/test/integration/isDigit.test.js @@ -0,0 +1,123 @@ +const request = require('supertest'); +const app = require('../../server'); + +describe('POST /api/isDigit', () => { + it('should return true for valid digit strings', async () => { + const response = await request(app) + .post('/api/isDigit') + .send({ inputString: '123' }) + .expect(200); + + expect(response.body.result).toBe(true); + }); + + it('should return true for single digit', async () => { + const response = await request(app) + .post('/api/isDigit') + .send({ inputString: '0' }) + .expect(200); + + expect(response.body.result).toBe(true); + }); + + it('should return true for multiple digits with leading zeros', async () => { + const response = await request(app) + .post('/api/isDigit') + .send({ inputString: '000123' }) + .expect(200); + + expect(response.body.result).toBe(true); + }); + + it('should return false for strings containing letters', async () => { + const response = await request(app) + .post('/api/isDigit') + .send({ inputString: '12a3' }) + .expect(200); + + expect(response.body.result).toBe(false); + }); + + it('should return false for strings containing special characters', async () => { + const response = await request(app) + .post('/api/isDigit') + .send({ inputString: '12.3' }) + .expect(200); + + expect(response.body.result).toBe(false); + }); + + it('should return true for strings with leading/trailing whitespace', async () => { + const response = await request(app) + .post('/api/isDigit') + .send({ inputString: ' 123 ' }) + .expect(200); + + expect(response.body.result).toBe(true); + }); + + it('should return false for strings with internal spaces', async () => { + const response = await request(app) + .post('/api/isDigit') + .send({ inputString: '12 3' }) + .expect(200); + + expect(response.body.result).toBe(false); + }); + + it('should return 400 for empty strings', async () => { + const response = await request(app) + .post('/api/isDigit') + .send({ inputString: '' }) + .expect(400); + + expect(response.body.error).toBe('Input string required as a parameter.'); + }); + + + it('should return false for negative numbers', async () => { + const response = await request(app) + .post('/api/isDigit') + .send({ inputString: '-123' }) + .expect(200); + + expect(response.body.result).toBe(false); + }); + + it('should return false for decimal numbers', async () => { + const response = await request(app) + .post('/api/isDigit') + .send({ inputString: '12.34' }) + .expect(200); + + expect(response.body.result).toBe(false); + }); + + it('should return 400 for missing inputString parameter', async () => { + const response = await request(app) + .post('/api/isDigit') + .send({}) + .expect(400); + + expect(response.body.error).toBe('Input string required as a parameter.'); + }); + + it('should return 400 for null inputString parameter', async () => { + const response = await request(app) + .post('/api/isDigit') + .send({ inputString: null }) + .expect(400); + + expect(response.body.error).toBe('Input string required as a parameter.'); + }); + + it('should return 400 for undefined inputString parameter', async () => { + const response = await request(app) + .post('/api/isDigit') + .send({ inputString: undefined }) + .expect(400); + + expect(response.body.error).toBe('Input string required as a parameter.'); + }); + +}); diff --git a/test/unit/isDigit.test.js b/test/unit/isDigit.test.js new file mode 100644 index 0000000..d605939 --- /dev/null +++ b/test/unit/isDigit.test.js @@ -0,0 +1,71 @@ +const { isDigit } = require('../../validationFunctions'); + +describe('isDigit', () => { + it('should return true for strings containing only digits', () => { + expect(isDigit('123')).toBe(true); + expect(isDigit('0')).toBe(true); + expect(isDigit('123456789')).toBe(true); + expect(isDigit('000123')).toBe(true); + }); + + it('should return false for strings containing letters', () => { + expect(isDigit('12a3')).toBe(false); + expect(isDigit('abc')).toBe(false); + expect(isDigit('123abc')).toBe(false); + expect(isDigit('abc123')).toBe(false); + }); + + it('should return false for strings containing special characters', () => { + expect(isDigit('12.3')).toBe(false); + expect(isDigit('12-3')).toBe(false); + expect(isDigit('12_3')).toBe(false); + expect(isDigit('12@3')).toBe(false); + expect(isDigit('12#3')).toBe(false); + }); + + it('should allow leading/trailing spaces but fail on internal spaces', () => { + expect(isDigit(' 123')).toBe(true); + expect(isDigit('123 ')).toBe(true); + expect(isDigit(' 123 ')).toBe(true); + expect(isDigit('12 3')).toBe(false); + }); + + it('should return false for empty strings', () => { + expect(isDigit('')).toBe(false); + }); + + it('should return false for null and undefined', () => { + expect(isDigit(null)).toBe(false); + expect(isDigit(undefined)).toBe(false); + }); + + it('should return false for non-string inputs', () => { + expect(isDigit(true)).toBe(false); + expect(isDigit(false)).toBe(false); + expect(isDigit({})).toBe(false); + expect(isDigit([])).toBe(false); + }); + + it('should return true for strings with leading and trailing whitespace', () => { + expect(isDigit(' 123 ')).toBe(true); + expect(isDigit('\t123\n')).toBe(true); + expect(isDigit('\r123\f')).toBe(true); + }); + + it('should return false for decimal numbers', () => { + expect(isDigit('12.34')).toBe(false); + expect(isDigit('.123')).toBe(false); + expect(isDigit('123.')).toBe(false); + }); + + it('should return false for negative numbers', () => { + expect(isDigit('-123')).toBe(false); + expect(isDigit('-0')).toBe(false); + }); + + it('should return false for hexadecimal strings', () => { + expect(isDigit('1A2B3C')).toBe(false); + expect(isDigit('0xFF')).toBe(false); + expect(isDigit('1a2b3c')).toBe(false); + }); +}); diff --git a/validationFunctions.js b/validationFunctions.js index 94157ad..33aa1d1 100644 --- a/validationFunctions.js +++ b/validationFunctions.js @@ -476,6 +476,31 @@ module.exports = class ValidationFunctions { const decimalDegreesRegex = /^-?\d{1,3}(?:\.\d+)?,\s*-?\d{1,3}(?:\.\d+)?$/; return decimalDegreesRegex.test(trimmedInput); } + + /** + * Checks if the given string contains only digit characters (0-9). + * + * This function validates that the input string consists exclusively of numeric digits. + * It returns false for empty strings, null, undefined, or strings containing any non-digit characters. + * + * @param {string} inputString - The string to validate. + * @returns {boolean} - Returns `true` if `inputString` contains only digits, otherwise `false`. + * + * @example + * isDigit("123"); // Returns: true + * isDigit("0"); // Returns: true + * isDigit("12a3"); // Returns: false + * isDigit("12.3"); // Returns: false + * isDigit(""); // Returns: false + * isDigit("abc"); // Returns: false + */ + static isDigit(inputString) { + if (!inputString || typeof inputString !== "string") return false; + + const trimmedInput = inputString.trim(); + const digitRegex = /^[0-9]+$/; + return digitRegex.test(trimmedInput); + } } const handleAxiosError = (error) => {