clojure-spec

About: http://clojure.org/about/spec Guide: http://clojure.org/guides/spec API: https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html
stephenmhopper 2020-12-31T13:49:57.025600Z

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.

alexmiller 2020-12-31T16:28:48.026500Z

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

stephenmhopper 2020-12-31T16:30:53.027200Z

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