This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-12-22
Channels
- # adventofcode (35)
- # beginners (137)
- # braveandtrue (1)
- # calva (33)
- # cider (40)
- # cljsrn (4)
- # clojure (10)
- # clojure-spec (26)
- # clojure-uk (29)
- # clojurescript (18)
- # core-async (6)
- # cursive (1)
- # emacs (2)
- # figwheel-main (17)
- # fulcro (28)
- # jobs-discuss (4)
- # leiningen (1)
- # lumo (19)
- # off-topic (2)
- # om-next (1)
- # reitit (2)
- # rum (8)
- # spacemacs (19)
- # tools-deps (9)
- # yada (3)
How do I spec inside a reagent reaction or other container type that isn't a seq
I just googled and found https://stackoverflow.com/questions/37972074/how-to-clojure-spec-a-reference-type-like-atom which says "Don't", okay i get it but that is not compatible with performant UI programming
Has this been debated before?
You need to ask, is this hard to understand, do people wonder what the shape and structure of this thing is, if so, a spec can be a great way to make that easier
Or, if you specifically want to perform generative tests on a particular function that would benefit from it
Or if you need to validate user input, or validate data you're about to persist, etc.
Now to spec an atom, I mean, specs are just predicates. You can easilly do: #(s/int? @%)
And for atom, the other challenge is mutation. Your spec needs to either be the union of all possible valid values the atom will ever contain, throughout its many mutations
Or it has to be more generic, like validates that you expect the thing to be an atom and that's all
Or you need a way to know the particular context and time and what in that context and time the atom is supposed to contain
The validator idea is good also. Validators were designed to validate the data being set on an atom. So you can leverage spec to make the validation.
It's the ratom which is the incidental complexity, it is spiritually just data. For better or worse, Reagent UIs pass things that are data as reactions and it is what it is. But I was unable to get it to work in a nontrivial case
Can you show an example? Also, the ratom isn't a normal atom, and its reactive nature has a different hook for when the data would be updated and need to be validated. At least from what I know, I actually havn't used reagent
I have encountered errors when trying to use s/and
with s/or
as the first argument:
=> (s/explain
(s/and (s/or :int int? :double double?)
pos?)
3)
ClassCastException clojure.lang.MapEntry cannot be cast to java.base/java.lang.Number clojure.lang.Numbers.isPos (Numbers.java:96)
Using s/or
after the first argument appears to work though:
(s/explain
(s/and pos?
(s/or :int int? :double double?))
3)
Success!
Unfortunately, if you are trying to write (s/and (s/or ...) (s/or ...))
there doesn’t seem to be a workaround.
s/and will flow the conformed result so the and will be receiving a value like [:int pos?]
prob the best option here is to (s/or :int (s/and int? pos?) :double (s/and double? pos?))
ok, now that I read the doc, I see the part about “successively conformed values”.