Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

chore(server): fully remove remaining zod (#1050)

Corentin Thomasset46ead73ace95
diff --git a/apps/docs/package.json b/apps/docs/package.json
index 22afc276..d6f34e3d 100644
--- a/apps/docs/package.json
+++ b/apps/docs/package.json
@@ -28,7 +28,8 @@
"tailwind-merge": "^2.6.0",
"unocss-preset-animations": "catalog:",
"valibot": "catalog:",
- "yaml": "^2.8.0"
+ "yaml": "^2.8.0",
+ "zod": "3.25.76"
},
"devDependencies": {
"@antfu/eslint-config": "catalog:",
diff --git a/apps/papra-server/package.json b/apps/papra-server/package.json
index 2ec21a8e..e1994d2d 100644
--- a/apps/papra-server/package.json
+++ b/apps/papra-server/package.json
@@ -83,8 +83,7 @@
"sanitize-html": "^2.17.0",
"stripe": "^17.7.0",
"tsx": "catalog:",
- "valibot": "catalog:",
- "zod": "^3.25.67"
+ "valibot": "catalog:"
},
"devDependencies": {
"@antfu/eslint-config": "catalog:",
diff --git a/apps/papra-server/src/modules/shared/validation/validation.legacy.test.ts b/apps/papra-server/src/modules/shared/validation/validation.legacy.test.ts
deleted file mode 100644
index f93acf36..00000000
--- a/apps/papra-server/src/modules/shared/validation/validation.legacy.test.ts
+++ /dev/null
@@ -1,146 +0,0 @@
-import { Hono } from 'hono';
-import { describe, expect, test } from 'vitest';
-import { z } from 'zod';
-import { legacyValidateJsonBody, legacyValidateParams, legacyValidateQuery } from './validation.legacy';
-
-function makeJsonBodyPayload(payload: Record<string, unknown>) {
- return {
- body: JSON.stringify(payload),
- headers: new Headers({ 'Content-Type': 'application/json' }),
- };
-}
-
-describe('validation', () => {
- describe('validateJsonBody', () => {
- describe('validateJsonBody creates a validation middleware that check the request json body against a schema', async () => {
- test('an invalid payload should trigger a 400 error', async () => {
- const app = new Hono().post('/', legacyValidateJsonBody(z.object({ name: z.string({ required_error: 'The name is required' }) })), (context) => {
- return context.json({ ok: true });
- });
-
- const response = await app.request('/', { method: 'POST', ...makeJsonBodyPayload({ }) });
- const responseBody = await response.json();
-
- expect(response.status).toBe(400);
- expect(responseBody).toEqual({
- error: {
- message: 'Invalid request body',
- code: 'server.invalid_request.body',
- details: [{
- path: 'name',
- message: 'The name is required',
- }],
- },
- });
- });
-
- test('a valid request should pass through', async () => {
- const app = new Hono().post('/', legacyValidateJsonBody(z.object({ name: z.string() })), (context) => {
- return context.json({ ok: true });
- });
-
- const response = await app.request('/', { method: 'POST', ...makeJsonBodyPayload({ name: 'hono' }) });
- const responseBody = await response.json();
-
- expect(response.status).toBe(200);
- expect(responseBody).toEqual({ ok: true });
- });
-
- test('no additional properties should be allowed', async () => {
- const app = new Hono().post('/', legacyValidateJsonBody(z.object({ name: z.string() })), (context) => {
- return context.json({ ok: true });
- });
-
- const response = await app.request('/', { method: 'POST', ...makeJsonBodyPayload({ name: 'hono', foo: 'bar' }) });
- const responseBody = await response.json();
-
- expect(response.status).toBe(400);
- expect(responseBody).toEqual({
- error: {
- message: 'Invalid request body',
- code: 'server.invalid_request.body',
- details: [{
- message: 'Unrecognized key(s) in object: \'foo\'',
- }],
- },
- });
- });
- });
- });
-
- describe('validateQuery', () => {
- describe('validateQuery creates a validation middleware that check the request query parameters against a schema', async () => {
- test('an invalid query should trigger a 400 error', async () => {
- const app = new Hono().get('/', legacyValidateQuery(z.object({ name: z.string({ required_error: 'The name is required' }) })), (context) => {
- return context.json({ ok: true });
- });
-
- const response = await app.request('/');
- const responseBody = await response.json();
-
- expect(response.status).toBe(400);
- expect(responseBody).toEqual({
- error: {
- message: 'Invalid query parameters',
- code: 'server.invalid_request.query',
- details: [{
- path: 'name',
- message: 'The name is required',
- }],
- },
- });
- });
-
- test('a valid query should pass through', async () => {
- const app = new Hono().get('/', legacyValidateQuery(z.object({ name: z.string() })), (context) => {
- return context.json({ ok: true });
- });
-
- const response = await app.request('/?name=hono');
- const responseBody = await response.json();
-
- expect(response.status).toBe(200);
- expect(responseBody).toEqual({ ok: true });
- });
- });
- });
-
- describe('validateParams', () => {
- describe('validateParams creates a validation middleware that check the request url parameters against a schema', async () => {
- test('an invalid params should trigger a 400 error', async () => {
- const app = new Hono().get('/:name', legacyValidateParams(z.object({ name: z.string().startsWith('foo-') })), (context) => {
- return context.json({ ok: true });
- });
-
- const response = await app.request('/test');
- const responseBody = await response.json();
-
- expect(response.status).toBe(400);
- expect(responseBody).toEqual({
- error: {
- message: 'Invalid URL parameters',
- code: 'server.invalid_request.params',
- details: [
- {
- path: 'name',
- message: 'Invalid input: must start with "foo-"',
- },
- ],
- },
- });
- });
-
- test('a valid params should pass through', async () => {
- const app = new Hono().get('/:name', legacyValidateParams(z.object({ name: z.string().startsWith('foo-') })), (context) => {
- return context.json({ ok: true });
- });
-
- const response = await app.request('/foo-bar');
- const responseBody = await response.json();
-
- expect(response.status).toBe(200);
- expect(responseBody).toEqual({ ok: true });
- });
- });
- });
-});
diff --git a/apps/papra-server/src/modules/shared/validation/validation.legacy.ts b/apps/papra-server/src/modules/shared/validation/validation.legacy.ts
deleted file mode 100644
index e54b6bbc..00000000
--- a/apps/papra-server/src/modules/shared/validation/validation.legacy.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-import type { ValidationTargets } from 'hono';
-import type z from 'zod';
-import { validator } from 'hono/validator';
-
-function formatValidationError({ error }: { error: z.ZodError }) {
- const details = (error.errors ?? []).map((e) => {
- return {
- ...(e.path.length === 0 ? {} : { path: e.path.join('.') }),
- message: e.message,
- };
- });
-
- return details;
-}
-
-function buildLegacyValidator<Target extends keyof ValidationTargets>({ target, error }: { target: Target; error: { message: string; code: string } }) {
- return <Schema extends z.ZodTypeAny>(schema: Schema, { allowAdditionalFields = false }: { allowAdditionalFields?: boolean } = {}) => {
- return validator(target, (value, context) => {
- // @ts-expect-error try to enforce strict mode
- // eslint-disable-next-line ts/no-unsafe-assignment, ts/no-unsafe-call
- const refinedSchema: Schema = allowAdditionalFields ? schema : (schema.strict?.() ?? schema);
-
- const result = refinedSchema.safeParse(value);
-
- if (result.success) {
- // eslint-disable-next-line ts/no-unsafe-return
- return result.data as z.infer<Schema>;
- }
-
- const details = formatValidationError({ error: result.error });
-
- return context.json(
- {
- error: {
- ...error,
- details,
- },
- },
- 400,
- );
- });
- };
-}
-
-export const legacyValidateJsonBody = buildLegacyValidator({ target: 'json', error: { message: 'Invalid request body', code: 'server.invalid_request.body' } });
-export const legacyValidateQuery = buildLegacyValidator({ target: 'query', error: { message: 'Invalid query parameters', code: 'server.invalid_request.query' } });
-export const legacyValidateParams = buildLegacyValidator({ target: 'param', error: { message: 'Invalid URL parameters', code: 'server.invalid_request.params' } });
-export const legacyValidateFormData = buildLegacyValidator({ target: 'form', error: { message: 'Invalid form data', code: 'server.invalid_request.form_data' } });
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 5008f53a..20e9be5f 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -116,6 +116,9 @@ importers:
yaml:
specifier: ^2.8.0
version: 2.8.2
+ zod:
+ specifier: 3.25.76
+ version: 3.25.76
devDependencies:
'@antfu/eslint-config':
specifier: 'catalog:'
@@ -152,7 +155,7 @@ importers:
dependencies:
'@better-auth/expo':
specifier: 'catalog:'
'@corentinth/chisels':
specifier: 'catalog:'
version: 2.1.0
@@ -439,7 +442,7 @@ importers:
version: 12.27.0
'@better-auth/expo':
specifier: 'catalog:'
'@cadence-mq/core':
specifier: ^0.2.1
version: 0.2.1
@@ -560,9 +563,6 @@ importers:
valibot:
specifier: 'catalog:'
version: 1.3.1([email protected])
- zod:
- specifier: ^3.25.67
- version: 3.25.76
devDependencies:
'@antfu/eslint-config':
specifier: 'catalog:'
@@ -5511,6 +5511,7 @@ packages:
'@xmldom/[email protected]':
resolution: {integrity: sha512-cQzWCtO6C8TQiYl1ruKNn2U6Ao4o4WBBcbL61yJl84x+j5sOWWFU9X7DpND8XZG3daDppSsigMdfAIl2upQBRw==}
engines: {node: '>=10.0.0'}
+ deprecated: this version has critical issues, please update to the latest version
resolution: {integrity: sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==}
@@ -13782,31 +13783,20 @@ snapshots:
'@bcoe/[email protected]': {}
- dependencies:
- '@better-auth/utils': 0.3.0
- '@better-fetch/fetch': 1.1.18
- '@standard-schema/spec': 1.0.0
- better-call: 1.1.5([email protected])
- jose: 6.1.0
- kysely: 0.28.8
- nanostores: 1.0.1
- zod: 4.1.12
-
dependencies:
'@better-auth/utils': 0.3.0
'@better-fetch/fetch': 1.1.18
'@standard-schema/spec': 1.0.0
- better-call: 1.1.5([email protected])
+ better-call: 1.1.5([email protected])
jose: 6.1.0
kysely: 0.28.8
nanostores: 1.0.1
zod: 4.1.12
dependencies:
'@better-fetch/fetch': 1.1.18
better-call: 1.1.5([email protected])
@@ -18538,7 +18528,7 @@ snapshots:
'@better-fetch/fetch': 1.1.18
'@noble/ciphers': 2.0.1
'@noble/hashes': 2.0.1
- better-call: 1.1.5([email protected])
+ better-call: 1.1.5([email protected])
defu: 6.1.4
jose: 6.1.0
kysely: 0.28.8