Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to using zod for validation on voting API endpoints #1476

Merged
merged 8 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions common/models/rest-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ServerMessageEvent } from "./messages";
import { BehaviorOption, QueueMode, RoomSettings, RoomUserInfo, Visibility } from "./types";
import { QueueItem, Video, VideoId } from "./video";
import type { Category } from "sponsorblock-api";
import { createRoomSchema } from "./zod-schemas";
import { createRoomSchema, voteSchema } from "./zod-schemas";
import { z } from "zod";

export type OttResponseBody<T = unknown, E extends OttApiError = OttApiError> =
Expand Down Expand Up @@ -83,7 +83,7 @@ export type OttApiResponseAddPreview = {
result: Video[];
};

export interface OttApiRequestVote extends VideoId {}
export type OttApiRequestVote = z.infer<typeof voteSchema>;

export type OttApiRequestAccountRecoveryStart = {
email?: string;
Expand Down
11 changes: 9 additions & 2 deletions common/models/zod-schemas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ROOM_NAME_REGEX } from "ott-common/constants";
import { ALL_VIDEO_SERVICES, ROOM_NAME_REGEX } from "ott-common/constants";
import { Visibility, QueueMode } from "ott-common/models/types";
import { z } from "zod";
import { VideoService } from "./video";
import { string, z } from "zod";

Check warning on line 4 in common/models/zod-schemas.ts

View check run for this annotation

Codecov / codecov/patch

common/models/zod-schemas.ts#L3-L4

Added lines #L3 - L4 were not covered by tests

// These strings are not allowed to be used as room names.
const RESERVED_ROOM_NAMES = ["list", "create", "generate"];
Expand All @@ -18,3 +19,9 @@
visibility: z.nativeEnum(Visibility).default(Visibility.Public).optional(),
queueMode: z.nativeEnum(QueueMode).optional(),
});

export const voteSchema = z.object({
service: z.enum(ALL_VIDEO_SERVICES),
id: z.string(),
Victor-M-Giraldo marked this conversation as resolved.
Show resolved Hide resolved
token: z.string(),
});

Check warning on line 27 in common/models/zod-schemas.ts

View check run for this annotation

Codecov / codecov/patch

common/models/zod-schemas.ts#L22-L27

Added lines #L22 - L27 were not covered by tests
27 changes: 5 additions & 22 deletions server/api/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import { v4 as uuidv4 } from "uuid";
import { counterHttpErrors } from "../metrics";
import { conf } from "../ott-config";
import { createRoomSchema } from "ott-common/models/zod-schemas";
import { createRoomSchema, voteSchema } from "ott-common/models/zod-schemas";
import { ZodError } from "zod";
import { fromZodError } from "zod-validation-error";

Expand Down Expand Up @@ -304,17 +304,9 @@
};

const addVote: RequestHandler<{ name: string }, unknown, OttApiRequestVote> = async (req, res) => {
if (!req.token) {
Victor-M-Giraldo marked this conversation as resolved.
Show resolved Hide resolved
throw new OttException("Missing token");
}
if (!req.body.service) {
throw new BadApiArgumentException("service", "missing");
}
if (!req.body.id) {
throw new BadApiArgumentException("id", "missing");
}
const body = voteSchema.parse(req.body);

Check warning on line 307 in server/api/room.ts

View check run for this annotation

Codecov / codecov/patch

server/api/room.ts#L307

Added line #L307 was not covered by tests

const client = clientmanager.getClientByToken(req.token, req.params.name);
const client = clientmanager.getClientByToken(body.token, req.params.name);

Check warning on line 309 in server/api/room.ts

View check run for this annotation

Codecov / codecov/patch

server/api/room.ts#L309

Added line #L309 was not covered by tests
await clientmanager.makeRoomRequest(client, {
type: RoomRequestType.VoteRequest,
video: { service: req.body.service, id: req.body.id },
Expand All @@ -329,17 +321,8 @@
req,
res
) => {
if (!req.token) {
throw new OttException("Missing token");
}
if (!req.body.service) {
throw new BadApiArgumentException("service", "missing");
}
if (!req.body.id) {
throw new BadApiArgumentException("id", "missing");
}

const client = clientmanager.getClientByToken(req.token, req.params.name);
const body = voteSchema.parse(req.body);
const client = clientmanager.getClientByToken(body.token, req.params.name);

Check warning on line 325 in server/api/room.ts

View check run for this annotation

Codecov / codecov/patch

server/api/room.ts#L324-L325

Added lines #L324 - L325 were not covered by tests
await clientmanager.makeRoomRequest(client, {
type: RoomRequestType.VoteRequest,
video: { service: req.body.service, id: req.body.id },
Expand Down
Loading