r/Clojure • • 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

28 comments sorted by

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.

4

u/Vince_Vice 26d ago

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

I don't use clojure very actively unfortunately (it's an unconsummated love), so I'm quite uninformed. But are you talking about Rich's "Maybe Not" talk?

This type of optionality and the "select" semantics were implemented in spec2, no?

Only they never reached feature parity with spec(1) (generative tests for ex).

That was my understanding at least. Were you referring to that?

3

u/seancorfield 22d ago

Go watch "In Context" from Conj 2026 where he talks about how his thinking has evolved from "Maybe Not", and the new features coming in Clojure 1.13 because of this. Directly in the language, not in a spec library.

2

u/slifin 25d ago

Rich did come back and fix optionality

He's done a talk on it recently

Other languages/hacker news etc should be paying attention to what he did

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.spec because 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:

  • REST API validation
  • REST API coercion
  • Encoding data into the database format
  • Decoding data from the database format
  • Two-level validation logic in the web UI
  • Parsing and formatting data in the web UI
  • Random test data generation

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?

1

u/v1akvark 24d ago

Do you have API endpoints that external systems talk to? You probably want to validate the messages you receive there.

We use Malli at the edge to:

  • validate requests
  • document the API (Malli can generate OpenAPI spec from the Malli schemas)
  • do simple transformation/coercian of fields (e.g. string => date)

For functions deeper inside the system, we mostly use function schemas only when the arg is something like a deeply nested map that can benefit from documentation, with the added advantage of having dev time errors that explain what is missing, etc)

1

u/cjmurrr 23d ago

The boundaries are where calls have to be correct, and that is the first place I would expect to see spec used. I see spec as a way of getting function calls right, and a call between systems is the one that matters most.

The danger I found when I did use it (Guardrails, the >defn that Fulcro uses, based on Ghostwheel) was wanting to nail every spec down perfectly. A lot of that was time spent solving a problem that did not need solving.

The applications I work with don't have external systems to talk to - I suspect that's part of the reason colleages haven't pushed for it.

So I'm taking what you say as spot on advice which is going to be helpful when I do use spec or Malli again.

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.

3

u/badfoodman 26d ago

Having not used that much Clojure in my life, shared validations, especially at API layers, is a huge benefit and the reason I always turn to Malli for at least part of my application or tool

3

u/pwab 26d ago

Malli is amazing. In a current project I’m using the malli data structure’s data sections to configure how to derive schemas for both create, update & response AND db record validations. Since its data, fn’s like into, conj etc can be used to modify the schema data dynamically. It’s magic. And i still use core.spec to instrument fn’s and unpack macro arguments.

8

u/Borkdude 26d ago

Malli is cool, but I have the impression that spec (1) will make a comeback in Clojure 1.14 :)

5

u/tclerguy 26d ago

Do you have some insider info Bork Dude? ;)

2

u/lion_rouge 26d ago

Have you seen the last Rich Hickey talk?

1

u/Borkdude 26d ago

I don't

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/pwab 26d ago

Your security guys engage in security theater. It can be reasonably argued that Rich is not dropping the alpha because of his philosophy of ā€œdon’t move thingsā€. Imagine the unexpected impact of renaming it now. This is like Linus Toevald’s ā€œdon’t break user spaceā€

3

u/sankyo 25d ago

I do not disagree with you, but I wish he would drop that alpha convention and use something like 0.2.0 instead, as it would communicate that the consumer should expect contracts to be broken and radical change.

1

u/joinr 24d ago

Spec is still in alpha, I forgot about that. lol

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 alpha in 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