When I first heard of
lein-try on the Cognitect Podcast (and then again in the Clojure Cookbook I thought wow! How cool is that! I tried it with following the example in the readme
clj-time to get familar with using it.
First add lein-try to your ~/.lein/profiles plugins vector, run
lein deps to get it installed.
I wanted to understand Prismatic’s Schema and wanted to try it. I wanted to start simple so I could understand it.
A repl starts, then require the library and give it an alias
Ok now ready to play!
First we need to define a schema:
Then we can use validate on a data structure to make sure it matches
On success we get back the same structure. However, if we pass an invalid structure
Wait a minute. What is this? an actually helpful error message in Clojure?!?!
It tells you the keyword of the invalid value and what value was passed in. Nice.
We can validate, but what does check do?
1 2 3 4
Looks like you’d use check when you are prepared to deal with nil, and validate when a representation of the data structure is needed.
Now lets try a more complex example.
1 2 3 4
This example is Source which consists of 3 elements. Lets try leaving one off:
1 2 3
This makes me think maybe there is a way to indicate an optional value? Reading down farther on the documentation I see how to make keys optional. Lets redefine our Source with year as optional:
1 2 3 4 5
Sweet! I also read that since Schemas are just data structures they are composable. Lets add Source to Recipe to make a more complex structure.
1 2 3 4 5 6 7
Nice!! Since I’m a ruby developer by day, I think how could I do this in ruby? Well, in Rails, we have validations on models and once you set the attributes you could call model.valid? which would return boolean. If it’s not valid, it populates a errors attribute on the model with a nested hash of key/error messages. Composing two together is not as straightforward. You might be able to with the
accepts_nested_attributes_for on a Recipe model, but I conclude its not going to be as elegant as using Clojure and Schema :)
lein-try made it easy to experiement with a library and poke around to practice :)