Smart Conversion
Effortlessly adapt your input data to the expected type using oRPC's Smart Conversion feature.
Important: If you're using the oRPC client, you don't need this feature. The oRPC client has a built-in mechanism for handling data serialization and deserialization between the client and server, ensuring seamless type transformations.
Overview
Smart Conversion is a powerful feature in oRPC that ensures incoming string-based data is automatically transformed into the expected types based on your Zod schema. This eliminates the need for manual parsing, ensuring your application works seamlessly with correctly formatted data.
Safe and Predictable
Smart Conversion only applies transformations when:
- The incoming data can be converted.
- The schema explicitly expects the transformed type.
For example:
- Incoming data
'true'
is converted totrue
if the schema isz.boolean()
. - No conversion occurs if the schema is
z.boolean().or(z.string())
, as the input already matches the schema.
This ensures safe usage without data loss or unintended side effects.
Supported Conversions
The following types and transformations are supported:
Boolean
Strings that represent boolean values are converted as follows:
Supported values:
'true'
,'on'
,'t'
→true
'false'
,'off'
,'f'
→false
Number
Strings that can be safely parsed as numbers are converted:
BigInt
Strings representing valid bigint values are transformed:
Date
Valid date strings are converted into Date objects:
Supported formats:
- Full ISO date-time strings (
2024-11-27T00:00:00.000Z
) - Dates only (
2024-11-27
) - Times only (
19:00:00
)
RegExp
Strings representing regular expressions are converted into RegExp objects:
URL
Valid URL strings are converted into URL objects:
Set
Arrays of items are converted into Set
objects:
Map
Key-value pair arrays are converted into Map
objects: