Fork me on GitHub
#malli
<
2021-03-07
>
mike_ananev12:03:31

@ikitommi what is the difference between :title and :description in spec? I want to describe my config file using malli and guessing what should I use to describe every field in the config map.

mike_ananev12:03:07

(def http-server-host [:and {:title "http server host"} ne-string])
;; or
(def http-server-host [:and {:description "http server host"} ne-string])

ikitommi12:03:53

@mike1452 those are pulled from JSON Schema convention: > The `title` and `description` keywords must be strings. A “title” will preferably be short, whereas a “description” will provide a more lengthy explanation about the purpose of the data described by the schema. I would use :description there. In OpenAPI & Swagger, title is pulled as the Schema name, e.g.

[:map {:title "User", :description "Describes User of the System"} 
 [:name {:description "Name of the User"} :string] 
 [:age {:description "Age, must be >= 18} [:int {:min 18}]]]

ikitommi12:03:50

pulling the return types too:

(-var-schema #'str)
;[:function
; [:=> :catn :string]
; [:=> [:catn [x :any]] :string]
; [:=> [:catn [x :any] [ys [:+ :any]]] :string]]

(-var-schema #'distinct?)
;[:function
; [:=> [:catn [x :any]] :boolean]
; [:=> [:catn [x :any] [y :any]] :boolean]
; [:=> [:catn [x :any] [y :any] [more [:+ :any]]] :boolean]]

borkdude12:03:11

Clever:

user=> (meta (second (:arglists (meta #'str))))
{:tag java.lang.String}

ikitommi13:03:25

Noticed too that the hand-written arglists seem to have weird syntax.

joshkh16:03:08

loving malli so far! what's the idiomatic way of "extending" a schema? for example let's say i have a Person map with the basic attributes first-name and last-name, and then a ProfessionalPerson that is a Person with an additional required profession attribute. would i create a ProfessionalPerson definition that contains only just [:map [:profession string]] and then mu/merge it on top of the Person map [:map [:first-name string?][:last-name-string?]]?

joshkh16:03:53

cool, thanks borkdude. on a side note it's too bad that mu/assoc doesn't allow for & kvs like clojure.core/assoc 😄

juhoteperi16:03:10

Might be possible to implement. The options parameter makes it a bit inconvenient though, we could check that if odd number of params, last param is the options, and for even number of params, no options, just kv pairs. Not sure if worth the complexity.

juhoteperi16:03:25

malli.util/dissoc also takes just one key.

borkdude16:03:55

if you need more than one k/v, you can just use mu/merge :)

borkdude16:03:26

or repeat mu/assoc

joshkh16:03:50

yeah, merge is fine by me. i just thought it was funny that my first instinct was to treat it like the assoc i know.

juhoteperi16:03:42

Yeah. Though I think it will be worth to mention the differences to clojure.core functions in the docstrings, now the util fns mention just "like clojure.core/x" but then many of them take a bit different parameters.

joshkh16:03:55

perhaps. admittedly i just tried it out while completely ignoring the very clear docstring params in my editor. you can lead a horse to water but you can't make it drink 😉

borkdude16:03:05

It's awesome that you can do this with malli btw.

👍 3
joshkh16:03:58

absolutely. and the deep merging is really handy, too

ikitommi17:03:54

there are at least two options to resolve the malli.util varargs issue: 1. make all utilities only work with Schema instances, e.g. no auto-coercion from schema AST => only place to pass the options would be the m/schema -> simpler, more boilerplate, 2. make a custom type/record/protocol for the options, could be the first argument in all functions, “the schema context” - easy to distinguish it from other args

schmee19:03:09

do I need to have sci for a spec like this?

[:map {:gen/fmap 'map->Point}
       [:lat [:double {:min -180.0 :max 180}]] 
       [:long [:double {:min -180.0 :max 180}]]]]

schmee19:03:45

can you configure malli to use the clojure compiler for eval instead of sci?

schmee20:03:32

I can’t get this to work with or without sci, can you not use namespace-qualified functions as arguments to :gen/fmap? :thinking_face:

ikitommi20:03:00

yes, just don't quote and it works

ikitommi20:03:31

(but, can't be deserialized if it's a fn value)

ikitommi20:03:42

If you wan't eval, PR welcome. Could be option :malli.core/evaluator a, there could be a -eval-evaualuator in malli.core

schmee20:03:57

ahh, I was fooled by the output of my REPL, it printed the record as a plain map (and skipped the record type)!

schmee20:03:03

then all is well, thanks for the help 🙂