green-coder / minimallist

A minimalist data driven data model library, inspired by Clojure Spec and Malli.
Eclipse Public License 2.0
66 stars 1 forks source link

Added support for the :transformer node #6

Open green-coder opened 4 years ago

green-coder commented 4 years ago

Fixes issue #5

green-coder commented 4 years ago

@borkdude Here is an example of how it works:

(m/describe (h/transform (h/vector-of (h/fn int?))
                         (h/repeat 2 3 (h/fn odd?))
                         (fn [out-data] (map inc out-data))
                         (fn [in-data] (mapv #(* % 10) in-data)))
            [2 4 6])
;=> [30 50 70]
borkdude commented 4 years ago

I'm getting :invalid with this example. Would it be more comprehensible if transform got an opts map instead of 4 positional args? Can you explain what "inner model" and "outer model" is in more detail?

green-coder commented 4 years ago

I tried the example again, it works on my computer.

I'm getting :invalid with this example.

Strange, it works on my computer. Did you use the second commit that I added yesterday?

Would it be more comprehensible if transform got an opts map instead of 4 positional args?

That's a good idea.

Can you explain what "inner model" and "outer model" is in more detail?

The transform node is a bridge between 2 models. I named them outer-model and the inner-model. The outer model is the one used before you cross the bridge, and the inner model is on the other side of the bridge. The functions outer->inner and outer<-inner are transforming the data which is validated / parsed / generated as you cross the bridge.

In practice, the outer model is used mainly to ensure a valid data format before calling outer->inner on it.

I might iterate again on this PR and change the format of the node to cover a more general way to bridge.