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?
For example {:a 1} is valid but
{} , {1 2} and {:a 1 :b 2} are invalid
I think :map-of supports :min and :max these days, so that should just be...
[:map-of {:min 1 :max 1} :keyword :int]Is it possible to add function style schema for a protocol, to validate input and output for protocol functions?
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?might need to use :fn for that:
https://github.com/metosin/malli?tab=readme-ov-file#fn-schemas
Exactly what I had in mind, ty
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?
no, nothing permits that right now. that's why bsless's solution is also "outside" the map
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?There's mt/strip-extra-keys-transformer
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]}}That only strips extra keys from maps, not extra items from sequences.
Oh I missed that. Yeah I don't know of an existing transformer that does that