Skip to content

Commit

Permalink
chore: create library for year query format (#89)
Browse files Browse the repository at this point in the history
## Description

- Year schema library verifies that the value of year is an integer of
length 4 and returns error statements if not
- In certain use cases it is optional so it remains optional in only
those situations, along with situations where it must be converted to a
number.

## Related Issue

Closes #73 

## Motivation and Context

- Simplifies code making it easier to read.

## How Has This Been Tested?

- Ran locally with comparisons using postman.


## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [ ] My code involves a change to the database schema.
- [ ] My code requires a change to the documentation.
  • Loading branch information
sagardhunna authored Jan 23, 2025
1 parent 23478cd commit a44c1bd
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 29 deletions.
9 changes: 2 additions & 7 deletions apps/api/src/schema/calendar.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { z } from "@hono/zod-openapi";
import { terms } from "@packages/db/schema";
import { yearSchema } from "./lib";

export const calendarQuerySchema = z.object({
year: z
.string({ message: "Parameter 'year' is required" })
.length(4, { message: "Parameter 'year' must have length 4" })
.refine((x) => !Number.isNaN(Number.parseInt(x, 10)), {
message: "Parameter 'year' must be an integer",
})
.openapi({ param: { name: "year", in: "query" }, example: "2024" }),
year: yearSchema.openapi({ param: { name: "year", in: "query" } }),
quarter: z
.enum(terms, {
message:
Expand Down
6 changes: 2 additions & 4 deletions apps/api/src/schema/enrollment-history.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { z } from "@hono/zod-openapi";
import { terms, websocSectionTypes, websocStatuses } from "@packages/db/schema";
import { yearSchema } from "./lib";

export const enrollmentHistoryQuerySchema = z
.object({
year: z
.string()
.regex(/^\d{4}$/, { message: "Invalid year provided" })
.optional(),
year: yearSchema.optional(),
quarter: z.enum(terms, { invalid_type_error: "Invalid quarter provided" }).optional(),
instructorName: z.string().optional(),
department: z.string().optional(),
Expand Down
6 changes: 2 additions & 4 deletions apps/api/src/schema/grades.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from "@hono/zod-openapi";
import { courseLevels, terms } from "@packages/db/schema";
import { yearSchema } from "./lib";

const geCategories = [
"GE-1A",
Expand All @@ -15,10 +16,7 @@ const geCategories = [
] as const;

export const gradesQuerySchema = z.object({
year: z
.string()
.regex(/^\d{4}$/, { message: "Invalid year provided" })
.optional(),
year: yearSchema.optional(),
quarter: z.enum(terms, { invalid_type_error: "Invalid quarter provided" }).optional(),
instructor: z.string().optional(),
department: z.string().optional(),
Expand Down
7 changes: 2 additions & 5 deletions apps/api/src/schema/larc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "@hono/zod-openapi";
import { terms } from "@packages/db/schema";
import { courseNumberSchema, daysSchema, timeSchema } from "./lib";
import { courseNumberSchema, daysSchema, timeSchema, yearSchema } from "./lib";

export const larcQuerySchema = z.object({
instructorName: z
Expand All @@ -18,10 +18,7 @@ export const larcQuerySchema = z.object({
courseNumber: courseNumberSchema
.optional()
.openapi({ description: "The course number(s) of the LARC section's related course." }),
year: z
.string({ message: "Parameter 'year' is required " })
.regex(/^\d{4}$/, { message: "Invalid year provided" })
.openapi({ description: "The year of the LARC section", example: "2024" }),
year: yearSchema,
quarter: z
.enum(terms, {
message:
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/schema/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./time.ts";
export * from "./day.ts";
export * from "./course.ts";
export * from "./year.ts";
15 changes: 15 additions & 0 deletions apps/api/src/schema/lib/year.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from "@hono/zod-openapi";

/**
* Expects a string forming a four-digit positive integer
*/

export const yearSchema = z.coerce
.string()
.refine((val) => val !== ("" || "undefined" || "null"), {
message: "Parameter 'year' is required",
})
.refine((val) => /^\d{4}$/.test(val), {
message: "Year must be a 4-digit positive integer",
})
.openapi({ example: "2024" });
6 changes: 2 additions & 4 deletions apps/api/src/schema/websoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from "@hono/zod-openapi";
import type { FinalExamStatus } from "@packages/db/schema";
import { courseLevels, terms, websocSectionTypes, websocStatuses } from "@packages/db/schema";
import { isBaseTenInt } from "@packages/stdlib";
import { courseNumberSchema, daysSchema, timeSchema } from "./lib";
import { courseNumberSchema, daysSchema, timeSchema, yearSchema } from "./lib";

const anyArray = ["ANY"] as const;

Expand Down Expand Up @@ -73,9 +73,7 @@ const isValidRestrictionCode = (code: string): code is (typeof restrictionCodes)
(restrictionCodes as readonly string[]).includes(code);

export const websocQuerySchema = z.object({
year: z
.string({ message: "Parameter 'year' is required " })
.length(4, { message: "Parameter 'year' must have length 4" }),
year: yearSchema,
quarter: z.enum(terms, { required_error: "Parameter 'quarter' is required" }),
ge: z
.enum(geCategories)
Expand Down
7 changes: 2 additions & 5 deletions apps/api/src/schema/week.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { z } from "@hono/zod-openapi";
import { yearSchema } from "./lib";
const shortMonths = [4, 6, 9, 11];

const isLeap = (x: number) => x % 4 === 0 && (x % 100 === 0 ? x % 400 === 0 : true);

export const weekQuerySchema = z
.object({
year: z.coerce
.number()
.refine((x) => x.toString().length === 4, { message: "Parameter 'year' must have length 4" })
.openapi({ example: 2024 })
.optional(),
year: yearSchema.transform((x) => Number.parseInt(x, 10)).optional(),
month: z.coerce
.number()
.refine((x) => 1 <= x && x <= 12, {
Expand Down

0 comments on commit a44c1bd

Please sign in to comment.