Skip to content

Commit

Permalink
Linter + changed error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
liya-zhu committed Nov 29, 2024
1 parent a2c9170 commit d5a7d64
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 184 deletions.
92 changes: 61 additions & 31 deletions backend/typescript/middlewares/validators/activityValidators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response, NextFunction } from "express";
import { getApiValidationError, validateDate, validatePrimitive, } from "./util";
import { getApiValidationError, validateDate, validatePrimitive } from "./util";

/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable-next-line import/prefer-default-export */
Expand All @@ -9,11 +9,12 @@ export const activityRequestDtoValidator = async (
next: NextFunction,
) => {
const { body } = req;
console.log(body)

if (body.userId !== undefined &&
if (
body.userId !== undefined &&
body.userId !== null &&
!validatePrimitive(body.userId, "integer")) {
!validatePrimitive(body.userId, "integer")
) {
return res.status(400).send(getApiValidationError("userId", "integer"));
}

Expand All @@ -22,30 +23,42 @@ export const activityRequestDtoValidator = async (
}

if (!validatePrimitive(body.activityTypeId, "integer")) {
return res.status(400).send(getApiValidationError("activityTypeId", "integer"));
return res
.status(400)
.send(getApiValidationError("activityTypeId", "integer"));
}

if (body.scheduledStartTime !== undefined &&
if (
body.scheduledStartTime !== undefined &&
body.scheduledStartTime !== null &&
!validateDate(body.scheduledStartTime)) {
return res.status(400).send(getApiValidationError("scheduledStartTime", "Date"));
!validateDate(body.scheduledStartTime)
) {
return res
.status(400)
.send(getApiValidationError("scheduledStartTime", "Date"));
}

if (body.startTime !== undefined &&
if (
body.startTime !== undefined &&
body.startTime !== null &&
!validateDate(body.startTime)) {
!validateDate(body.startTime)
) {
return res.status(400).send(getApiValidationError("startTime", "Date"));
}

if (body.endTime !== undefined &&
if (
body.endTime !== undefined &&
body.endTime !== null &&
!validateDate(body.endTime)) {
!validateDate(body.endTime)
) {
return res.status(400).send(getApiValidationError("endTime", "Date"));
}

if (body.notes !== undefined &&
if (
body.notes !== undefined &&
body.notes !== null &&
!validatePrimitive(body.notes, "string")) {
!validatePrimitive(body.notes, "string")
) {
return res.status(400).send(getApiValidationError("notes", "string"));
}

Expand All @@ -58,47 +71,64 @@ export const activityUpdateDtoValidator = async (
next: NextFunction,
) => {
const { body } = req;
console.log(body)

if (body.userId !== undefined &&
if (
body.userId !== undefined &&
body.userId !== null &&
!validatePrimitive(body.userId, "integer")) {
!validatePrimitive(body.userId, "integer")
) {
return res.status(400).send(getApiValidationError("userId", "integer"));
}

if (body.petId !== undefined &&
if (
body.petId !== undefined &&
body.petId !== null &&
!validatePrimitive(body.petId, "integer")) {
!validatePrimitive(body.petId, "integer")
) {
return res.status(400).send(getApiValidationError("petId", "integer"));
}

if (body.activityTypeId !== undefined &&
if (
body.activityTypeId !== undefined &&
body.activityTypeId !== null &&
!validatePrimitive(body.activityTypeId, "integer")) {
return res.status(400).send(getApiValidationError("activityTypeId", "integer"));
!validatePrimitive(body.activityTypeId, "integer")
) {
return res
.status(400)
.send(getApiValidationError("activityTypeId", "integer"));
}

if (body.scheduledStartTime !== undefined &&
if (
body.scheduledStartTime !== undefined &&
body.scheduledStartTime !== null &&
!validateDate(body.scheduledStartTime)) {
return res.status(400).send(getApiValidationError("scheduledStartTime", "Date"));
!validateDate(body.scheduledStartTime)
) {
return res
.status(400)
.send(getApiValidationError("scheduledStartTime", "Date"));
}

if (body.startTime !== undefined &&
if (
body.startTime !== undefined &&
body.startTime !== null &&
!validateDate(body.startTime)) {
!validateDate(body.startTime)
) {
return res.status(400).send(getApiValidationError("startTime", "Date"));
}

if (body.endTime !== undefined &&
if (
body.endTime !== undefined &&
body.endTime !== null &&
!validateDate(body.endTime)) {
!validateDate(body.endTime)
) {
return res.status(400).send(getApiValidationError("endTime", "Date"));
}

if (body.notes !== undefined &&
if (
body.notes !== undefined &&
body.notes !== null &&
!validatePrimitive(body.notes, "string")) {
!validatePrimitive(body.notes, "string")
) {
return res.status(400).send(getApiValidationError("notes", "string"));
}

Expand Down
11 changes: 9 additions & 2 deletions backend/typescript/middlewares/validators/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
type Type = "string" | "integer" | "boolean" | "decimal" | "PetStatus" | "Sex" | "Date";
type Type =
| "string"
| "integer"
| "boolean"
| "decimal"
| "PetStatus"
| "Sex"
| "Date";

const allowableContentTypes = new Set([
"text/plain",
Expand Down Expand Up @@ -51,7 +58,7 @@ export const validateFileType = (mimetype: string): boolean => {
};

export const validateDate = (value: any): boolean => {
return value instanceof Date && !isNaN(value.getTime());
return !Number.isNaN(Date.parse(value));
};

export const getApiValidationError = (
Expand Down
4 changes: 2 additions & 2 deletions backend/typescript/models/activity.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import User from "./user.model";
import Pet from "./pet.model";
import ActivityType from "./activityType.model";

@Table({
@Table({
tableName: "activities",
timestamps: true,
timestamps: true,
createdAt: "created_at",
updatedAt: "updated_at",
})
Expand Down
Loading

0 comments on commit d5a7d64

Please sign in to comment.