Skip to content

feat: read path parameters as integers without a schema - #153

Merged
Ya-hari merged 4 commits into
mainfrom
feat/int-path-params
Nov 17, 2026
Merged

Ya-hari merged 4 commits into
mainfrom
feat/int-path-params

Conversation

@Ya-hari

@Ya-hari Ya-hari commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

何を・なぜ / What and why

パスの値を、スキーマなしで整数として読めるようにします。今までは :id がいつも文字列だったので、ハンドラーごとに「整数か確かめて、違えば 404」を書くか、Zod などを足す必要がありました。

app.get("/notes/:id", { params: { id: "uint" } }, ({ id }) => {
    // id: number
});
  • "int" は整数(負の数も含む)、"uint" は 0 以上の整数です。書いた名前だけが number になり、ほかのパラメーターは文字列のままです
  • 数になるのはハンドラーの 1 つ目の引数だけです。c.params は型(Record<string, string>)のとおり、文字列のまま残します
  • 整数とみなすのは、^(0|-?[1-9][0-9]*)$ に合い、Number.isSafeInteger() が真になるものだけです
    • 相談のときの ^-?(0|[1-9][0-9]*)$ だと -0 が通るので、弾くリストに合わせて -0 を外した形にしました
  • 整数でなければハンドラーは呼ばず、404 にします(app.notFound のハンドラーに回るので、c.notFound() と同じです)。スキーマが通らなかったときの 400 とは分けています
  • 開発モードの 404 では、どの名前に何が来たかを出します: :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) と、スキーマを付けた形は変えていません。追加の機能です

弾く値(ドキュメントの表とテストに同じものを載せています)

01 00 1.5 1.0 " 1 " +1 -0 1e3 0x10 1_000 9007199254740992 -9007199254740992 1(全角) abc - Infinity NaN。"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 が通る / pass
  • 新しい機能や修正にテストを付けた / Added tests for new features or fixes(test/param-types.test.ts、test/types.check.ts、test/docs)
  • ui/ を変えたなら bun run build:pages の結果も含めた / Included bun run build:pages output if ui/ changed(ui/ は変えていません)
  • 公開の API や挙動を変えるなら、先に Issue で相談した / Discussed in an issue first if this changes public APIs or behavior(パスの値を、スキーマなしで整数として読む #142)
  • 性能に関わるなら、bench/ の前後の数字と条件を書いた / Added before/after bench/ numbers and conditions if performance-related(スキーマのないルートの道は変えていません)

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
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

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

@Ya-hari
Ya-hari merged commit 887d7ac into main Nov 17, 2026
32 checks passed
@Ya-hari
Ya-hari deleted the feat/int-path-params branch November 17, 2026 12:51
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.

パスの値を、スキーマなしで整数として読む

1 participant