malli

Zach 2024-09-18T10:10:47.934819Z

How do I write a schema for a map that has a single key value pair where the key is a keyword and the value is an int?

Zach 2024-09-18T10:11:59.165099Z

For example {:a 1} is valid but {} , {1 2} and {:a 1 :b 2} are invalid

opqdonut 2024-09-18T10:17:23.615269Z

I think :map-of supports :min and :max these days, so that should just be...

[:map-of {:min 1 :max 1} :keyword :int]

ingesol 2024-09-18T11:36:53.253689Z

Is it possible to add function style schema for a protocol, to validate input and output for protocol functions?

➕ 1
Zach 2024-09-18T15:05:17.292239Z

Is it possible to define constraints? For example, let's say I have a map with 3 keys, a, b and c:

{:a 3
 :b 4
 :c 5}
Is there a way to validate that a^2 + b^2 = c^2?

shane 2024-09-18T15:37:18.959719Z

might need to use :fn for that: https://github.com/metosin/malli?tab=readme-ov-file#fn-schemas

Zach 2024-09-18T19:20:50.667529Z

Exactly what I had in mind, ty

shane 2024-09-18T15:40:34.875629Z

is it possible in a :map for the field schemas to get access to the entire map value? I'm looking at https://github.com/bsless/malli-keys-relations and I would like the error to be associated with a specific field rather than at the :malli/error level. Or is there a better way of doing these key relations now?

opqdonut 2024-09-19T05:54:55.143799Z

no, nothing permits that right now. that's why bsless's solution is also "outside" the map

flowthing 2024-09-18T17:26:44.343719Z

Given a schema like this:

[:sequential
    [:map [:type [:enum "Foo"]] [:value :string]]]
When coercing, I want to drop all items from the sequence that do not validate against the map schema. So, for example, this would pass coercion as is:
[{:type "Foo" :value "bar"}]
But this:
[{:type "Foo" :value "bar"}
 {:type "Baz" :value "quux"}]
Would coerce into this:
[{:type "Foo" :value "bar"}]
I'm thinking I could write a custom transformer that, given e.g. [:sequential {:decode/invalid-items :drop} ...], would drop invalid items from sequences. Does that sound like a reasonable approach, or is there a built-in feature I'm overlooking?

Eva O 2024-09-18T18:57:11.150239Z

There's mt/strip-extra-keys-transformer

Eva O 2024-09-18T18:57:57.709279Z

Example from the README:

(def strict-json-transformer
  (mt/transformer
    mt/strip-extra-keys-transformer
    mt/json-transformer))

(m/decode
  Address
  {:id "Lillan",
   :EVIL "LYN"
   :tags ["coffee" "artesan" "garden"],
   :address {:street "Ahlmanintie 29"
             :DARK "ORKO"
             :city "Tampere"
             :zip 33100
             :lonlat [61.4858322 23.7854658]}}
  strict-json-transformer)
;{:id "Lillan",
; :tags #{:coffee :artesan :garden},
; :address {:street "Ahlmanintie 29"
;           :city "Tampere"
;           :zip 33100
;           :lonlat [61.4858322 23.7854658]}}

flowthing 2024-09-18T18:58:16.702789Z

That only strips extra keys from maps, not extra items from sequences.

Eva O 2024-09-18T18:58:51.394979Z

Oh I missed that. Yeah I don't know of an existing transformer that does that

✅ 1