diff --git a/server.js b/server.js index fdb4945..627a3a6 100644 --- a/server.js +++ b/server.js @@ -1055,4 +1055,35 @@ app.post('/api/isCountry', async (req, res) => { } }); +/** + * POST /api/isValidStateCode + * @summary Returns true if valid US state code, otherwise false + * @param {BasicRequest} request.body.required - The state code to validate + * @return {BasicResponse} 200 - Success response + * @return {BadRequestResponse} 400 - Bad request response + * @example request - test + * { + * "inputString": "LA" + * } + * @example response - 200 - example payload + * { + * "result": true + * } + * @example response - 400 - example + * { + * "error": "Input string required as a parameter." + * } + */ +app.post('/api/isValidStateCode', (req, res) => { + const { inputString } = req.body; + + if (!inputString) { + return res.status(400).json({ error: requiredParameterResponse }); + } + + const result = ValidationFunctions.isValidStateCode(inputString); + + res.json({ result }); +}); + module.exports = app; diff --git a/test/integration/isValidStateCode.test.js b/test/integration/isValidStateCode.test.js new file mode 100644 index 0000000..5574411 --- /dev/null +++ b/test/integration/isValidStateCode.test.js @@ -0,0 +1,32 @@ +const request = require('supertest'); +const app = require('../../server.js'); + +describe('POST /api/isValidStateCode', () => { + it('should return true for a valid US state code', async () => { + const response = await request(app) + .post('/api/isValidStateCode') + .send({ inputString: 'LA' }) + .expect(200); + + expect(response.body).toHaveProperty('result', true); + }); + + it('should return false for an invalid US state code', async () => { + const response = await request(app) + .post('/api/isValidStateCode') + .send({ inputString: 'LAX' }) + .expect(200); + + expect(response.body).toHaveProperty('result', false); + }); + + it('should return 400 if inputString is missing', async () => { + const response = await request(app) + .post('/api/isValidStateCode') + .send({}) + .expect(400); + + expect(response.body).toHaveProperty('error'); + expect(response.body.error).toBeDefined(); + }); +}); diff --git a/test/unit/isValidStateCode.test.js b/test/unit/isValidStateCode.test.js new file mode 100644 index 0000000..79db67f --- /dev/null +++ b/test/unit/isValidStateCode.test.js @@ -0,0 +1,21 @@ +const { isValidStateCode } = require('../../validationFunctions'); + +describe('isValidStateCode', () => { + it('should return true for valid US state codes', () => { + // Valid US state codes + expect(isValidStateCode('LA')).toBe(true); + expect(isValidStateCode('NY')).toBe(true); + expect(isValidStateCode('FL')).toBe(true); + }); + + it('should return false for invalid US state codes', () => { + // Invalid US state codes + expect(isValidStateCode('LAX')).toBe(false); + expect(isValidStateCode('NY1')).toBe(false); + expect(isValidStateCode('FL?')).toBe(false); + }); + + it('should return false for empty strings', () => { + expect(isValidStateCode('')).toBe(false); + }); +}); diff --git a/validationFunctions.js b/validationFunctions.js index 0876a3b..2e1f577 100644 --- a/validationFunctions.js +++ b/validationFunctions.js @@ -432,6 +432,24 @@ module.exports = class ValidationFunctions { throw new Error(error_details); } } + + /** + * Checks if the provided string is a valid US state code. + * + * @param {string} inputString - The state code to validate. + * @return {boolean} - Returns `true` if `inputString` is a valid US state code, otherwise `false`. + */ + static isValidStateCode(inputString) { + const validStateCodes = [ + "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", + "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", + "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", + "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", + "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" + ]; + + return validStateCodes.includes(inputString); + } } const handleAxiosError = (error) => { diff --git a/views/pages/index.pug b/views/pages/index.pug index 2b36629..264175f 100644 --- a/views/pages/index.pug +++ b/views/pages/index.pug @@ -29,6 +29,7 @@ block content option(value='isBinaryString') Is Binary String option(value='isBoolean') Is Boolean option(value='isCountry') Is Country + option(value='isValidStateCode') Is Valid State Code br br