Reputation: 2850
I am using plumatic/schema with my compojure-api to validate the input for an endpoint.
I have two keys in my schema: Field1
& Field2
. I want to be able to define a rule for my schema which for example says:
WHEN Field1 = "AA"
THEN Field2 is required-key
ELSE Field2 is optional-key
But, I only seem to only be able to set the key to either require or optional. Is it possible to make a key depended on another key?
(schema/def Field1
(schema/enum "AA" "BB"))
(schema/def Field2
(schema/enum "AAAA" "BBBB" "CCCC"))
(schema/defschema MySchema
{(schema/required-key :field1) Field1
; Here I want some kind of logic to make the key required if
(if (= Field1 "AA")
(schema/required-key :field2) Field2
(schema/optional-key :field) Field2)
})
Upvotes: 1
Views: 153
Reputation: 16060
(use '[plumbing.core])
(schema/defschema MySchema
(schema/conditional
(fn-> :field1 (= "AA"))
(schema/schema-with-name {(schema/required-key :field1) Field1
(schema/required-key :field2) Field2} "cond1")
:else
(schema/schema-with-name {(schema/required-key :field1) Field1
(schema/optional-key :field2) Field2} "cond2")))
Upvotes: 2