Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(tags): update tag color validation to allow uppercase letters (#524)

Corentin Thomassetc84a9219886e
diff --git a/.changeset/quiet-peas-mate.md b/.changeset/quiet-peas-mate.md
new file mode 100644
index 00000000..20df29d9
--- /dev/null
+++ b/.changeset/quiet-peas-mate.md
@@ -0,0 +1,5 @@
+---
+"@papra/app-server": patch
+---
+
+Fixed the api validation of tag colors to make it case incensitive
diff --git a/apps/papra-server/src/modules/tags/tags.constants.ts b/apps/papra-server/src/modules/tags/tags.constants.ts
index dd70ca02..3651a121 100644
--- a/apps/papra-server/src/modules/tags/tags.constants.ts
+++ b/apps/papra-server/src/modules/tags/tags.constants.ts
@@ -1,6 +1,6 @@
import { createPrefixedIdRegex } from '../shared/random/ids';
-export const TagColorRegex = /^#[0-9a-f]{6}$/;
+export const TagColorRegex = /^#[0-9A-F]{6}$/;
export const tagIdPrefix = 'tag';
export const tagIdRegex = createPrefixedIdRegex({ prefix: tagIdPrefix });
diff --git a/apps/papra-server/src/modules/tags/tags.routes.ts b/apps/papra-server/src/modules/tags/tags.routes.ts
index 694ee8ec..eaddd446 100644
--- a/apps/papra-server/src/modules/tags/tags.routes.ts
+++ b/apps/papra-server/src/modules/tags/tags.routes.ts
@@ -14,10 +14,9 @@ import { ensureUserIsInOrganization } from '../organizations/organizations.useca
import { validateJsonBody, validateParams } from '../shared/validation/validation';
import { createWebhookRepository } from '../webhooks/webhook.repository';
import { deferTriggerWebhooks } from '../webhooks/webhook.usecases';
-import { TagColorRegex } from './tags.constants';
import { createTagNotFoundError } from './tags.errors';
import { createTagsRepository } from './tags.repository';
-import { tagIdSchema } from './tags.schemas';
+import { tagColorSchema, tagIdSchema } from './tags.schemas';
export function registerTagsRoutes(context: RouteDefinitionContext) {
setupCreateNewTagRoute(context);
@@ -38,7 +37,7 @@ function setupCreateNewTagRoute({ app, db }: RouteDefinitionContext) {
validateJsonBody(z.object({
name: z.string().min(1).max(50),
- color: z.string().regex(TagColorRegex, 'Invalid Color format, must be a hex color code like #000000'),
+ color: tagColorSchema,
description: z.string().max(256).optional(),
})),
@@ -95,7 +94,7 @@ function setupUpdateTagRoute({ app, db }: RouteDefinitionContext) {
validateJsonBody(z.object({
name: z.string().min(1).max(64).optional(),
- color: z.string().regex(TagColorRegex, 'Invalid Color format, must be a hex color code like #000000').optional(),
+ color: tagColorSchema.optional(),
description: z.string().max(256).optional(),
})),
diff --git a/apps/papra-server/src/modules/tags/tags.schemas.test.ts b/apps/papra-server/src/modules/tags/tags.schemas.test.ts
new file mode 100644
index 00000000..f457dbaa
--- /dev/null
+++ b/apps/papra-server/src/modules/tags/tags.schemas.test.ts
@@ -0,0 +1,25 @@
+import { describe, expect, test } from 'vitest';
+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(() => tagColorSchema.parse('FFFFFF')).toThrow();
+ expect(() => tagColorSchema.parse('#FFF')).toThrow();
+ expect(() => tagColorSchema.parse('#123ABCG')).toThrow();
+ expect(() => tagColorSchema.parse('#123AB')).toThrow();
+ expect(() => tagColorSchema.parse('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');
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/tags/tags.schemas.ts b/apps/papra-server/src/modules/tags/tags.schemas.ts
index 1b98b3da..be2f4935 100644
--- a/apps/papra-server/src/modules/tags/tags.schemas.ts
+++ b/apps/papra-server/src/modules/tags/tags.schemas.ts
@@ -1,4 +1,5 @@
import { z } from 'zod';
-import { tagIdRegex } from './tags.constants';
+import { TagColorRegex, tagIdRegex } from './tags.constants';
export const tagIdSchema = z.string().regex(tagIdRegex);
+export const tagColorSchema = z.string().toUpperCase().regex(TagColorRegex, 'Invalid Color format, must be a hex color code like #000000');