Pulling parts of the schema out into variables
atroche opened this issue · 3 comments
Hi,
The schema I'm building is pretty big, and it'd be nice to build it up out of named parts rather than just having a single large map.
To take a small example, instead of doing…
(def author
'{:name str
:email str
:locale '(or "en" "jp" "de")})
I'd like to do:
(def locale '(or "en" "jp" "de"))
(def author
'{:name str
:email str
:locale locale})
But I can't find a way to do this. Any tips would be much appreciated!
Cheers.
So far the only way I have of doing this is:
(def author
`{:name ~'str
:email ~'str
:locale ~locale})
Which is pretty ugly…
Ah, I found your link to backtick in this issue comment, and using its template macro I can do:
(def author
(template {:name str
:email str
:locale ~locale}))
I'm surprised this isn't a use case you've come across in your own work, @miner. What do you do for large schemas?
The (grammar ...) pattern lets you give names to subparts of a schema. You can also merge-schema to share grammar "rules". I use Backtick template to make use of other Clojure data in schemas.