Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/callable-param-matchers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: don't treat callable standard schemas as function param matchers
13 changes: 9 additions & 4 deletions packages/kit/src/exports/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ export function defineParams(definitions) {
* @returns {import('@sveltejs/kit').ParamMatcher}
*/
export function normalize_param_definition(definition) {
// standard schemas can be callable (e.g. ArkType), so this must be checked before the function case
if (
definition &&
(typeof definition === 'object' || typeof definition === 'function') &&
'~standard' in definition
) {
return definition;
}

if (typeof definition === 'function') {
return /** @type {import('@sveltejs/kit').ParamMatcher} */ (
/** @type {unknown} */ ({
Expand All @@ -55,9 +64,5 @@ export function normalize_param_definition(definition) {
);
}

if (definition && typeof definition === 'object' && '~standard' in definition) {
return definition;
}

throw new Error('Invalid param definition');
}
20 changes: 20 additions & 0 deletions packages/kit/src/utils/params.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,23 @@ test('normalize_param_definition propagates thrown errors', () => {

assert.throws(() => matcher['~standard'].validate('x'), /boom/);
});

test('normalize_param_definition passes callable standard schemas through untouched', () => {
// standard schemas can be callable (e.g. ArkType)
const callable = /** @type {import('@sveltejs/kit').ParamMatcher} */ (
/** @type {unknown} */ (
Object.assign(() => 'from-call', {
'~standard': {
version: 1,
vendor: 'test',
validate: () => ({ value: 'from-schema' })
}
})
)
);

const matcher = normalize_param_definition(callable);

assert.equal(matcher, callable);
assert.deepEqual(matcher['~standard'].validate('x'), { value: 'from-schema' });
});
Loading