Fork me on GitHub
#malli
<
2021-03-05
>
beders04:03:31

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}

ikitommi07:03:57

@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.

ikitommi07:03:58

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

ikitommi07:03:05

(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}

ikitommi07:03:59

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]*$"]

beders17:03:42

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

ikitommi07:03:31

comments welcome on how it should work.

ordnungswidrig08:03:09

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

dharrigan08:03:23

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

borkdude11:03:01

@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

thinking-face 3
ikitommi12:03:01

@borkdude brilliant idea. Let's do it.

dviramontes17:03:45

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

borkdude17:03:36

@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

thanks3 3
borkdude17:03:48

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

dviramontes17:03:40

gotcha, thanks very much!

borkdude17:03:05

TIL, malli also supports keywords as predicates:

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

borkdude17:03:13

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