Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
32 changes: 32 additions & 0 deletions test/integration/isValidStateCode.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
21 changes: 21 additions & 0 deletions test/unit/isValidStateCode.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
18 changes: 18 additions & 0 deletions validationFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions views/pages/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down