|
| 1 | +--- |
| 2 | +title: Smart Coercion Plugin |
| 3 | +description: Automatically converts input values to match schema types without manually defining coercion logic. |
| 4 | +--- |
| 5 | + |
| 6 | +# Smart Coercion Plugin |
| 7 | + |
| 8 | +Automatically converts input values to match schema types without manually defining coercion logic. |
| 9 | + |
| 10 | +::: warning |
| 11 | +This plugin improves developer experience but impacts performance. For high-performance applications or complex schemas, manually defining coercion in your schema validation is more efficient. |
| 12 | +::: |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +::: code-group |
| 17 | + |
| 18 | +```sh [npm] |
| 19 | +npm install @orpc/json-schema@latest |
| 20 | +``` |
| 21 | + |
| 22 | +```sh [yarn] |
| 23 | +yarn add @orpc/json-schema@latest |
| 24 | +``` |
| 25 | + |
| 26 | +```sh [pnpm] |
| 27 | +pnpm add @orpc/json-schema@latest |
| 28 | +``` |
| 29 | + |
| 30 | +```sh [bun] |
| 31 | +bun add @orpc/json-schema@latest |
| 32 | +``` |
| 33 | + |
| 34 | +```sh [deno] |
| 35 | +deno install npm:@orpc/json-schema@latest |
| 36 | +``` |
| 37 | + |
| 38 | +::: |
| 39 | + |
| 40 | +## Setup |
| 41 | + |
| 42 | +Configure the plugin with [JSON Schema Converters](/docs/openapi/openapi-specification#generating-specifications) for your validation libraries. |
| 43 | + |
| 44 | +```ts |
| 45 | +import { OpenAPIHandler } from '@orpc/openapi/fetch' |
| 46 | +import { |
| 47 | + experimental_SmartCoercionPlugin as SmartCoercionPlugin |
| 48 | +} from '@orpc/json-schema' |
| 49 | + |
| 50 | +const handler = new OpenAPIHandler(router, { |
| 51 | + plugins: [ |
| 52 | + new SmartCoercionPlugin({ |
| 53 | + schemaConverters: [ |
| 54 | + new ZodToJsonSchemaConverter(), |
| 55 | + // Add other schema converters as needed |
| 56 | + ], |
| 57 | + }) |
| 58 | + ] |
| 59 | +}) |
| 60 | +``` |
| 61 | + |
| 62 | +## How It Works |
| 63 | + |
| 64 | +The plugin converts values **safely** using these rules: |
| 65 | + |
| 66 | +1. **Schema-guided:** Only converts when the schema says what type to use |
| 67 | +2. **Safe only:** Only converts values that make sense (like `'123'` to `123`) |
| 68 | +3. **Keep original:** If conversion is unsafe, keeps the original value |
| 69 | +4. **Smart unions:** Picks the best conversion for union types |
| 70 | +5. **Deep conversion:** Works inside nested objects and arrays |
| 71 | + |
| 72 | +::: info |
| 73 | +JavaScript native types such as BigInt, Date, RegExp, URL, Set, and Map are not natively supported by JSON Schema. To enable correct coercion, oRPC relies on the `x-native-type` metadata in your schema: |
| 74 | + |
| 75 | +- `x-native-type: 'bigint'` for BigInt |
| 76 | +- `x-native-type: 'date'` for Date |
| 77 | +- `x-native-type: 'regexp'` for RegExp |
| 78 | +- `x-native-type: 'url'` for URL |
| 79 | +- `x-native-type: 'set'` for Set |
| 80 | +- `x-native-type: 'map'` for Map |
| 81 | + |
| 82 | +The built-in [JSON Schema Converters](/docs/openapi/openapi-specification#generating-specifications) handle these cases (except for some experimental converters). Since this approach is not part of the official JSON Schema specification, if you use a custom converter, you may need to add the appropriate `x-native-type` metadata to your schemas to ensure proper coercion. |
| 83 | +::: |
| 84 | + |
| 85 | +## Conversion Rules |
| 86 | + |
| 87 | +### String → Boolean |
| 88 | + |
| 89 | +Support specific string values (case-insensitive): |
| 90 | + |
| 91 | +- `'true'`, `'on'` → `true` |
| 92 | +- `'false'`, `'off'` → `false` |
| 93 | + |
| 94 | +::: info |
| 95 | +HTML `<input type="checkbox">` elements submit `'on'` or `'off'` as values, so this conversion is especially useful for handling checkbox input in forms. |
| 96 | +::: |
| 97 | + |
| 98 | +### String → Number |
| 99 | + |
| 100 | +Support valid numeric strings: |
| 101 | + |
| 102 | +- `'123'` → `123` |
| 103 | +- `'3.14'` → `3.14` |
| 104 | + |
| 105 | +### String/Number → BigInt |
| 106 | + |
| 107 | +Support valid numeric strings or numbers: |
| 108 | + |
| 109 | +- `'12345678901234567890'` → `12345678901234567890n` |
| 110 | +- `12345678901234567890` → `12345678901234567890n` |
| 111 | + |
| 112 | +### String → Date |
| 113 | + |
| 114 | +Support ISO date/datetime strings: |
| 115 | + |
| 116 | +- `'2023-10-01'` → `new Date('2023-10-01')` |
| 117 | +- `'2020-01-01T06:15'` → `new Date('2020-01-01T06:15')` |
| 118 | +- `'2020-01-01T06:15Z'` → `new Date('2020-01-01T06:15Z')` |
| 119 | +- `'2020-01-01T06:15:00Z'` → `new Date('2020-01-01T06:15:00Z')` |
| 120 | +- `'2020-01-01T06:15:00.123Z'` → `new Date('2020-01-01T06:15:00.123Z')` |
| 121 | + |
| 122 | +### String → RegExp |
| 123 | + |
| 124 | +Support valid regular expression strings: |
| 125 | + |
| 126 | +- `'/^\\d+$/i'` → `new RegExp('^\\d+$', 'i')` |
| 127 | +- `'/abc/'` → `new RegExp('abc')` |
| 128 | + |
| 129 | +### String → URL |
| 130 | + |
| 131 | +Support valid URL strings: |
| 132 | + |
| 133 | +- `'https://example.com'` → `new URL('https://example.com')` |
| 134 | + |
| 135 | +### Array → Set |
| 136 | + |
| 137 | +Support arrays of **unique values**: |
| 138 | + |
| 139 | +- `['apple', 'banana']` → `new Set(['apple', 'banana'])` |
| 140 | + |
| 141 | +### Array → Object |
| 142 | + |
| 143 | +Converts arrays to objects with numeric keys: |
| 144 | + |
| 145 | +- `['apple', 'banana']` → `{ 0: 'apple', 1: 'banana' }` |
| 146 | + |
| 147 | +::: info |
| 148 | +This is particularly useful for [Bracket Notation](/docs/openapi/bracket-notation) when you need objects with numeric keys. |
| 149 | +::: |
| 150 | + |
| 151 | +### Array → Map |
| 152 | + |
| 153 | +Support arrays of key-value pairs: |
| 154 | + |
| 155 | +- `[['key1', 'value1'], ['key2', 'value2']]` → `new Map([['key1', 'value1'], ['key2', 'value2']])` |
0 commit comments