Fork me on GitHub
#reagent
<
2016-01-13
>
dvcrn08:01:57

this might be the wrong place but someone here good with schema?

dvcrn08:01:41

I have a schema like this:

(def schema {:greeting s/Str
             :data [{:title s/Str
                     :images [{:url s/Str}]
                     :latitude s/Num
                     :longitude s/Num}]
             :loading? s/Bool
             :last-lat s/Num
             :last-long s/Num})
Now I want to tell it that a empty version of :data is ok too. Right now it throws invalid when I try to validate it

mitchelkuijpers08:01:21

@dvcrn:

(s/maybe [{:title s/Str
                     :images [{:url s/Str}]
                     :latitude s/Num
                     :longitude s/Num}])

dvcrn08:01:27

oh whoops I wanted to put that into #C073DKH9P 😄

dvcrn08:01:32

ah, let me try that

dvcrn08:01:38

hmm it still fails the validation

mitchelkuijpers08:01:47

That is weird because the docs of that func say "A value that must either be nil or satisfy schema"

dvcrn08:01:55

ok it was a derpy error. Forget everything I wrote simple_smile

hugobessaa14:01:55

hey people, anyone got a schema for reagent component?

mccraigmccraig14:01:16

what dyu mean @hugobessaa ? i have tonnes of components with schema for their inputs, but there's nothing special about those schema relating to reagent

jaen14:01:57

Yeah, reagent components are just functions, so you can just use the usual schema function validation stuff for it.

escherize14:01:16

maybe hugo means a schema that is hiccup-shaped? so it could validate

[:div]
[:div "hi"]
[:div {:style {:width "10px"} "hi"]
[:div [:div "hi"]]
[:div>span "hi"] 
etc

hugobessaa16:01:10

(s/defschema ComponentForm1
  [(s/one (s/conditional   keyword? s/Keyword
                           :else    js/Function) "comp")
   (s/maybe (s/conditional keyword? s/Keyword
                           string?  s/Str
                           vector?  (s/pred vector?)
                           map?     (s/pred map?)
                           :else    js/Function))])

;; TODO: validate if function uses schema Component
;; (s/defschema ComponentForm2
;;   js/Function)

;; TODO: validate if object is indeed a ReactElement
;; (s/defschema ComponentForm3
;;   js/Object)

(s/defschema Component
  (s/conditional vector? ComponentForm1
                 :else js/Function))

hugobessaa16:01:31

Don't know how to correctly implement ComponentForm2 nor ComponentForm3

jaen16:01:46

Ah duh, I've misunderstood you, sorry '

jaen16:01:40

But why not just:

(schema/defn some-component [prop :- PropSchema other-prop :- OtherPropSchema]
   ...)
?

hugobessaa16:01:33

(schema.core/defn some-component :- Component [prop :-PropSchema] ,,,)

jaen16:01:57

And then just set (schema/set-fn-validation! true)

hugobessaa16:01:18

And sometimes

hugobessaa16:01:41

(schema.core/defn container :- Component [child :- Component])

jaen16:01:58

Oh, I see.

jaen16:01:43

Hmm, I imagine you could try schema/=> that is the function schema to try to type that.

hugobessaa16:01:38

@jaen thanks, I will take a look at =>

jaen16:01:30

Form1 is of type props -> hiccup-vector, form2 is of type initial-props -> props -> hiccup-vector (a function returning a Form1 that is), Form3 is of type initial-props -> react-component, where hiccup-vector is possibly nested vectors that start with a symbol or any of those forms.

jaen16:01:38

But that seems it would get complex pretty fast.

jaen16:01:21

If you get it working then do share, I'll be interested to know how it ended up looking.

hugobessaa16:01:56

I'll improve my first solution as soon as I need form2 and form3

hugobessaa16:01:07

probably later today or tomorrow

hugobessaa16:01:20

I'll post here any improvements

hugobessaa17:01:14

I now need something for the output of reagent.core/reaction. Any ideas?

hugobessaa17:01:59

:- reagent.ratom/Reaction works just fine

jaen17:01:22

Was about to suggest IReactiveAtom but that would catch ratom as well.

hugobessaa18:01:38

how are you testing reagent components?

hugobessaa18:01:20

this looks like a start

hugobessaa18:01:43

but I'm interested into examples of specter being used to test hiccup syntax

hugobessaa20:01:54

this one includes rendering the component to the DOM. I'm suspecting I will have to do that anyway…

gadfly36120:01:19

Yeah, i think a lot of the time you can get away without it, but it's really nice to have it in the toolbelt IMO (especially bc it runs super fast)

hugobessaa23:01:41

@gadfly361: after some fiddling, I'm convinced this approach is better. Checking hiccup isn't as nice. Would need some tooling I don't have time to write now. Thanks for the link again.