Skip to content
Back to skills

Orpc Zod Smart Coercion

ASecurity

Refined alternative to z.coerce — automatically coerces inputs to the expected type without modifying the schema.

  • 8 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 12, 2026
researchexpressgitapiperformance

Works with

  • api

Security analysis

A96/100
  • mediumInstalls packages at runtime which could introduce malicious dependencies

Pro shows the line behind each finding and how to fix it

Scanned September 12, 2026

npx -y skills add ali-master/skills --skill orpc-zod-smart-coercion --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Orpc Zod Smart Coercion?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Orpc Zod Smart Coercion
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ali-master-orpc-zod-smart-coercion/badge)](https://www.skillsdirectory.com/skills/ali-master-orpc-zod-smart-coercion)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: oRPC Zod Smart Coercion
description: Refined alternative to z.coerce — automatically coerces inputs to the expected type without modifying the schema.
license: MIT
metadata:
  author: Ali Torki
  homepage: https://github.com/ali-master
  version: "1.0.0"
---

# Zod Smart Coercion

A plugin alternative to `z.coerce` that automatically converts inputs to the expected type without modifying the input schema.

> In Zod v4, this plugin only supports **discriminated unions**. Regular unions are not coerced.

## Installation

```sh
npm install @orpc/zod@latest
```

## Setup

```ts
import { OpenAPIHandler } from '@orpc/openapi/fetch'
import { ZodSmartCoercionPlugin } from '@orpc/zod' // Zod v3
import { experimental_ZodSmartCoercionPlugin as ZodSmartCoercionPlugin } from '@orpc/zod/zod4' // Zod v4

const handler = new OpenAPIHandler(router, {
  plugins: [new ZodSmartCoercionPlugin()],
})
```

> Do not use with [RPCHandler](/docs/rpc-handler) — it may negatively impact performance.

## Safe and Predictable Conversion

Coercion only happens when:

1. The schema expects a specific type and the input can be converted.
2. The input does not already match the schema.

For example:
- If input is `'true'` but the schema does not expect a boolean — no conversion.
- If the schema accepts both boolean and string — `'true'` is **not** coerced to a boolean.

## Conversion Rules

| Source                              | Target | Examples                                                       |
| ----------------------------------- | ------ | -------------------------------------------------------------- |
| `'true'`, `'on'`, `'t'`             | `true` | Boolean                                                        |
| `'false'`, `'off'`, `'f'`           | `false` | Boolean                                                       |
| `'42'`                              | `42`   | Number                                                         |
| `'12345678901234567890'`            | `BigInt` | BigInt                                                       |
| `'2024-11-27T00:00:00.000Z'`        | `Date` | ISO date/datetime                                              |
| `'/^abc$/i'`                        | `RegExp` | Regular expression                                           |
| `'https://example.com'`             | `URL`  | URL object                                                     |
| `['a', 'b', 'a']`                   | `Set`  | Deduped Set                                                    |
| `[['k1', 'v1'], ['k2', 'v2']]`      | `Map`  | Map from key/value pairs                                       |

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.