Fork me on GitHub
#clojure-spec
<
2020-12-31
>
stephenmhopper13:12:57

Happy new year’s eve, everyone! I’m working on writing some form validation for a web application. In my web app, most of my requests have well-formed request bodies that are known ahead of time. However, some of the requests have fields / properties whose structure is determined by metadata entries in my application’s database. Is there a good example somewhere of programmatically generating Clojure specs for handling these at runtime? So far, I’ve found that I can create a spec with (s/spec int?) (for example), but then I’m not sure how to use that with (s/keys) as all of the s/keys examples I’ve seen require specs to be registered in the global spec registry. Any ideas on how I can do this? Do I need spec2 for this? Also, it’s worth noting that changes to these metadata DB rows are infrequent enough that I could probably create the specs by reading the DB when the application first starts, but I’d like to avoid doing that if possible.

Alex Miller (Clojure team)16:12:48

you certainly can register the specs dynamically but it requires a bit of care with macros or eval to do so. spec 2 is a little friendlier for this use case but is not ready for real use yet

stephenmhopper16:12:53

Okay, thank you, Alex. I just found https://github.com/metosin/spec-tools, which might fit my use case. TBD