malli

https://github.com/metosin/malli :malli:
armed 2021-04-07T13:44:07.335600Z

Hey, have issue with malli.error/humanize, here is minimal example (cljs):

(let [Schema2 [:map
              [:field keyword?]]
      Schema [:map
              [:foo int?]
              [:bar [:or nil? Schema2]]]]
  (malli.error/humanize
   (malli/explain Schema {:foo 1
                          :bar {:field "test"}})))
Throws error: #object[Error Error: Vector's key for assoc must be a number.]

armed 2021-04-07T13:46:32.336800Z

What I'm missing here?

armed 2021-04-07T13:48:59.337400Z

By the way, in clojure I get another error for same snippet:

Execution error (ClassCastException) at malli.error/-ensure (error.cljc:124).
class clojure.lang.Keyword cannot be cast to class java.lang.Number (clojure.lang.Keyword is in unnamed module of loader 'app'; java.lang.Number is in module java.base of loader 'bootstrap')

ikitommi 2021-04-07T13:50:22.337800Z

@armed itโ€™s a known issue, see https://github.com/metosin/malli/pull/333

1๐Ÿ‘
ikitommi 2021-04-07T13:50:49.338400Z

you could say [:maybe Schema2] and it should work.

ikitommi 2021-04-07T13:51:08.338800Z

but, need to be fixed to be always robust.

armed 2021-04-07T13:52:00.339500Z

@ikitommi thanks for explanation, going to use :maybe

danielneal 2021-04-07T15:22:58.340100Z

When I call malli.util/update on a schema, the properties of the key I update are dropped, is there any way to preserve them?

danielneal 2021-04-07T15:24:02.340300Z

(malli.util/update
    (malli.core/schema
      [:map
       [:a {:optional true}
        int?]
       [:b {:optional true} int?]])
    :a identity)

danielneal 2021-04-07T15:24:09.340500Z

;=> [:map [:a int?] [:b {:optional true} int?]]

nilern 2021-04-07T15:26:19.341100Z

Looks like a bug to me :thinking_face:

danielneal 2021-04-07T15:27:51.341300Z

Shall I open an issue?

nilern 2021-04-07T15:35:09.343900Z

:thinking-face: util/update is implemented by getting the value, then setting it But it would be reasonable to expect that update preserves the properties even if get + assoc would not

danielneal 2021-04-07T15:37:47.345500Z

I agree

nilern 2021-04-07T15:38:00.345800Z

I think an issue would at the very least be a better place to discuss how that should work

danielneal 2021-04-07T15:44:06.346Z

cool, added one here https://github.com/metosin/malli/issues/412

Hankstenberg 2021-04-07T20:11:30.349800Z

What would be a good way to "cut" a data structure using a schema that it partially adheres to? To keep what conforms to the schema and discard the rest? The best approach I can think of is to use m/explain and then use the data from :errors to run an update-in on the data. Is there a better way to do that? It seems to me like there should be.

nilern 2021-04-07T20:21:39.350300Z

Sounds like a more general version of strip-extra-keys-transformer

nilern 2021-04-07T20:23:08.350800Z

Or perhaps the tentative coercer https://github.com/metosin/malli/issues/404