Fork me on GitHub
#malli
<
2023-12-12
>
Alex Sky10:12:50

Hello! How correct is it that in regexp we cut off uuid with upper case? What do you think? I used to be able to pass such uuid in old versions for example 0525AEAC-310A-4B2F-91EA-DD940AA1C30D

(def ^:private uuid-re
  #"(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")

(defn -string->uuid [x]
  (if (string? x)
    (if-let [x (re-matches uuid-re x)]
      #?(:clj  (UUID/fromString x)
         :cljs (uuid x))
      x)
    x))

Alex Sky10:12:52

Standards such as https://www.itu.int/en/ITU-T/asn1/Pages/UUID/uuids.aspx and https://www.rfc-editor.org/rfc/rfc4122 require them to be formatted using lower-case letters, but also require parsers to accept upper-case letters.

delaguardo10:12:52

this regex is case insensitive. at least in clojure. but I'm not sure about cljs

delaguardo11:12:17

if regex begins with (?i) it should be case insensitive

Alex Sky11:12:42

you're right. Hmm, that's freakin' weird. I'll keep looking into why it's not working for me.

Alex Sky11:12:09

I have a version 0.10.4 where

(def ^:private uuid-re
  #"^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$")
:melting_face:

delaguardo11:12:12

that's definitely a bug)

Quest18:12:51

Is it possible to schema def a :namespace "value" itself?

> (type *ns*)
clojure.lang.Namespace
Trying to write a recursive schema for a nested string map representing all loaded namespaces with this shape:
{"aleph" {"flow" {:ns aleph.flow}
          "http" {:ns aleph.http}
          "netty" {:ns aleph.netty}}
  ...}

Quest18:12:38

This schema works using a more generic match with :some

(def =>ns-tree
  [:schema {:registry
            {::ns-tree
             [:map-of :string
              [:or
               [:ref ::ns-tree]
               [:map [:ns :some]]]]}}
   ::ns-tree])

hifumi12321:12:47

Assuming the value associated to :ns is always a symbol, using :fn with a predicate like (comp some? (partial find-ns))

Quest01:12:54

Thanks, this worked with a tweak. My value in :ns is an actual namespace "object" & find-ns can't take it A quick call to str stops the exception -- [:fn (comp find-ns symbol str)] I assume this was too niche or that there's pragmatic reasons that :namespace isn't a Malli builtin -- but I'm willing to PR it if anyone sees this thread in the future & wants it.

hifumi12320:12:52

If you have an actual namespace object, then you can use a predicate like (partial instance? clojure.lang.Namespace)

hifumi12320:12:17

e.g.

user=> (instance? clojure.lang.Namespace (find-ns 'user))
true
Minor caveat: this only tests if an object is a Namespace, it does not tell you whether the corresponding namespace has been loaded or not

👍 1
Quest20:12:06

I like that as a more performant and generic solution, not often I've needed instance? Having a pred for a loaded namespace is useful but heavier & arguably dirty to include in an otherwise "pure" validation (Sidenote: for writing a performant "is namespace loaded check", the-ns is an interesting option that takes either a ns object or a symbol naming a namespace object. I've never seen this FN in a decade of Clojure so making a note here. https://clojuredocs.org/clojure.core/the-ns )