Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

fix(config): properly generate config schema for ide autocompletion (#985)

Corentin Thomasset00cfbf88dfb3
diff --git a/apps/docs/package.json b/apps/docs/package.json
index dc2854f6..a1df9f6e 100644
--- a/apps/docs/package.json
+++ b/apps/docs/package.json
@@ -19,6 +19,7 @@
"dependencies": {
"@astrojs/solid-js": "^5.1.0",
"@astrojs/starlight": "^0.34.3",
+ "@valibot/to-json-schema": "^1.6.0",
"astro": "catalog:",
"sharp": "^0.32.5",
"shiki": "^3.4.2",
@@ -26,9 +27,8 @@
"starlight-theme-rapide": "^0.5.0",
"tailwind-merge": "^2.6.0",
"unocss-preset-animations": "catalog:",
- "yaml": "^2.8.0",
- "zod": "^3.25.67",
- "zod-to-json-schema": "^3.24.5"
+ "valibot": "catalog:",
+ "yaml": "^2.8.0"
},
"devDependencies": {
"@antfu/eslint-config": "catalog:",
diff --git a/apps/docs/src/pages/papra-config-schema.json.ts b/apps/docs/src/pages/papra-config-schema.json.ts
index df15a584..aab1e02c 100644
--- a/apps/docs/src/pages/papra-config-schema.json.ts
+++ b/apps/docs/src/pages/papra-config-schema.json.ts
@@ -1,46 +1,32 @@
import type { APIRoute } from 'astro';
import type { ConfigDefinition } from 'figue';
-import { mapValues } from 'lodash-es';
-import { z } from 'zod';
-import { zodToJsonSchema } from 'zod-to-json-schema';
+import { toJsonSchema } from '@valibot/to-json-schema';
+import * as v from 'valibot';
import { configDefinition } from '../../../papra-server/src/modules/config/config';
-function buildConfigSchema({ configDefinition }: { configDefinition: ConfigDefinition }) {
- const schema: any = mapValues(configDefinition, (config) => {
- if (typeof config === 'object' && config !== null && 'schema' in config && 'doc' in config) {
- return config.schema;
+function buildConfigSchema({ configDefinition }: { configDefinition: ConfigDefinition }): v.GenericSchema {
+ const entries: Record<string, v.GenericSchema> = {};
+
+ for (const [key, value] of Object.entries(configDefinition)) {
+ if ('schema' in value) {
+ entries[key] = v.optional(value.schema as v.GenericSchema);
} else {
- return buildConfigSchema({
- configDefinition: config as ConfigDefinition,
- });
+ entries[key] = v.optional(buildConfigSchema({ configDefinition: value }));
}
- });
+ }
- return z.object(schema);
+ return v.object(entries);
}
-function stripRequired(schema: any) {
- if (schema.type === 'object') {
- schema.required = [];
- for (const key in schema.properties) {
- stripRequired(schema.properties[key]);
- }
- }
-}
+function getConfigSchema() {
+ const schema = buildConfigSchema({ configDefinition });
+ const jsonSchema = toJsonSchema(schema, { typeMode: 'input', errorMode: 'ignore' });
-function addSchema(schema: any) {
- schema.properties.$schema = {
+ (jsonSchema.properties ??= {}).$schema = {
type: 'string',
description: 'The schema of the configuration file, to be used by IDEs to provide autocompletion and validation',
};
-}
-
-function getConfigSchema() {
- const schema = buildConfigSchema({ configDefinition });
- const jsonSchema = zodToJsonSchema(schema, { pipeStrategy: 'output' });
- stripRequired(jsonSchema);
- addSchema(jsonSchema);
return jsonSchema;
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index dc3683fb..1a25111f 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -86,6 +86,9 @@ importers:
'@astrojs/starlight':
specifier: ^0.34.3
+ '@valibot/to-json-schema':
+ specifier: ^1.6.0
astro:
specifier: 'catalog:'
@@ -107,15 +110,12 @@ importers:
unocss-preset-animations:
specifier: 'catalog:'
+ valibot:
+ specifier: 'catalog:'
+ version: 1.3.1([email protected])
yaml:
specifier: ^2.8.0
version: 2.8.2
- zod:
- specifier: ^3.25.67
- version: 3.25.76
- zod-to-json-schema:
- specifier: ^3.24.5
- version: 3.25.1([email protected])
devDependencies:
'@antfu/eslint-config':
specifier: 'catalog:'
@@ -5407,6 +5407,11 @@ packages:
peerDependencies:
'@urql/core': ^5.0.0
+ '@valibot/[email protected]':
+ resolution: {integrity: sha512-d6rYyK5KVa2XdqamWgZ4/Nr+cXhxjy7lmpe6Iajw15J/jmU+gyxl2IEd1Otg1d7Rl3gOQL5reulnSypzBtYy1A==}
+ peerDependencies:
+ valibot: ^1.3.0
+
'@vitest/[email protected]':
resolution: {integrity: sha512-I+MlLwyJRBjmJr1kFYSxoseINbIdpxIAeK10jmXgB0FUtIfdYsvM3lGAvBu5yk8WPyhefzdmbCHCc1idFbNRcg==}
peerDependencies:
@@ -17737,6 +17742,10 @@ snapshots:
'@urql/core': 5.2.0([email protected])
wonka: 6.3.5
+ dependencies:
+ valibot: 1.3.1([email protected])
+
dependencies:
'@bcoe/v8-coverage': 1.0.2