oRPC Smart Coercion Plugin
Automatically converts input values to match schema types without manual coercion logic.
SKILL.mdskills/orpc-smart-coercion/SKILL.md
GitHubSmart Coercion Plugin
Automatically converts input values to match schema types.
Impacts performance. For high-performance applications, manually define coercion in your schema validation.
Installation
npm install @orpc/json-schema@latest
Setup
import { OpenAPIHandler } from '@orpc/openapi/fetch'
import { SmartCoercionPlugin } from '@orpc/json-schema'
const handler = new OpenAPIHandler(router, {
plugins: [
new SmartCoercionPlugin({
schemaConverters: [
new ZodToJsonSchemaConverter(),
],
})
]
})
How It Works
- Schema-guided — Only converts when the schema specifies type
- Safe only — Only safe conversions like
'123'to123 - Keep original — Unsafe conversions keep the original value
- Smart unions — Picks the best for union types
- Deep conversion — Works inside nested structures
Native Type Support
For non-JSON-native types, oRPC uses x-native-type metadata:
x-native-type: 'bigint'for BigIntx-native-type: 'date'for Datex-native-type: 'regexp'for RegExpx-native-type: 'url'for URLx-native-type: 'set'for Setx-native-type: 'map'for Map
Conversion Rules
String → Boolean
'true','on'→true'false','off'→false
String → Number
'123'→123'3.14'→3.14
String/Number → BigInt
'12345678901234567890'→12345678901234567890n
String → Date
- ISO format:
'2023-10-01','2020-01-01T06:15:00Z', etc.
String → RegExp
'/^\\d+$/i'→new RegExp('^\\d+$', 'i')
String → URL
'https://example.com'→new URL('https://example.com')
Array → Set
['apple', 'banana']→new Set(['apple', 'banana'])
Array → Object
['apple', 'banana']→{ 0: 'apple', 1: 'banana' }
Array → Map
[['key1', 'value1'], ['key2', 'value2']]→new Map(...)