Skip to content

breaking: remove param files in folder in favor of params.js/ts file#16189

Open
dummdidumm wants to merge 25 commits into
version-3from
matcher-params-file
Open

breaking: remove param files in folder in favor of params.js/ts file#16189
dummdidumm wants to merge 25 commits into
version-3from
matcher-params-file

Conversation

@dummdidumm

Copy link
Copy Markdown
Member

Instead of having to declare one param matcher per file, you now declare all of them within one file. A defineParams function both helps with declaring them in a type-safe way and also normalizes the property values: You can either parse a standard schema object, or a function that either throws or returns the parsed value (if you don't want to bring in a schema library).

closes #10407

Kudos to @phi-bre for suggesting this in #16148 (comment)

dummdidumm and others added 7 commits June 23, 2026 22:05
basically implements #10407 (comment):
- new `parse` function can be exported from `params/someMatcher.js`
- can be either alongside `match` or standalone
- if it throws it counts as invalid and the route will not be matched

closes #10407
match can now also be a standard schema. If it parses you get the transformed value. If it throws it counts as invalid and the route will not be matched.

closes #10407
…`'~standard'` property, so callable Standard Schemas (e.g. ArkType) are misclassified as plain predicate functions, breaking validation and transformation.

This commit fixes the issue reported at packages/kit/src/utils/routing.js:139

## Bug

`ParamMatcher` is typed as `((param: string) => boolean) | StandardSchemaV1<string, any>`. Several headline Standard Schema implementations expose their schema as a **callable function that also carries a `'~standard'` property**. ArkType is the canonical example:

```js
const t = type('string.numeric.parse');
t('42');            // → 42 (parsed value, or an ArkErrors object on failure)
t['~standard'].validate('42'); // also available
```

In the original `run_matcher`, the `typeof matcher === 'function'` branch comes first:

```js
if (typeof matcher === 'function') {
	return matcher(value) ? { success: true, value } : { success: false };
}
if ('~standard' in matcher) { ... }
```

Because an ArkType schema is a function, it never reaches the `'~standard'` branch. Instead `matcher(value)` is called and its return value treated as a boolean:

- **Invalid input**: ArkType returns a truthy `ArkErrors` object → treated as a *successful* match, and the raw, untransformed string is stored as the param value. The route matches when it should not.
- **Valid input that transforms to a falsy value** (e.g. `0`, empty string): the truthiness check fails → the route is incorrectly rejected.

So both validation and transformation are broken for callable Standard Schemas.

## Fix

Reorder the checks so `'~standard'` is tested first. `'~standard' in matcher` works on functions too (functions are objects), so a callable Standard Schema is now routed through the proper `validate` path. Plain predicate functions lack the `'~standard'` property and correctly fall through to the function branch.

```js
function run_matcher(matcher, value) {
	if ('~standard' in matcher) {
		const result = matcher['~standard'].validate(value);
		if (result instanceof Promise || result.issues) {
			return { success: false };
		}
		return { success: true, value: result.value };
	}

	if (typeof matcher === 'function') {
		return matcher(value) ? { success: true, value } : { success: false };
	}

	return { success: false };
}
```

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: dummdidumm <sholthausen@web.de>
Instead of having to declare one param matcher per file, you now declare all of them within one file. A `defineParams` function both helps with declaring them in a type-safe way and also normalizes the property values: You can either parse a standard schema object, or a function that either throws or returns the parsed value (if you don't want to bring in a schema library).

closes #10407

Kudos to @phi-bre for suggesting this in #16148 (comment)
@changeset-bot

changeset-bot Bot commented Jun 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6067b3a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot

Copy link
Copy Markdown

Comment thread packages/kit/src/utils/params.js
Comment thread packages/kit/src/core/sync/write_types/index.js
vercel Bot and others added 3 commits June 26, 2026 22:50
…cing the invalid path `src/params/.js` instead of `src/params.js`.

This commit fixes the issue reported at packages/kit/src/core/sync/write_types/index.js:605

## Bug

In `generate_params_type` (`write_types/index.js:606`) and the identical logic in `write_non_ambient.js:101`, the fallback used when `resolve_entry` returns `null` was:

```js
path.join(config.kit.files.params.replace(/.(js|ts)$/, ''), '.js');
```

`config.kit.files.params` defaults to `path.join(files.src, 'params')` → `src/params` (no extension). The `.replace(/.(js|ts)$/, '')` is a no-op there, and `path.join('src/params', '.js')` inserts a path separator, yielding `src/params/.js`. Even when the config value has an extension (e.g. `src/params.ts`), the replace strips it to `src/params` and `path.join` again yields `src/params/.js`.

Verified with Node:

```
$ node -e "console.log(require('path').join('src/params', '.js'))"
src/params/.js
```

### Trigger / impact

When a route references a matcher but `resolve_entry(config.kit.files.params)` returns `null` (params file not found at resolution time), the generated `$types`/app-types contain an import from the broken path `src/params/.js` instead of `src/params.js`, producing invalid type imports.

## Fix

Replaced `path.join(base, '.js')` with string concatenation `base + '.js'` in both locations:

```js
config.kit.files.params.replace(/.(js|ts)$/, '') + '.js';
```

Verified output is now `src/params.js` for both `src/params` and `src/params.ts` inputs.

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: dummdidumm <sholthausen@web.de>
@dummdidumm dummdidumm marked this pull request as draft June 26, 2026 22:55
@adiguba

adiguba commented Jun 28, 2026

Copy link
Copy Markdown

Hello,

Two remarks.

  • Did the resolve() will be correctly typed ?

For example if I have this params :

export const params = defineParams({
  details: v.picklist(['min', 'standard', 'max']);
});

I think this should be typed accordingly :

resolve('/info/[details=details]', { details: 'min' }); // OK
resolve('/info/[details=details]', { details: 'minimum' }); // Type Error
  • It would be nice to have an optional format() method to complement parse().
    This method would be used by resolve() to build the URL

For example, if I have bigint as uid, and I want to use it as hexadecimal on the URL :

export const params = defineParams({
  uid: {
    // parse the uid as a hexa BigInt :
    parse: v.pipe(v.string(),v.transform((value) => BigInt(`0x${value}`)),
    // format the BigInt in hexa
    format: (value) => value.toString(16)
   }
});

@Conduitry Conduitry changed the title breaking: remove param files in folder in favor or params.js/ts file breaking: remove param files in folder in favor of params.js/ts file Jun 29, 2026
Comment thread .changeset/ten-lands-send.md Outdated
Co-authored-by: Conduitry <git@chor.date>
@dummdidumm dummdidumm marked this pull request as ready for review June 29, 2026 20:22
@dummdidumm dummdidumm marked this pull request as draft June 29, 2026 20:22
@dummdidumm dummdidumm marked this pull request as ready for review June 30, 2026 10:31
@sacrosanctic

Copy link
Copy Markdown
Contributor

Food for thought, param.ts in singular form.

Though prior art is mixed:

  • /params/[matcher].ts
  • env.ts
  • routes
  • hooks.ts

@sirkostya009

sirkostya009 commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

this is better than what we currently got. one question though, will this feature be backported to kit v2 behind an experimental flag? docs dont mention anything about that

@dummdidumm

Copy link
Copy Markdown
Member Author

no there are no plans to backport this at the moment

@pkg-svelte-dev

pkg-svelte-dev Bot commented Jul 1, 2026

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from 6067b3a:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/6067b3a96161b9ceec13693f9c91aeaed6f3ff6f

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16189

@Rich-Harris

Copy link
Copy Markdown
Member

param.ts in singular form.

that wouldn't really make sense. hooks, routes, params are all plural. env isn't, because it's describing the environment as a whole, rather than multiple environments

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Suggestion:

A throwing param matcher breaks client-side navigation (silent no-op / unhandled promise rejection) even though the server handles the same throw gracefully, creating a server/client asymmetry introduced by commit f5479de.

Fix on Vercel

@dummdidumm

dummdidumm commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

Vercel is right, but I don't see a good way how we can sensibly handle this. We could fall back to a full page reload to get the better error message eventually that way.

// in function navigate(...)
	try {
		intent ??= await get_navigation_intent(url, false);
	} catch {
		// Likely a matcher threw unexpectedly. Fall back to the server to get a proper error page.
		return await native_navigation(url, replace_state);
	}

Anything more would mean polluting the whole navigation code for such an edge case and that is not worth it. It should never happen anyway.

Comment thread documentation/docs/30-advanced/10-advanced-routing.md Outdated
Comment thread packages/kit/src/exports/params.js Outdated
Comment thread packages/kit/src/runtime/server/respond.js Outdated
Comment thread packages/kit/src/utils/routing.js Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants