Skip to content

Commit

Permalink
feat: add validation for empty body (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
EricRibeiro authored Aug 25, 2022
1 parent 9866d37 commit 2db6147
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ Using `fetch`:
```javascript
fetch('http://localhost:3000/mortgage', {
method: 'POST',
body: JSON.stringify({
propertyPrice: 600000,
downPayment: 200000,
nominalInterestRate: 4,
amortization: 25,
paymentSchedule: "monthly"
body: JSON.stringify({
propertyPrice: 600000,
downPayment: 200000,
nominalInterestRate: 4,
amortization: 25,
paymentSchedule: "monthly"
}),
headers: {
'Content-type': 'application/json; charset=UTF-8'
Expand Down
3 changes: 2 additions & 1 deletion handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export const main: Handler = (event: any) => {
let response: { statusCode: number; body: any; };

if (event.requestContext.http.method === 'POST') {
const { propertyPrice, downPayment, nominalInterestRate, amortization, paymentSchedule } = JSON.parse(event.body);
const payload = event.body ? JSON.parse(event.body) : {};
const { propertyPrice, downPayment, nominalInterestRate, amortization, paymentSchedule } = payload;
const mortgage = calculate(propertyPrice, downPayment, nominalInterestRate, amortization, paymentSchedule);

response = (typeof mortgage === 'number')
Expand Down
30 changes: 28 additions & 2 deletions src/validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ describe("Tests if an error is returned when the parameter is null, undefined, N
});
});

describe("Tests if the down payment parameter is validated correctly", () => {
describe("Tests if the down payment is validated correctly", () => {
it("should return error if down payment is below the minimum", () => {
const { propertyPrice, nominalInterestRate, amortization, paymentSchedule} = defaultValues;
const { propertyPrice, nominalInterestRate, amortization, paymentSchedule } = defaultValues;
const downPayment = 500;

const errors = validate(propertyPrice, downPayment as any, nominalInterestRate, amortization, paymentSchedule);
Expand All @@ -50,3 +50,29 @@ describe("Tests if the down payment parameter is validated correctly", () => {
expect(errorInfo).toBe(`With a property price of "${propertyPrice}", the down payment must be at least 35000. "${downPayment}" was received instead.`);
});
});

describe("Tests if the payment schedule is validated correctly", () => {
it("should return error if payment schedule is invalid", () => {
const { propertyPrice, downPayment, nominalInterestRate, amortization } = defaultValues;
const paymentSchedule = "weekly";

const errors = validate(propertyPrice, downPayment, nominalInterestRate, amortization, paymentSchedule);
expect(errors.length).toBe(1);

const errorInfo = JSON.parse(errors[0].message).information;
expect(errorInfo).toBe("The paymentSchedule must be one of the following: accelerated-bi-weekly, bi-weekly, monthly. \"weekly\" was received instead.");
});
});

describe("Tests if the amortization is validated correctly", () => {
it("should return error if amortization is outside range", () => {
const { propertyPrice, downPayment, nominalInterestRate, paymentSchedule } = defaultValues;
const amortization = 35;

const errors = validate(propertyPrice, downPayment, nominalInterestRate, amortization, paymentSchedule);
expect(errors.length).toBe(1);

const errorInfo = JSON.parse(errors[0].message).information;
expect(errorInfo).toBe("The amortization must be one of the following: 5, 10, 15, 20, 25, 30 but \"35\" was received instead.");
});
});
10 changes: 5 additions & 5 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function validateAmortization(amortization: number): Error[] {
if (isValid && !amortizations.includes(amortization)) {
const invalidAmortizationError = getError(
"The amortization is invalid.",
`The amortization value must be one of the following: ${amortizations.join(", ")} but "${amortization}" was received instead.`
`The amortization must be one of the following: ${amortizations.join(", ")} but "${amortization}" was received instead.`
)

errors.push(invalidAmortizationError);
Expand All @@ -72,17 +72,17 @@ function validatePaymentSchedule(paymentSchedule: string): Error[] {

if (!isValid) {
const invalidFormatError = getError(
"The payment schedule is invalid.",
`The payment schedule must be a non-null string but "${paymentSchedule}" was received instead.`
"The paymentSchedule is invalid.",
`The paymentSchedule must be a non-null string but "${paymentSchedule}" was received instead.`
)

errors.push(invalidFormatError);
}

if (isValid && !schedules.includes(paymentSchedule)) {
const invalidAmortizationError = getError(
"The payment schedule is invalid.",
`The payment schedule value must be one of the following: ${schedules.join(", ")}`
"The paymentSchedule is invalid.",
`The paymentSchedule must be one of the following: ${schedules.join(", ")}. "${paymentSchedule}" was received instead.`
);

errors.push(invalidAmortizationError);
Expand Down

0 comments on commit 2db6147

Please sign in to comment.