Fork me on GitHub
#malli
<
2022-02-09
>
bringe17:02:45

Hello. I’m wondering how I can update a schema to dissoc a prop if the schema uses :and at the top-level. For example, if I have the following:

(def my-schema
    [:and
     [:map
      [:id uuid?]
      [:some-prop int?]
      [:some-prop-2 int?]]
     [:fn {:error/message "Some prop must be less than some prop 2"}
      (fn [{:keys [some-prop some-prop-2]}]
        (< some-prop some-prop-2))]])
and I want to dissoc :id from the map, but keep everything else in the schema the same, how can I do that? I see the malli.util dissoc function and others, but I’m not sure how to use them when :and is involved.

bringe17:02:21

Ah, it seems that (mu/update my-schema 0 mu/dissoc :id) works, but maybe there is a better way?

crimeminister18:02:34

There's a function for manipulating properties that might be worth looking at in case it makes intent clearer: (mu/update-properties).

👍 1
pithyless19:02:37

for mu/merge there is a kind of special case: > * for :and schemas, the first child is used in merge, rest kept as-is I wonder if that could (and should) be generalized to other malli.util operations

bringe03:02:08

@U066SF64X I’m not sure if update-properties can be used directly in this case, given the :and , but maybe there’s a good way to use it in combination with another operation to make intent clearer. @U05476190 Yeah :thinking_face:.

Oliver Marks20:02:52

HI, I have this spec which is being used by a reitit route, I am having issues where it fails because the value is a string instead of a keyword, I have been trying to use the json transformer but I think it fails because its not specifically a keyword test, any suggestion on how I could solve this ?

[:or
   [:map {:closed true}]
   [:map {:closed true}
    [:start [:enum :january :feburary :march :april :may :june :july :august :september :october :november :december]]
    [:end [:enum :january :feburary :march :april :may :june :july :august :september :october :november :december]]]]

Ben Sless20:02:19

You need to add a decoder

Oliver Marks20:02:34

So i am running the spec through the json-transformer decoder but I dont think it handles this case because its of enum type not keyword

Oliver Marks20:02:01

I am currently looking at -json-decoders trying to see if I can add to that some how

Oliver Marks20:02:07

[:category/type {:optional true} keyword?]
currently the above is working as intended, feels like i need to extend some where I tried adding [:and keyword? [:enum :one :two]] but that does not trigger the conversion

Grant Horner21:02:00

Is there a :seqable schema that would allow you to specify the children’s type? I’m running into an issue where I’d like to have a function accept a vector, list or set of a particular entity, but haven’t been able to figure out how to build the correct collection schema for it. I’m assuming I should be using malli.core/-collection-schema to add a custom schema to the registry

ikitommi08:02:15

You can also just define the Schema as Var as use it without registering, like Reagent compoents:

(def Seqable (m/-collection-schema {:type 'Seqable, :pred seqable?}))

(m/validate [Seqable :int] '(1 2 3)) ; => true

ikitommi08:02:45

If you feel that is important, please write an issue to add that to malli.

ikitommi08:02:08

adding new types means the following: • add it to the core • generator for it • JSON Schema (and optionally swagger) mappings • default humanized error message • clj-kondo mappings • + tests

ikitommi08:02:08

without registering it, you won’t can’t deserialize a serialized schema, but for anything else, it should work normally.