Effect CommunityEC
Effect Community8mo ago
14 replies
Huge Letters

Declare schema does encoding twice which gives runtime errors

I use a declare schema because I do the all of the decoding from string myself but the encoding I can just delegate to Schema module - I don't want to use a transform because then I'm gonna be validating data twice basically: during my decoding and then Schema itself during a type-side transform.

So here's a playground with a reproduction
https://effect.website/play#c1509f6421b3

in logs you may notice that for nested values encoding seems to trigger twice - first time it just encodes a HashMap to a HashMap(its weird, it should encode to an array of entries), and then the second time a HashMap to array of entries(correct).

Using MapSchema direclty instead of a schema parameter from encode callback gives an error because once again for whatever reason it tries to encode twice - but this time it encodes correctly the first time(to array of entries) but fails second time because it doesn't expect an array of entries.

The end result I wanna have is to uncomment line 53 because what I want is to be able to encode a HasMap to a string - but this also gives me a runtime error because despite what the type claims (I get types may be tricky here since it's recursive), encoded can be either a HashMap(doesn't have map method so crashes) or an array of entries(what it always should be)
Effect Documentation
Was this page helpful?