Fork me on GitHub
#clojurescript
<
2022-11-18
>
Dallas Surewood07:11:30

So some people seem to be sharing specs between clj/cljs. They define their specs in a cljc file and validate on both sides. What I'm confused about is how they combine this with namespaced keys. If my spec has namespaced keys, and I want to send a form from the client that has those same keys, the cljs-http chops off the namespaced part before sending it to the backend. Now I can't validate whatever was sent because it's invalid without the namespace

cjohansen07:11:02

You have a few options: 1. Send the data as EDN or Transit if you make the POST yourself (e.g. with js/fetch or similar), that will maintain the namespaces. 2. Implement a server-side handler that receives data from a specific form and translates the data to the fully qualified data model. 3. Use :req-un and :opt-un to use the specs on unqualified keys, but that doesn’t work if there are collisions, and you loose the benefit of namespaces, so investigate the two other options first.

cjohansen07:11:13

(and possibly others as well)

Dallas Surewood07:11:49

Thanks, I'll probably just go with edn for now

cjohansen08:11:23

That would be my go to as well 😊

Rupert (All Street)09:11:56

If you have lots of data then transit is much faster than EDN and is a 100% compatible drop in replacement (no code change required - so you can do it later when needed). Main downside is Transit is less human readable - but can still be sort of read by humans in small quantities.

p-himik10:11:07

Yeah, I always go with Transit in such cases. And Transit can be human-readable if you disable compression by using the :json-verbose format.

localshred15:11:50

+1 for using transit for serialization speed/compactness while preserving all the clj native data structure goodies

rgm23:11:18

I've generally found shipping transit between clojurescript frontends and clojure backends pretty great except for one odd gotcha around UUIDs: https://github.com/cognitect/transit-cljs/pull/10 Not entirely sure whether the status of that one has changed but it's a relatively trivial trip hazard to deal with once you know about it.