Titouan Créac'hT
tRPC2y ago
1 reply
Titouan Créac'h

Custom data transformer and input/output schema

Hello 👋

I would like to code a data transformer, but I would this data transformer to be aware of the input and output Schema of the procedure.

why:

My typical use case is sending data type that are not serializable directly. I use effect-ts .
So let's take a Schema from @effect/schema:

const MySchema = Schema.Struct({
  optionalValue: Schema.Option(Schema.String)
});


...

myProcedure: protectedProcedure
  .input(S.decodeUnknownSync(MySchema)) // validating and decoding
  .mutation(async ({ ctx, input }) => {
     // ... 
  })


With effect-ts schema can be decoded and encoded.

As we see in the example: data that are sent to myProdedure are decoded.
But thoses data are not encoded in the client.

|                  | Encoded                     | Decoded | 
|------------------|-----------------------------|---------|
| Client to Server | no                          | yes     |        
| Server to Client | no (possible with .output)  | no      | 
|                  |                             |         | 


This is also a problem with the types that can be encoded and decoded to another type:

const Schema = S.NumberFromString 


in this case, the typescript type inferred would be number but in my client side, I should send a
string
to the mutation, so it doesn't match.

So to solve all my problem, I can encode automatically so that: I would send a number from my mutation, that would be encoded to a string, that would be
decoded to a number, and everything would be perfect.

I've read the doc about data transformer, this is probably what I need, but I need to get the input / output schema to makes this working and this is not possible.

Do you have an idea, or a solution on how to achieve that ?

Thanks in advance 🙏
Was this page helpful?