Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

test(validation): add valibot validation middleware (#1029)

Corentin Thomasset366e4eead117
diff --git a/apps/papra-server/src/modules/shared/validation/validation.test.ts b/apps/papra-server/src/modules/shared/validation/validation.test.ts
new file mode 100644
index 00000000..baf97ce8
--- /dev/null
+++ b/apps/papra-server/src/modules/shared/validation/validation.test.ts
@@ -0,0 +1,189 @@
+import { Hono } from 'hono';
+import * as v from 'valibot';
+import { describe, expect, test } from 'vitest';
+import { validateJsonBody, validateParams, validateQuery } from './validation';
+
+function jsonPayload(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(
+ '/',
+ validateJsonBody(v.object({ name: v.string() })),
+ (context) => {
+ return context.json({ ok: true });
+ },
+ );
+
+ const response = await app.request('/', { method: 'POST', ...jsonPayload({}) });
+ 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: 'Invalid key: Expected "name" but received undefined',
+ }],
+ },
+ });
+ });
+
+ test('a valid request should pass through', async () => {
+ const app = new Hono().post(
+ '/',
+ validateJsonBody(v.object({ name: v.string() })),
+ (context) => {
+ return context.json({ ok: true });
+ },
+ );
+
+ const response = await app.request('/', { method: 'POST', ...jsonPayload({ 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(
+ '/',
+ validateJsonBody(v.strictObject({ name: v.string() })),
+ (context) => {
+ return context.json({ ok: true });
+ },
+ );
+
+ const response = await app.request('/', { method: 'POST', ...jsonPayload({ 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: [{
+ path: 'foo',
+ message: 'Invalid key: Expected never but received "foo"',
+ }],
+ },
+ });
+ });
+
+ test('default values are applied', async () => {
+ const app = new Hono().post(
+ '/',
+ validateJsonBody(v.object({ name: v.optional(v.string(), 'pocky') })),
+ (context) => {
+ const body = context.req.valid('json');
+ return context.json({ ok: true, name: body.name });
+ },
+ );
+
+ const response = await app.request('/', { method: 'POST' });
+ const responseBody = await response.json();
+
+ expect(responseBody).toEqual({ ok: true, name: 'pocky' });
+ });
+ });
+ });
+
+ 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(
+ '/',
+ validateQuery(v.object({ name: v.string() })),
+ (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: 'Invalid key: Expected "name" but received undefined',
+ }],
+ },
+ });
+ });
+
+ test('a valid query should pass through', async () => {
+ const app = new Hono().get(
+ '/',
+ validateQuery(v.object({ name: v.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',
+ validateParams(v.object({ name: v.pipe(v.string(), v.startsWith('foo-', 'Invalid input: must start with "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',
+ validateParams(v.object({ name: v.pipe(v.string(), v.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.ts b/apps/papra-server/src/modules/shared/validation/validation.ts
new file mode 100644
index 00000000..53c533e5
--- /dev/null
+++ b/apps/papra-server/src/modules/shared/validation/validation.ts
@@ -0,0 +1,37 @@
+import type { ValidationTargets } from 'hono';
+import { validator } from 'hono/validator';
+import * as v from 'valibot';
+
+function formatValidationIssues(issues: v.GenericIssue[]) {
+ return issues.map((issue) => {
+ return {
+ path: v.getDotPath(issue),
+ message: issue.message,
+ };
+ });
+}
+
+function buildValidator<Target extends keyof ValidationTargets>({ target, error }: { target: Target; error: { message: string; code: string } }) {
+ return <Schema extends v.GenericSchema>(schema: Schema) => validator(target, (value, context) => {
+ const result = v.safeParse(schema, value);
+
+ if (result.success) {
+ return result.output;
+ }
+
+ return context.json(
+ {
+ error: {
+ ...error,
+ details: formatValidationIssues(result.issues),
+ },
+ },
+ 400,
+ );
+ });
+}
+
+export const validateJsonBody = buildValidator({ target: 'json', error: { message: 'Invalid request body', code: 'server.invalid_request.body' } });
+export const validateQuery = buildValidator({ target: 'query', error: { message: 'Invalid query parameters', code: 'server.invalid_request.query' } });
+export const validateParams = buildValidator({ target: 'param', error: { message: 'Invalid URL parameters', code: 'server.invalid_request.params' } });
+export const validateFormData = buildValidator({ target: 'form', error: { message: 'Invalid form data', code: 'server.invalid_request.form_data' } });