Fork me on GitHub
#malli
<
2023-04-28
>
cap10morgan15:04:25

I have a question about coercion of :or schemas: If the value already conforms to one of the or'd schemas but it can be coerced to the first one, does it make sense to coerce it to that (as seems to be the current behavior)? Wouldn't it be better to leave it alone? Is there a way to do that? For example, I have a schema that is just [:or :keyword :string] but it keeps coercing all of my strings to keywords even though strings are allowed too.

cap10morgan15:04:11

Should I just do a validate check before coercing?

cap10morgan15:04:30

And actually, I'm not sure I can easily do that in this context. I'm using reitit w/ malli request coercion and I need it to leave my strings alone, but it converts them to keywords.

cap10morgan15:04:08

perhaps coercion should short-circuit if the value already validates?

cap10morgan15:04:45

probably some performance implications to all of this

ikitommi15:04:47

Or should already short-circuit on first valid value. It has a (small) performance penalty to validate after decode, but think it's the only way to make it work

cap10morgan15:04:31

yeah, I guess it's the decode step that's doing the string -> keyword change. I'm using the json-transformer but trying to leave strings as strings. this is not a map key. but the [:or :keyword :string] schema seems to decode the string to a keyword. unless I'm misunderstanding...

ikitommi15:04:44

use [:or :string :keyword]?

cap10morgan15:04:26

yeah, I had thought I couldn't b/c I need EDN to stay as keywords when using that. but then I remembered that that won't activate the json-transformer in the first place. so that might be the key!

cap10morgan15:04:03

yeah that seems to work. glad it was a simple PEBKAC error! thanks!

👍 2