malli

https://github.com/metosin/malli :malli:
beders 2021-03-05T04:57:31.181200Z

quick question with regards to properties. I’m trying to put some properties into my schemas, but can’t seem to be getting them back. What am I doing wrong here?

; malli 0.3.0:
; [malli.registry :as registry]
(def registry
  (registry/composite-registry
    malli/default-registry
    {:common/single-line [:re {:bubu :lala} #"^[^\r\n]*$"]}))

(malli/properties :common/single-line {:registry registry})
=> nil
Expected: {:bubu :lala}

ikitommi 2021-03-05T07:34:57.186600Z

@beders oh, that’s not good. registry interally wraps the registered schema instances into :malli.core/schema , which is an eager reference type. When you pull out an instance schema from a registry, you get the reference back. it mostly a pass-through, e.g. calling -validatorto the reference return the validator of the referenced schema. But: for some reason, the current impl returns the reference properties and options if asked. I think it’s a bad feature, should be changed.

ikitommi 2021-03-05T07:36:58.187900Z

need to think how that effects other things. before that, you m/deref safely:

ikitommi 2021-03-05T07:37:05.188200Z

(def registry
  (mr/composite-registry
    m/default-registry
    {:common/single-line [:re {:bubu :lala} #"^[^\r\n]*$"]}))

(-> (m/schema :common/single-line {:registry registry})
    (doto prn)
    (m/deref)
    (doto prn)
    (m/properties))
;:common/single-line
;[:re {:bubu :lala} #"^[^\r\n]*$"]
;=> {:bubu :lala}

ikitommi 2021-03-05T07:38:59.188700Z

workaround for now:

(def registry
  (mr/composite-registry
    m/default-registry
    {:common/single-line [:re {:bubu :lala} #"^[^\r\n]*$"]}))

(defn schema [?schema]
  (m/deref (m/schema ?schema {:registry registry})))

(schema :common/single-line)
; => [:re {:bubu :lala} #"^[^\r\n]*$"]

beders 2021-03-09T17:49:42.261200Z

Thanks for the help and explanation. It would be good to have some documentation around explaining instances vs. Schema. I’m still confused 🙂

ikitommi 2021-03-05T07:41:31.189600Z

comments welcome on how it should work.

ordnungswidrig 2021-03-05T08:25:09.190Z

Is there a standard body of localized error messages for humanize?

dharrigan 2021-03-05T08:28:08.190200Z

Only english https://github.com/metosin/malli/blob/master/src/malli/error.cljc

dharrigan 2021-03-05T08:28:23.190600Z

However, the docs do show how to add in other i18n messages

dharrigan 2021-03-05T08:29:34.190900Z

https://github.com/metosin/malli#custom-error-messages

borkdude 2021-03-05T11:51:01.192Z

@ikitommi What about making a plugin for malli which inspects clojure.spec specs and emits a malli schema from it? ;) Might help people migrating to malli

1
ikitommi 2021-03-05T12:55:01.194200Z

@borkdude brilliant idea. Let's do it.

2021-03-05T17:12:45.196300Z

Hi, trying to wrap my mind around how these two examples can/will differ over time when it comes to value generation

(def CDN1
  [:map
   [:images [:vector string?]]])

(def CDN2
  [:map
   [:images [:sequential string?]]])

(malli.generator/generate CDN1)
(malli.generator/generate CDN2)
-- i guess the question is - will they defer ? and if so how ? in my testing the generated values are very similar

borkdude 2021-03-05T17:15:36.197200Z

@dviramontes in clojure sequential can also be a list or lazy-seq for example, not always a vector, but that should not affect equality semantics

1
borkdude 2021-03-05T17:17:48.197900Z

it seems the generator doesn't really generate anything other than vectors at the moment (by experimentation) but it could

2021-03-05T17:18:40.198200Z

gotcha, thanks very much!

borkdude 2021-03-05T17:19:05.198700Z

TIL, malli also supports keywords as predicates:

(malli.generator/generate [:sequential :int])
I prefer that syntax personally

borkdude 2021-03-05T17:20:13.199Z

user=> (malli.core/validate :int 1)
true