4 ms·
Spec is great for validation. For example, we have a HTTP API built using https://github.com/metosin/reitit https://github.com/metosin/reitit where we have spec
by dancek 7y ago
Spec is great for validation. For example, we have a HTTP API built using https://github.com/metosin/reitit https://github.com/metosin/reitit where we have specs for each endpoint. We have a coercion middleware that checks each request (and optionally response) conforms to a spec (and coerces things like integer ids from path to the correct type). And there's a swagger handler that automatically generates a full documentation of the API just from the route tree and specs, including samples of input and output for all routes.
Similarly you can do HTML form validation, etc.
We even use honeysql with nice helper functions that ensure that data-changing SQL queries are of the expected form. Say, you want to update just one row, you have a helper function that checks the honeysql data has a where clause with a unique column and only then runs the query. Someone should maybe blog about that sometime.
What I've found, though, is that trying to use spec like type checking is usually not a great idea. I expect better from static analysis / compile-time checking, with clj-kondo being the most promising tool atm.
- Scarbutt 7y agoI remember spec being pitch for something you only use at dev time and turn off in production because it makes your runtime very very slow, what has change? Seems users are using it for everything now.
- nickik 7y agoNo. It was always designed to be configurable so you can keep on the API endpoints, but deactivate them internally.
- dancek 7y agoWe don't have instrumentation in production, ie. we spec much more at dev time. But for things you'd validate anyway spec is a good match and probably about as performant as hand-written validation. The only strange use of spec I mentioned is validating db queries. For us updates and deletes are rare enough that perf doesn't matter.
- fulafel 7y agoI bet you are thinking of https://clojure.org/guides/spec#_instrumentation_and_testing https://clojure.org/guides/spec#_instrumentation_and_testing - it was always just one use of spec among many.
- seancorfield 7y agoSpec has numerous uses. We use it heavily in production for data validation/conformance: https://corfield.org/blog/2019/09/13/using-spec/ https://corfield.org/blog/2019/09/13/using-spec/
- dkersten 7y agoI have a somewhat off-topic question I've been wondering about... How do you pronounce "reitit"? I've been considering giving a talk on it at my local clojure group, so I should probably learn how to actually say the word!
- wry_discontent 7y agoIn my brain I usually say re-it-it, but sometimes I think "rye-tit" which makes me laugh and definitely seems wrong.
- dancek 7y agoRaitit (rye-tit) is a Finnish word too: the plural of "a lone village road splitting the village in half". Would make a nice library name. Wonder if Metosin takes requests...
- motor_toni 7y agoIt's more like "ray-tit". A colleague at Metosin came up with a good alternative: "rate it!" Used in sentence like "I reitit 5 out of 5!"
- arcatan 7y agoIt's a Finnish word. Google Translate's text-to-speech is pretty decent - click the speaker icon on the left: https://translate.google.com/#view=home&op=translate&sl=fi&tl=en&text=reitit https://translate.google.com/#view=home&op=translate&sl=fi&t...
- dkersten 7y agoThank you!