Skip to content

Auto-generate JSON schema from Rust types - #2299

Merged
mre merged 10 commits into
masterfrom
fix/config-schema-validation
Oct 11, 2026
Merged

mre merged 10 commits into
masterfrom
fix/config-schema-validation

Conversation

@mre

@mre mre commented Sep 8, 2026 •

Copy link
Copy Markdown
Member
  • Builds on feat: generate a JSON schema for the config file #2286.
  • Fixes the generated schema so it matches supported config values.
  • Adds coverage for valid and invalid configs.
  • @ChrisJr404, could you take a look? The diff to schema PR is here. Note that it also contains some changes that landed in master. You can rebase your branch to see the clean diff if you like. 😃

@mre
mre force-pushed the fix/config-schema-validation branch from c24bb1a to d6a3686 Compare October 11, 2026 09:33
ChrisJr404 and others added 3 commits October 11, 2026 11:56
Add `lychee --generate config-schema`, which prints a JSON schema derived
from the config struct with schemars. Editors with a TOML language server
such as Taplo can use it for autocompletion and validation of lychee.toml.

Closes #1382
@mre
mre force-pushed the fix/config-schema-validation branch from 48043f8 to 71613b3 Compare October 11, 2026 09:56
@mre

mre commented Oct 11, 2026

Copy link
Copy Markdown
Member Author

While working on this feature, I noticed that lychee trims surrounding whitespace from basic-auth selectors. For example:

basic_auth = ["example.com user:password "]

The trailing space is silently removed, even though it could be part of the password. That's what lychee did before this PR as well, so it's not something introduced by this PR.

Other tools generally preserve spaces in the supplied credentials rather than trim or reject them: curl, Python Requests, and HTTPie. RFC 7617 also permits ordinary spaces in Basic-auth credentials, though it forbids control characters.

For this PR, I've made the schema match our existing parser. That's because rejecting whitespace only in the schema would mean that editor validation rejects configurations that lychee accepts at the moment. And I've added a comment explaining that choice.

I think we should revisit the trimming in the near future. Ideally, we'd preserve credential content rather than silently change it, but that requires a decision on how we will handle our combined <uri-regex> <username>:<password> syntax in the future. Not sure and open for suggestions.

mre added 3 commits October 11, 2026 12:24
Add schema tests covering spaces, tabs, newlines, and Unicode whitespace
around basic-auth selectors accepted by the parser.
@mre

mre commented Oct 11, 2026

Copy link
Copy Markdown
Member Author

The failing basic auth test is unrelated. Flaky endpoint.

@mre

mre commented Oct 11, 2026

Copy link
Copy Markdown
Member Author

This has been open for a while. I made a few more tweaks and switched to JSON Schema Draft 7, matching the draft used by the published SchemaStore schema.

I compared both schemas against lychee's config loader. They aren't identical: the generated schema covers newer options and follows the parser more closely in several places:

Configuration Published schema Generated schema lychee
cache_location = "cache.json" Rejects Accepts Accepts
Per-host accept = [200, 429] Rejects Accepts Accepts
verbose = "Warning" Rejects Accepts Accepts
generate = "config-schema" Rejects Accepts Accepts
accept = "banana" Accepts Rejects Rejects
method = "GET POST" Accepts Rejects Rejects
basic_auth = ["user:password"] Accepts Rejects Rejects
accept = "200..1000" Accepts Accepts Accepts

The comparison also caught a bug in our handling of exclusive range endpoints, which is now fixed. Some checks still belong to the parser: for example, both schemas accept "500..400", but lychee rejects the reversed range.

The generated file will be at docs/schema.json, with a test to keep it in sync with the Rust configuration types. It will also be included in release archives. This finally resolves the request for a versioned schema in #1382.

@Nick2bad4u, thanks for adding the existing SchemaStore schema. Would you mind updating it to use this generated version?

I'll go ahead and merge this now as the change should be rather uncontroversial. :)

@mre mre changed the title fix: align generated config schema with accepted values Auto-generate JSON schema from Rust types Oct 11, 2026
@mre
mre merged commit 5840437 into master Oct 11, 2026
7 of 8 checks passed
@mre
mre deleted the fix/config-schema-validation branch October 11, 2026 11:16
@Nick2bad4u

Copy link
Copy Markdown

This has been open for a while. I made a few more tweaks and switched to JSON Schema Draft 7, matching the draft used by the published SchemaStore schema.

I compared both schemas against lychee's config loader. They aren't identical: the generated schema covers newer options and follows the parser more closely in several places:

Configuration Published schema Generated schema lychee
cache_location = "cache.json" Rejects Accepts Accepts
Per-host accept = [200, 429] Rejects Accepts Accepts
verbose = "Warning" Rejects Accepts Accepts
generate = "config-schema" Rejects Accepts Accepts
accept = "banana" Accepts Rejects Rejects
method = "GET POST" Accepts Rejects Rejects
basic_auth = ["user:password"] Accepts Rejects Rejects
accept = "200..1000" Accepts Accepts Accepts

The comparison also caught a bug in our handling of exclusive range endpoints, which is now fixed. Some checks still belong to the parser: for example, both schemas accept "500..400", but lychee rejects the reversed range.

The generated file will be at docs/schema.json, with a test to keep it in sync with the Rust configuration types. It will also be included in release archives. This finally resolves the request for a versioned schema in #1382.

@Nick2bad4u, thanks for adding the existing SchemaStore schema. Would you mind updating it to use this generated version?

I'll go ahead and merge this now as the change should be rather uncontroversial. :)

Which url do you want to use? The raw.github @ main branch since it'll pull directly from the url instead and update automatically?

@nobkd

nobkd commented Oct 11, 2026

Copy link
Copy Markdown

Maybe github has something like https://raw.githubusercontent.com/lycheeverse/lychee/tags/nightly/docs/schema.json, but for the latest full release (instead of nightly tag)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants