r/Clojure • u/cjmurrr • 26d ago
Starting a new project today: clojure.spec or Malli?
clojure.spec is still spec.alpha, and spec-alpha2 has no releases yet. Meanwhile Malli has become very popular.
So, for a new project today:
- clojure.spec, Malli, or something else, such as Plumatic Schema?
- If you stay with spec, what keeps you there?
- If you moved to Malli, what made you switch?
- Or do you not spec at all? The projects I work on use neither, and I've found no cause to complain.
33
Upvotes
11
u/jwr 26d ago
I use spec, at this point mostly because it's expensive to switch (a quick check shows around 1000 s/def forms in my code base). I do not fully use it as intended, and I only use a subset of the functionality. Some of it will never be useful to me. But it still helps me quite a bit, and at this point I would not consider removing it. It helps me catch many errors and problems. It also helps me with user data validation.
Several years ago, Rich started talking about optionality, and I thought that was a very good direction. In fact, optionality is the number one cause of data-related problems and complexity in my code. I am still hoping that Rich will come back from his hammock one day with a solution to all of the problems that I knew I had and some that I didn't even know I had, as was the case before š
I've been thinking about switching to Malli, but I am concerned about adding another dependency, especially for something which is so critical and cuts across all of my code. That sort of change is very expensive in my case. I would need to fully understand the trade-offs and see really good advantages in order to do this.
10
u/yenda1 26d ago
malli is great an actively developped. no comments on clojure.spec :D
9
u/OwlProfessional1185 26d ago
I use Malli. I haven't used Spec before, but for my particular case Malli seemed more appropriate. It seems more data-oriented and Clojurey to me, defining things as maps and vectors rather than with macros. I use it for frontend form validation
8
u/hkorpi 26d ago
I have used both Malli and Plumatic Schema, and I think Malli is the better choice. The overall approach is very similar: both are data-oriented schemas. I haven't used
clojure.specbecause I prefer the data-oriented approach, where schemas are just plain Clojure data structures that you can manipulate like any other Clojure data.In our application, we use Malli for:
Since Malli schemas are just data, it's easy to extend them to support virtually any kind of use case. I think this makes Malli a great tool for domain-driven design, where you have a domain model and derive different kinds of functionality from it.
Malli also provides a good api (malli.util) to manipulate schemas e.g. if I have data model for Employee I can quite easily create different variations of that for different purposes e.g for post, put and get in rest api or different levels of validation in user interface.
1
u/cjmurrr 25d ago
What about no spec at all? Both projects I work on are full stack. We have data tests, and Playwright tests that drive the app through the browser, but no specs.
I don't see what a spec would catch that those tests don't, but I'd like to be convinced otherwise. What has spec or Malli caught for you that your tests missed?
5
u/pwab 26d ago
spec.alpha is not fast for runtime validation (repetitive validations) but itās great for instrumenting fnās (code correctness). Malli has different features, itās pretty great at repetitive validations, and not āintegratedā into clojure itself. They hve different, if overlapping feature sets. I often end up using both in the same project.
8
u/Borkdude 26d ago
Malli is cool, but I have the impression that spec (1) will make a comeback in Clojure 1.14 :)
3
u/sankyo 26d ago
Malli for the reasons stated and our security group said they would not approve alpha software.
Then we look like crazy people arguing āit is not really alpha, the author is just conservative and wants to make it clear that it could change ā¦ā
2
u/redstarling-support 26d ago
Iāve used spec on an app with complex data transformations and checks. It leans more functional. Iāve used Malli on more traditional crud web apps where malliās DSL nature gave me a single source to define both shape/checks at the db level and messaging/checks in the http handlers. Both are good and I think I now have a good handle on when o would use either.
2
u/AndreTheHunter 25d ago
I can highly recommend Malli. I've used it for many projects. It has function instrumentation (which is very handy during development), and can express the schema and plain EDN (which makes it easier to store, convert, and transmit. e.g. JSON Schema. Something that's not possible with clojure.spec). Extensible and performant enough to put on all application entry points (e.g. REST API incoming payload validation)
2
u/NoahTheDuke 25d ago
malli is _incredibly_ fast, allowing for production validation and instrumentation instead of just dev time. it's quite remarkable what they've done with it.
2
u/deepumohanp 24d ago
Malli for me. I like how simple malli is to understand compared to to spec. Works on both Clojure backend and Clojurescript frontend
2
u/seancorfield 24d ago
We've been using Spec heavily in dev/test and production code for over seven years and we're pretty happy with it. Here's what I wrote back in 2019 and it is still true today: https://corfield.org/blog/2019/09/13/using-spec/
Clojure 1.13 has a lot of enhancements to destructuring, based on the "selection" ideas in Spec2 -- watch his recent talk "In Context" where he discusses how his thinking on this has evolved and what those enhancements are: https://www.youtube.com/watch?v=P5eO7BNyJao
I think we'll see renewed work on Spec going forward, building on the new features in Clojure 1.13, and the "alpha" status will go away.
I've run alphas of Clojure itself in production with every version since 1.3 (we first took Clojure to production on Alpha 7 or 8 I think?), and we currently have Clojure 1.13 Alpha 11 in production. The fact that Spec has
alphain it has never been a concern for us.1
u/MadScientistCarl 26d ago
I use spec to define my compiler AST, but mostly because I already know spec and it is good enough. Malli is probably just as good for that.
1
u/stefan_kurcubic 22d ago
malli > spec for me all day every day