Silgi
Plugins

Smart Coercion

Automatically convert string query parameters to numbers, booleans, and null.

When procedures receive GET requests, all values arrive as strings from the query string. The coercion guard converts them to proper JavaScript types before validation runs.

Usage

import {  } from 'silgi/plugins'

const  = k
  .$use()
  .$input(z.object({ : z.number(), : z.boolean().optional() }))
  .$resolve(({  }) => db.users.find(.id))

// GET /users/get?data={"id":"42","active":"true"}
// Without coercion: validation fails (string "42" is not a number)
// With coercion: input becomes { id: 42, active: true }

Coercion rules

Input stringCoerced value
"123", "-42", "3.14"number
"true"true
"false"false
"null"null
"undefined"undefined
"" (empty)undefined
anything elsekept as string

Numbers are only coerced when the entire string is a valid number and String(Number(s)) === s. This prevents "01" or "1e308" from being coerced unexpectedly.

Standalone use

The coercion functions are also exported for use outside of guards:

import { ,  } from 'silgi/plugins'

('42') // 42
('true') // true
('hello') // "hello"

const  = { : '42', : 'Alice', : 'true' }
()
// { id: 42, name: "Alice", active: true }

Coercion processes top-level values, nested objects (one level deep), and array elements. It does not recurse into deeply nested structures.

What's next?

  • Procedures — input validation with Zod/Valibot/ArkType
  • Plugins — other available plugins

On this page