Fork me on GitHub
Martynas M08:04:16

Hey. Is there a way to decode a string into a symbol?

(malli.core/decode [:enum 'identity]
I want to parse JSON and I want to parse a string into a symbol but I want to force this exact value. I could define my own decoder and I did it in one other case but I don't think I want to define too many of them.

Martynas M08:04:04

Probably this is the way:

[:and :symbol [:enum 'identity]]

Ben Sless10:04:52

You can specify your own decoder in the schema

Ben Sless10:04:28

Where you can just put the symbol decoder

Martynas M08:04:36

This fails to generate: (malli.generator/generate [:and :symbol [:enum 'my-symbol]]) Should I create an issue?


oh, that’s an test.check impl detail, not sure how much we can do for it.


e.g. when multiple constraints, first one is used for generation and the rest to filter that the first one generated valid values.


quick & dirty fix, override the generator:

(malli.generator/generate [:and {:gen/schema [:enum 'my-symbol]} :symbol [:enum 'my-symbol]])

Martynas M05:04:48

I made this function for myself:

(defn exact-symbol [sym]
  [:and {:gen/schema [:enum sym]} :symbol [:enum sym]])
I simply use the generator to validate that my schema is correct. If it generates then it is somewhat correct. My assumption is that if the generator can pick it up then it's a valid definition (not that the result is valid but the definition).