malli

https://github.com/metosin/malli :malli:
ikitommi 2020-07-09T07:36:43.098900Z

re-implemented the schema visitor using walker: m/accept is now a postwalk and and there is new m/find-first to do lazy scanning for schemas. Though of doing a prewalk too, but coudn’t find any valid use case for such and would be more complex for schema extenders to add support for it. Not sure how useful the m/find-first is and should merge this: https://github.com/metosin/malli/pull/216

ikitommi 2020-07-09T07:39:15.100Z

should the m/accept be just m/walk in the future? or m/post-walk?

ikitommi 2020-07-09T09:05:05.100500Z

(deftest find-first-test
  (let [schema [:map
                [:x int?]
                [:y [:vector [:tuple
                              [:maybe int?]
                              [:or [:and {:salaisuus "turvassa"} boolean?] int?]
                              [:schema {:salaisuus "vaarassa"} false?]]]]
                [:z [:string {:salaisuus "piilossa"}]]]]

    (let [walked-properties (atom [])]
      (is (= "turvassa" (m/find-first
                          schema
                          (fn [s _in _options]
                            (some->> s m/properties (swap! walked-properties conj))
                            (some-> s m/properties :salaisuus)))))
      (is (= [{:salaisuus "turvassa"}] @walked-properties)))

    (let [walked-properties (atom [])]
      (is (= "vaarassa" (m/find-first
                          schema
                          (fn [s _in _options]
                            (some->> s m/properties (swap! walked-properties conj))
                            (some-> s m/properties :salaisuus #{"vaarassa"})))))
      (is (= [{:salaisuus "turvassa"}
              {:salaisuus "vaarassa"}] @walked-properties)))))

alpox 2020-07-09T09:45:24.102800Z

Hi all! Is there a possibility to merge definitions declaratively - for example in an edn definition given that the definitions to merge are defined in a registry?

alpox 2020-07-09T09:48:17.104800Z

I see I can do

[:and
 [:ref :test/object]
 [:ref :test/annotated]
 [:map ....]]
but sadly this would test for all to be true rather than that later definitions override definitions from the former. Basically, I'm asking if there is something like mu/merge in a declarative way

👍 1
ikitommi 2020-07-09T12:23:32.107200Z

@alpox nothing just now, but sounds like a good idea. Could you write an issue of that?

alpox 2020-07-09T13:39:17.107800Z

@ikitommi Done: https://github.com/metosin/malli/issues/217 thanks for the response!

euccastro 2020-07-09T14:35:07.108900Z

what's the idiomatic way to specify a nonempty string in a schema? should I actually use a custom registry as in the README?

euccastro 2020-07-09T14:39:43.109100Z

currently I'm doing this

(def nonempty-string
  [:and
   string?
   [:fn {:error/message "should be nonempty string"}
    (fn [x] (pos? (count x)))]])

euccastro 2020-07-09T14:42:05.109400Z

one minor problem with that is that malli/explain gives me two error messages: "should be string" and "should be nonempty string". I tried to set the :error/message in the :and instead to get only one error, but that didn't work

euccastro 2020-07-09T14:44:48.109600Z

(I'm using the malli version that reitit 0.5.2 requires, i.e. 0.0.1-20200525.162645-15, if that matters)

euccastro 2020-07-09T14:45:49.109900Z

I'm happy to upgrade to any version that is compatible with reitit 0.5.2

ikitommi 2020-07-09T14:58:12.110100Z

@euccastro [:string {:min 1}], will need to release new version to work with reitit

euccastro 2020-07-09T15:15:54.110300Z

thank you!

jkent 2020-07-09T15:23:10.111200Z

@ikitommi is there any chance you can release a new version of reitit with the latest malli?

euccastro 2020-07-09T17:24:10.114500Z

another option for early adopters would be to release cutting-edge versions of malli under a separate namespace and artifact id (e.g., malli.pre-alpha) so we can use that for our own code without fear of breaking reitit. that would also reduce the pressure to update reitit unless new malli functionality is actually needed there

euccastro 2020-07-09T17:24:37.114800Z

please ignore this if it sounds like too much of a hassle 🙂