Skip to content

Commit ea8e3e2

Browse files
committed
feat: enable standard schema param parsers
Close #2660 Only works if the param can be implicitly casted to a String
1 parent 7f32e99 commit ea8e3e2

File tree

17 files changed

+322
-22
lines changed

17 files changed

+322
-22
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@
7575
"onlyBuiltDependencies": [
7676
"chromedriver",
7777
"esbuild",
78-
"geckodriver"
78+
"geckodriver",
79+
"simple-git-hooks"
80+
],
81+
"ignoredBuiltDependencies": [
82+
"@nightwatch/nightwatch-inspector"
7983
]
8084
}
8185
}

packages/playground-file-based/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"semver": "^7.7.3",
14+
"valibot": "^1.3.1",
1415
"vue": "3.6.0-beta.5",
1516
"vue-router": "workspace:*",
1617
"zod": "^4.3.6"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts" setup>
2+
import { useRoute } from 'vue-router'
3+
4+
const route = useRoute()
5+
</script>
6+
7+
<template>
8+
<h1>Month (valibot): {{ route.params.month }}</h1>
9+
<pre>typeof: {{ typeof route.params.month }}</pre>
10+
</template>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script lang="ts" setup>
2+
import { useRoute } from 'vue-router'
3+
4+
definePage({
5+
params: {
6+
query: {
7+
mm: {
8+
parser: 'month-zod',
9+
default: 0,
10+
},
11+
},
12+
},
13+
})
14+
15+
const route = useRoute()
16+
</script>
17+
18+
<template>
19+
<h1>Month (zod): {{ route.params.month }}</h1>
20+
<pre>typeof: {{ typeof route.params.month }}</pre>
21+
<pre>{{ route.params }}</pre>
22+
</template>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as v from 'valibot'
2+
3+
const MonthSchema = v.pipe(
4+
v.string(),
5+
v.transform(Number),
6+
v.integer(),
7+
v.minValue(1),
8+
v.maxValue(12)
9+
)
10+
11+
export const parser = MonthSchema
12+
13+
// export const parser = defineParamParser({
14+
// get: value => {
15+
// const result = v.safeParse(MonthSchema, value)
16+
// if (!result.success) {
17+
// miss()
18+
// }
19+
// return result.output
20+
// },
21+
// set: value => String(value),
22+
// })
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineParamParser } from 'vue-router/experimental'
2+
import { z } from 'zod'
3+
4+
const MonthSchema = z.coerce.number().int().min(1).max(12)
5+
export const parser = MonthSchema
6+
7+
// export const parser = defineParamParser({
8+
// get: (value): number => {
9+
// return MonthSchema.parse(value)
10+
// },
11+
// set: (value: number): string => String(value),
12+
// })

packages/playground-file-based/src/routes.d.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,24 @@ import type {
1414
ParamValueZeroOrMore,
1515
ParamValueZeroOrOne,
1616
} from 'vue-router'
17+
import type {
18+
_ExtractParamParserType,
19+
} from 'vue-router/experimental
1720
1821
// Custom route params parsers
19-
type Param_date = ReturnType<NonNullable<typeof import('./params/date.ts').parser['get']>>
20-
type Param_npmOrg = ReturnType<NonNullable<typeof import('./params/npm-org.ts').parser['get']>>
21-
type Param_semver = ReturnType<NonNullable<typeof import('./params/semver.ts').parser['get']>>
22-
type Param_versionRange = ReturnType<NonNullable<typeof import('./params/version-range.ts').parser['get']>>
22+
type Param_date = _ExtractParamParserType<typeof import('./params/date.ts').parser>
23+
type Param_monthValibot = _ExtractParamParserType<typeof import('./params/month-valibot.ts').parser>
24+
type Param_monthZod = _ExtractParamParserType<typeof import('./params/month-zod.ts').parser>
25+
type Param_npmOrg = _ExtractParamParserType<typeof import('./params/npm-org.ts').parser>
26+
type Param_semver = _ExtractParamParserType<typeof import('./params/semver.ts').parser>
27+
type Param_versionRange = _ExtractParamParserType<typeof import('./params/version-range.ts').parser>
2328

2429
declare module 'vue-router' {
2530
interface TypesConfig {
2631
ParamParsers:
2732
| 'date'
33+
| 'month-valibot'
34+
| 'month-zod'
2835
| 'npm-org'
2936
| 'semver'
3037
| 'version-range'
@@ -162,6 +169,20 @@ declare module 'vue-router/auto-routes' {
162169
Record<never, never>,
163170
| never
164171
>,
172+
'/months/valibot-[month=month-valibot]': RouteRecordInfo<
173+
'/months/valibot-[month=month-valibot]',
174+
'/months/valibot-:month',
175+
{ month: Exclude<Param_monthValibot, unknown[]> },
176+
{ month: Exclude<Param_monthValibot, unknown[]> },
177+
| never
178+
>,
179+
'/months/zod-[month=month-zod]': RouteRecordInfo<
180+
'/months/zod-[month=month-zod]',
181+
'/months/zod-:month',
182+
{ month: Exclude<Param_monthZod, unknown[]>, mm?: Exclude<Param_monthZod, unknown[]> },
183+
{ month: Exclude<Param_monthZod, unknown[]>, mm: Exclude<Param_monthZod, unknown[]> },
184+
| never
185+
>,
165186
'/nested/': RouteRecordInfo<
166187
'/nested/',
167188
'/nested',
@@ -391,6 +412,18 @@ declare module 'vue-router/auto-routes' {
391412
views:
392413
| never
393414
}
415+
'src/pages/months/valibot-[month=month-valibot].vue': {
416+
routes:
417+
| '/months/valibot-[month=month-valibot]'
418+
views:
419+
| never
420+
}
421+
'src/pages/months/zod-[month=month-zod].vue': {
422+
routes:
423+
| '/months/zod-[month=month-zod]'
424+
views:
425+
| never
426+
}
394427
'src/pages/nested/_parent.vue': {
395428
routes:
396429
| '/nested/'

packages/router/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
"@rollup/plugin-node-resolve": "^16.0.3",
169169
"@rollup/plugin-replace": "^6.0.3",
170170
"@rollup/plugin-terser": "^0.4.4",
171+
"@standard-schema/spec": "^1.1.0",
171172
"@types/babel__generator": "^7.27.0",
172173
"@types/nightwatch": "^2.3.32",
173174
"@types/picomatch": "^4.0.2",
@@ -192,8 +193,10 @@
192193
"rollup-plugin-typescript2": "^0.36.0",
193194
"tsdown": "^0.20.1",
194195
"tsup": "^8.5.1",
196+
"valibot": "^1.3.1",
195197
"vite": "^7.3.0",
196-
"vue": "3.6.0-beta.2"
198+
"vue": "3.6.0-beta.2",
199+
"zod": "^4.3.6"
197200
},
198201
"peerDependencies": {
199202
"@pinia/colada": ">=0.21.2",

packages/router/src/experimental/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export {
4242
defineParamParser,
4343
definePathParamParser,
4444
defineQueryParamParser,
45+
normalizeParamParser as _normalizeParamParser,
46+
type ExtractParamParserType as _ExtractParamParserType,
4547
} from './route-resolver/matchers/param-parsers'
4648

4749
export { miss, MatchMiss as _MatchMiss } from './route-resolver/matchers/errors'

packages/router/src/experimental/route-resolver/matchers/param-parsers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type { ParamParser } from './types'
44
export { PARAM_PARSER_BOOL } from './booleans'
55
export { PARAM_PARSER_INT } from './integers'
66
export { PARAM_PARSER_STRING } from './strings'
7+
export { normalizeParamParser } from './standard-schema'
8+
export type { ExtractParamParserType } from './standard-schema'
79

810
export const PATH_PARAM_SINGLE_DEFAULT: ParamParser<string, string> = {}
911

0 commit comments

Comments
 (0)