Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use schema.core/enum in clojure?

Tags:

enums

clojure

Given an enum made from prismatic schema.core/enum, let's say:

(def myenumtype (schema.core/enum "a" "b" "c"))

How can I set another def to a specific enum item? Here I would like to set e to the "a" enum item.

(def e (???  myenumtype))

And how can I compare this to a specific enum? Here I would like to check to see if e is equal to the "a" enum type.

(= e ((??? "a") myenumtype))
like image 489
marathon Avatar asked Jun 09 '26 07:06

marathon


1 Answers

I think you're misunderstanding how Schema works. You're not creating an enum type, you're creating a validator that checks if a particular value equals one of the enumerated values.

In your case, all you need to do is:

(def e "a")

Here's an example REPL session:

user=> (schema.core/validate (schema.core/enum "a" "b" "c") "a")
"a"
=> (schema.core/validate (schema.core/enum "a" "b" "c") "z")
clojure.lang.ExceptionInfo: Value does not match schema: (not (#{"a" "b" "c"} "z"))
like image 51
Jeremy Avatar answered Jun 12 '26 12:06

Jeremy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!