Repository navigation
feat: read path parameters as integers without a schema - #153
Merged
Merged
Conversation
Routes can declare `{ params: { id: "int" } }` or `{ params: { id: "uint" } }`
instead of a Standard Schema. The handler's first argument then has those
names as `number`; other parameters stay strings.
A value counts as an integer only if it matches ^(0|-?[1-9][0-9]*)$ and
Number.isSafeInteger() is true ("uint" also requires 0 or more). Anything
else ("01", "1.5", " 1 ", "+1", "1e3", "-0", values beyond
Number.MAX_SAFE_INTEGER, ...) is not converted: the handler is not called
and the response is 404 through the app.notFound handler. In development
mode the 404 body says which parameter got which value.
- Names not in the path are a type error (app methods, createRouter) and
are refused at registration in development mode
- Values other than "int" / "uint" are refused at registration
- openAPI() emits `integer` (`minimum: 0` for "uint")
Closes #142
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
senri | afa694e | Commit Preview URL Branch Preview URL |
Nov 17 2026, 11:34 AM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
何を・なぜ / What and why
パスの値を、スキーマなしで整数として読めるようにします。今までは
:idがいつも文字列だったので、ハンドラーごとに「整数か確かめて、違えば 404」を書くか、Zod などを足す必要がありました。"int"は整数(負の数も含む)、"uint"は 0 以上の整数です。書いた名前だけがnumberになり、ほかのパラメーターは文字列のままですc.paramsは型(Record<string, string>)のとおり、文字列のまま残します^(0|-?[1-9][0-9]*)$に合い、Number.isSafeInteger()が真になるものだけです^-?(0|[1-9][0-9]*)$だと-0が通るので、弾くリストに合わせて-0を外した形にしましたapp.notFoundのハンドラーに回るので、c.notFound()と同じです)。スキーマが通らなかったときの 400 とは分けています:id に 0 以上の整数でない値 "abc" が来ました(...)"int"・"uint"以外の値: 型のエラーになり、登録のときも止めます(本番でも)app.get()などとcreateRouter()では型のエラーになります。開発モードでは、app.routes()の表も登録のときに止めます。それ以外の道を通ってきた場合は、リクエストのときに 500 にします(黙って通しません)query・bodyのスキーマと一緒に使えます。createRouter()・app.on()・endpoint()でも同じように書けますopenAPI()ではintegerとして出します("uint"にはminimum: 0も付けます)app.get(path, handler)と、スキーマを付けた形は変えていません。追加の機能です弾く値(ドキュメントの表とテストに同じものを載せています)
01001.51.0" 1 "+1-01e30x101_0009007199254740992-90071992547409921(全角)abc-InfinityNaN。"uint"では、これに加えて負の数も弾きます。大きさ
bun run size(hi のアプリ、minify・gzip): この PR の分は +222 B ほど。#152 を含む main を取り込んだあとで 7,573 B(上限 8,192 B まで残り 619 B)。ドキュメント
validation(日英): 「スキーマなしで、パスの値を整数として読む」の節(境界の表、404 のときの動き)routing/routes(日英): ハンドラーの引数の説明と、登録のときのエラーの説明から案内していますverifyのしるしを付けて、test/docsで確かめています関連する Issue / Related issues
Closes #142
確認 / Checklist
bun test・bun run test:node・bun run typecheck・bun run smokeが通る / passtest/param-types.test.ts、test/types.check.ts、test/docs)ui/を変えたならbun run build:pagesの結果も含めた / Includedbun run build:pagesoutput ifui/changed(ui/は変えていません)bench/の前後の数字と条件を書いた / Added before/afterbench/numbers and conditions if performance-related(スキーマのないルートの道は変えていません)