Skip to content

Commit

Permalink
fix: replace zod with valibot
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst committed Apr 15, 2024
1 parent 947a8d9 commit 5773b30
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 40 deletions.
3 changes: 2 additions & 1 deletion app/[locale]/(index)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createUrlSearchParams } from "@acdh-oeaw/lib";
import type { Metadata, ResolvingMetadata } from "next";
import { useTranslations } from "next-intl";
import { getTranslations, unstable_setRequestLocale as setRequestLocale } from "next-intl/server";
import * as v from "valibot";

import { MainContent } from "@/components/main-content";
import { ResourcesSection } from "@/components/resources-section";
Expand Down Expand Up @@ -48,7 +49,7 @@ export default function IndexPage(props: IndexPageProps) {

const searchParamsSchema = createResourceFiltersSearchParamsSchema(locale);

const filters = searchParamsSchema.parse({
const filters = v.parse(searchParamsSchema, {
limit: urlSearchParams.get("limit"),
locale: urlSearchParams.get("locale"),
page: urlSearchParams.get("page"),
Expand Down
3 changes: 2 additions & 1 deletion app/[locale]/curricula/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createUrlSearchParams } from "@acdh-oeaw/lib";
import type { Metadata, ResolvingMetadata } from "next";
import { useTranslations } from "next-intl";
import { getTranslations, unstable_setRequestLocale as setRequestLocale } from "next-intl/server";
import * as v from "valibot";

import { CurriculaSection } from "@/components/curricula-section";
import { MainContent } from "@/components/main-content";
Expand Down Expand Up @@ -44,7 +45,7 @@ export default function CurriculaPage(props: CurriculaPageProps) {

const searchParamsSchema = createResourceFiltersSearchParamsSchema(locale);

const filters = searchParamsSchema.parse({
const filters = v.parse(searchParamsSchema, {
limit: urlSearchParams.get("limit"),
locale: urlSearchParams.get("locale"),
page: urlSearchParams.get("page"),
Expand Down
3 changes: 2 additions & 1 deletion app/[locale]/resources/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createUrlSearchParams } from "@acdh-oeaw/lib";
import type { Metadata, ResolvingMetadata } from "next";
import { useTranslations } from "next-intl";
import { getTranslations, unstable_setRequestLocale as setRequestLocale } from "next-intl/server";
import * as v from "valibot";

import { MainContent } from "@/components/main-content";
import { ResourcesSection } from "@/components/resources-section";
Expand Down Expand Up @@ -44,7 +45,7 @@ export default function ResourcesPage(props: ResourcesPageProps) {

const searchParamsSchema = createResourceFiltersSearchParamsSchema(locale);

const filters = searchParamsSchema.parse({
const filters = v.parse(searchParamsSchema, {
limit: urlSearchParams.get("limit"),
locale: urlSearchParams.get("locale"),
page: urlSearchParams.get("page"),
Expand Down
12 changes: 6 additions & 6 deletions app/api/metadata/curricula/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import { join } from "node:path";

import { log } from "@acdh-oeaw/lib";
import type { NextRequest } from "next/server";
import { z } from "zod";
import * as v from "valibot";

import type { Resource } from "@/lib/content/types";

const filePath = join(process.cwd(), "./public/metadata/curricula.json");

const searchParamsSchema = z.object({
limit: z.coerce.number().int().positive().max(100).optional().default(10),
offset: z.coerce.number().int().nonnegative().optional().default(0),
const searchParamsSchema = v.object({
limit: v.optional(v.coerce(v.number([v.integer(), v.minValue(1), v.maxValue(100)]), Number), 10),
offset: v.optional(v.coerce(v.number([v.integer(), v.minValue(0)]), Number), 0),
});

export async function GET(request: NextRequest) {
const { searchParams } = request.nextUrl;

const result = searchParamsSchema.safeParse({
const result = v.safeParse(searchParamsSchema, {
limit: searchParams.get("limit"),
offset: searchParams.get("offset"),
});
Expand All @@ -26,7 +26,7 @@ export async function GET(request: NextRequest) {
return Response.json({ message: "Bad request" }, { status: 400 });
}

const { offset, limit } = result.data;
const { offset, limit } = result.output;

try {
const curricula = await readCurricula();
Expand Down
12 changes: 6 additions & 6 deletions app/api/metadata/resources/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import { join } from "node:path";

import { log } from "@acdh-oeaw/lib";
import type { NextRequest } from "next/server";
import { z } from "zod";
import * as v from "valibot";

import type { Resource } from "@/lib/content/types";

const filePath = join(process.cwd(), "./public/metadata/resources.json");

const searchParamsSchema = z.object({
limit: z.coerce.number().int().positive().max(100).optional().default(10),
offset: z.coerce.number().int().nonnegative().optional().default(0),
const searchParamsSchema = v.object({
limit: v.optional(v.coerce(v.number([v.integer(), v.minValue(1), v.maxValue(100)]), Number), 10),
offset: v.optional(v.coerce(v.number([v.integer(), v.minValue(0)]), Number), 0),
});

export async function GET(request: NextRequest) {
const { searchParams } = request.nextUrl;

const result = searchParamsSchema.safeParse({
const result = v.safeParse(searchParamsSchema, {
limit: searchParams.get("limit"),
offset: searchParams.get("offset"),
});
Expand All @@ -26,7 +26,7 @@ export async function GET(request: NextRequest) {
return Response.json({ message: "Bad request" }, { status: 400 });
}

const { offset, limit } = result.data;
const { offset, limit } = result.output;

try {
const resources = await readResources();
Expand Down
14 changes: 7 additions & 7 deletions components/curricula-filter-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createUrlSearchParams } from "@acdh-oeaw/lib";
import { useOptimistic, useState } from "react";
import { ListBox, ListBoxItem, type Selection } from "react-aria-components";
import { useFormState } from "react-dom";
import { z } from "zod";
import * as v from "valibot";

import { SelectField, SelectItem } from "@/components/ui/blocks/select-field";
import { TextInputField } from "@/components/ui/blocks/text-input-field";
Expand All @@ -16,13 +16,13 @@ import { getFormData } from "@/lib/get-form-data";
import { useRouter } from "@/lib/navigation";
import { cn } from "@/lib/styles";

const formSchema = z.object({
locale: z.enum([...locales, "all"]).optional(),
q: z.string().optional(),
tag: z.array(z.string()).optional(),
const formSchema = v.object({
locale: v.optional(v.picklist([...locales, "all"])),
q: v.optional(v.string()),
tag: v.optional(v.array(v.string())),
});

type FormSchema = z.infer<typeof formSchema>;
type FormSchema = v.Output<typeof formSchema>;

interface CurriculaFilterFormProps {
filters: FormSchema;
Expand All @@ -49,7 +49,7 @@ export function CurriculaFilterForm(props: CurriculaFilterFormProps) {

function action(prevState: undefined, formData: FormData) {
const input = getFormData(formData);
const filters = formSchema.parse(input);
const filters = v.parse(formSchema, input);

updateOptimisticFilters(filters);
router.push("?" + String(createUrlSearchParams(filters)));
Expand Down
14 changes: 7 additions & 7 deletions components/resources-filter-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createUrlSearchParams } from "@acdh-oeaw/lib";
import { useOptimistic, useState } from "react";
import { ListBox, ListBoxItem, type Selection } from "react-aria-components";
import { useFormState } from "react-dom";
import { z } from "zod";
import * as v from "valibot";

import { SelectField, SelectItem } from "@/components/ui/blocks/select-field";
import { TextInputField } from "@/components/ui/blocks/text-input-field";
Expand All @@ -16,13 +16,13 @@ import { getFormData } from "@/lib/get-form-data";
import { useRouter } from "@/lib/navigation";
import { cn } from "@/lib/styles";

const formSchema = z.object({
locale: z.enum([...locales, "all"]).optional(),
q: z.string().optional(),
tag: z.array(z.string()).optional(),
const formSchema = v.object({
locale: v.optional(v.picklist([...locales, "all"])),
q: v.optional(v.string()),
tag: v.optional(v.array(v.string())),
});

type FormSchema = z.infer<typeof formSchema>;
type FormSchema = v.Output<typeof formSchema>;

interface ResourcesFilterFormProps {
filters: FormSchema;
Expand All @@ -49,7 +49,7 @@ export function ResourcesFilterForm(props: ResourcesFilterFormProps) {

function action(prevState: undefined, formData: FormData) {
const input = getFormData(formData);
const filters = formSchema.parse(input);
const filters = v.parse(formSchema, input);

updateOptimisticFilters(filters);
router.push("?" + String(createUrlSearchParams(filters)));
Expand Down
21 changes: 10 additions & 11 deletions lib/schemas/resource-filter-schema.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { z } from "zod";
import * as v from "valibot";

import { type Locale, locales } from "@/config/i18n.config";

export function createResourceFiltersSearchParamsSchema(locale: Locale) {
return z.object({
limit: z.coerce.number().int().positive().max(100).optional().default(20).catch(20),
locale: z
.enum([...locales, "all"])
.optional()
.default(locale)
.catch(locale),
page: z.coerce.number().int().positive().optional().default(1).catch(1),
q: z.string().optional().default("").catch(""),
tag: z.array(z.string()).optional().default([]).catch([]),
return v.object({
limit: v.fallback(
v.optional(v.coerce(v.number([v.integer(), v.minValue(1), v.maxValue(100)]), Number), 20),
20,
),
locale: v.fallback(v.optional(v.picklist([...locales, "all"]), locale), locale),
page: v.fallback(v.optional(v.coerce(v.number([v.integer(), v.minValue(1)]), Number), 1), 1),
q: v.fallback(v.optional(v.string(), ""), ""),
tag: v.fallback(v.optional(v.array(v.string()), []), []),
});
}

0 comments on commit 5773b30

Please sign in to comment.