Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

refactor(tags): migrate route validation from zod to valibot (#1044)

Corentin Thomassete05e6e86f0b3
diff --git a/apps/papra-server/src/modules/tags/tags.routes.ts b/apps/papra-server/src/modules/tags/tags.routes.ts
index 1bef723c..0d255f82 100644
--- a/apps/papra-server/src/modules/tags/tags.routes.ts
+++ b/apps/papra-server/src/modules/tags/tags.routes.ts
@@ -1,5 +1,5 @@
import type { RouteDefinitionContext } from '../app/server.types';
-import { z } from 'zod';
+import * as v from 'valibot';
import { API_KEY_PERMISSIONS } from '../api-keys/api-keys.constants';
import { requireAuthentication } from '../app/auth/auth.middleware';
import { getUser } from '../app/auth/auth.models';
@@ -7,14 +7,14 @@ import { createDocumentActivityRepository } from '../documents/document-activity
import { deferRegisterDocumentActivityLog } from '../documents/document-activity/document-activity.usecases';
import { createDocumentNotFoundError } from '../documents/documents.errors';
import { createDocumentsRepository } from '../documents/documents.repository';
-import { documentIdSchema } from '../documents/documents.schemas.legacy';
-import { organizationIdSchema } from '../organizations/organization.schemas.legacy';
+import { documentIdSchema } from '../documents/documents.schemas';
+import { organizationIdSchema } from '../organizations/organization.schemas';
import { createOrganizationsRepository } from '../organizations/organizations.repository';
import { ensureUserIsInOrganization } from '../organizations/organizations.usecases';
-import { legacyValidateJsonBody, legacyValidateParams } from '../shared/validation/validation.legacy';
+import { validateJsonBody, validateParams } from '../shared/validation/validation';
import { createTagNotFoundError } from './tags.errors';
import { createTagsRepository } from './tags.repository';
-import { tagColorSchema, tagIdSchema } from './tags.schemas.legacy';
+import { tagColorSchema, tagDescriptionSchema, tagIdSchema, tagNameSchema } from './tags.schemas';
import { addTagToDocument, createTag } from './tags.usecases';
export function registerTagsRoutes(context: RouteDefinitionContext) {
@@ -30,16 +30,14 @@ function setupCreateNewTagRoute({ app, db, config }: RouteDefinitionContext) {
app.post(
'/api/organizations/:organizationId/tags',
requireAuthentication({ apiKeyPermissions: ['tags:create'] }),
- legacyValidateParams(z.object({
+ validateParams(v.strictObject({
organizationId: organizationIdSchema,
})),
-
- legacyValidateJsonBody(z.object({
- name: z.string().trim().min(1).max(50),
+ validateJsonBody(v.strictObject({
+ name: tagNameSchema,
color: tagColorSchema,
- description: z.string().trim().max(256).optional(),
+ description: v.optional(tagDescriptionSchema),
})),
-
async (context) => {
const { userId } = getUser({ context });
@@ -64,10 +62,9 @@ function setupGetOrganizationTagsRoute({ app, db }: RouteDefinitionContext) {
app.get(
'/api/organizations/:organizationId/tags',
requireAuthentication({ apiKeyPermissions: ['tags:read'] }),
- legacyValidateParams(z.object({
+ validateParams(v.strictObject({
organizationId: organizationIdSchema,
})),
-
async (context) => {
const { userId } = getUser({ context });
@@ -91,17 +88,15 @@ function setupUpdateTagRoute({ app, db }: RouteDefinitionContext) {
app.put(
'/api/organizations/:organizationId/tags/:tagId',
requireAuthentication({ apiKeyPermissions: ['tags:update'] }),
- legacyValidateParams(z.object({
+ validateParams(v.strictObject({
organizationId: organizationIdSchema,
tagId: tagIdSchema,
})),
-
- legacyValidateJsonBody(z.object({
- name: z.string().trim().min(1).max(64).optional(),
- color: tagColorSchema.optional(),
- description: z.string().trim().max(256).optional(),
+ validateJsonBody(v.strictObject({
+ name: v.optional(tagNameSchema),
+ color: v.optional(tagColorSchema),
+ description: v.optional(tagDescriptionSchema),
})),
-
async (context) => {
const { userId } = getUser({ context });
@@ -126,11 +121,10 @@ function setupDeleteTagRoute({ app, db }: RouteDefinitionContext) {
app.delete(
'/api/organizations/:organizationId/tags/:tagId',
requireAuthentication({ apiKeyPermissions: ['tags:delete'] }),
- legacyValidateParams(z.object({
+ validateParams(v.strictObject({
organizationId: organizationIdSchema,
tagId: tagIdSchema,
})),
-
async (context) => {
const { userId } = getUser({ context });
@@ -152,15 +146,13 @@ function setupAddTagToDocumentRoute({ app, db, webhookTriggerServices }: RouteDe
app.post(
'/api/organizations/:organizationId/documents/:documentId/tags',
requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.DOCUMENTS.UPDATE, API_KEY_PERMISSIONS.TAGS.READ] }),
- legacyValidateParams(z.object({
+ validateParams(v.strictObject({
organizationId: organizationIdSchema,
documentId: documentIdSchema,
})),
-
- legacyValidateJsonBody(z.object({
+ validateJsonBody(v.strictObject({
tagId: tagIdSchema,
})),
-
async (context) => {
const { userId } = getUser({ context });
@@ -207,12 +199,11 @@ function setupRemoveTagFromDocumentRoute({ app, db, webhookTriggerServices }: Ro
app.delete(
'/api/organizations/:organizationId/documents/:documentId/tags/:tagId',
requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.DOCUMENTS.UPDATE, API_KEY_PERMISSIONS.TAGS.READ] }),
- legacyValidateParams(z.object({
+ validateParams(v.strictObject({
organizationId: organizationIdSchema,
documentId: documentIdSchema,
tagId: tagIdSchema,
})),
-
async (context) => {
const { userId } = getUser({ context });
diff --git a/apps/papra-server/src/modules/tags/tags.schemas.test.ts b/apps/papra-server/src/modules/tags/tags.schemas.test.ts
index 030e17ad..46777889 100644
--- a/apps/papra-server/src/modules/tags/tags.schemas.test.ts
+++ b/apps/papra-server/src/modules/tags/tags.schemas.test.ts
@@ -1,25 +1,26 @@
+import * as v from 'valibot';
import { describe, expect, test } from 'vitest';
-import { tagColorSchema } from './tags.schemas.legacy';
+import { tagColorSchema } from './tags.schemas';
describe('tags schemas', () => {
describe('tagColorSchema', () => {
test('the color of a tag is a 6 digits hex color code', () => {
- expect(() => tagColorSchema.parse('#FFFFFF')).not.toThrow();
- expect(() => tagColorSchema.parse('#000000')).not.toThrow();
- expect(() => tagColorSchema.parse('#123ABC')).not.toThrow();
- expect(() => tagColorSchema.parse('#abcdef')).not.toThrow();
+ expect(() => v.parse(tagColorSchema, '#FFFFFF')).not.toThrow();
+ expect(() => v.parse(tagColorSchema, '#000000')).not.toThrow();
+ expect(() => v.parse(tagColorSchema, '#123ABC')).not.toThrow();
+ expect(() => v.parse(tagColorSchema, '#abcdef')).not.toThrow();
- expect(() => tagColorSchema.parse('FFFFFF')).toThrow();
- expect(() => tagColorSchema.parse('#FFF')).toThrow();
- expect(() => tagColorSchema.parse('#123ABCG')).toThrow();
- expect(() => tagColorSchema.parse('#123AB')).toThrow();
- expect(() => tagColorSchema.parse('blue')).toThrow();
+ expect(() => v.parse(tagColorSchema, 'FFFFFF')).toThrow();
+ expect(() => v.parse(tagColorSchema, '#FFF')).toThrow();
+ expect(() => v.parse(tagColorSchema, '#123ABCG')).toThrow();
+ expect(() => v.parse(tagColorSchema, '#123AB')).toThrow();
+ expect(() => v.parse(tagColorSchema, 'blue')).toThrow();
});
test('the color of a tag is always uppercased', () => {
- expect(tagColorSchema.parse('#abcdef')).toBe('#ABCDEF');
- expect(tagColorSchema.parse('#abCdEf')).toBe('#ABCDEF');
- expect(tagColorSchema.parse('#123abc')).toBe('#123ABC');
+ expect(v.parse(tagColorSchema, '#abcdef')).toBe('#ABCDEF');
+ expect(v.parse(tagColorSchema, '#abCdEf')).toBe('#ABCDEF');
+ expect(v.parse(tagColorSchema, '#123abc')).toBe('#123ABC');
});
});
});
diff --git a/apps/papra-server/src/modules/tags/tags.schemas.ts b/apps/papra-server/src/modules/tags/tags.schemas.ts
index c70d7b64..8219842e 100644
--- a/apps/papra-server/src/modules/tags/tags.schemas.ts
+++ b/apps/papra-server/src/modules/tags/tags.schemas.ts
@@ -1,4 +1,24 @@
+import * as v from 'valibot';
import { createRegexSchema } from '../shared/schemas/string.schemas';
-import { tagIdRegex } from './tags.constants';
+import { TagColorRegex, tagIdRegex } from './tags.constants';
export const tagIdSchema = createRegexSchema(tagIdRegex);
+
+export const tagNameSchema = v.pipe(
+ v.string(),
+ v.trim(),
+ v.minLength(1),
+ v.maxLength(64),
+);
+
+export const tagColorSchema = v.pipe(
+ v.string(),
+ v.toUpperCase(),
+ v.regex(TagColorRegex, 'Invalid Color format, must be a hex color code like #000000'),
+);
+
+export const tagDescriptionSchema = v.pipe(
+ v.string(),
+ v.trim(),
+ v.maxLength(256),
+);