r/Clojure • • Oct 08 '20

Introducing Malli, a new data validation and specification library for Clojure/Script

After a year in incubation, we are proud to present Malli, a new data validation and specification library for Clojure/Script! Big thanks to Clojurists Together for helping to ship it out.

Introduction post: https://metosin.fi/blog/malli/

In short, Malli covers:

  • schema definitions, as data

  • support for open and closed maps

  • data validation and error reporting

  • humanised and localised error messages

  • spell-checking of map and multi keys

  • bi-directional value transformation

  • value and schema generation

  • tools for programming with schemas

  • immutable, mutable, dynamic, lazy and composite registries

  • support for JSON Schema, Swagger and DOT

  • open apis for both end users and extenders

Coordinates:

On behalf of Metosin and all contributors,

Tommi

107 Upvotes

13 comments sorted by

2

u/TheLastSock Oct 09 '20

Does this do generative testing? Is that what "value and scheme generation" means?

4

u/ikitommi Oct 09 '20 edited Oct 09 '20

Malli integrates with test.check, so whatever utilities work with that, work with Malli, but there are no extra testing helpers in the library itself, could be.

Both value generation & schema generation are described in the post, but in short the latter is the inverse of the first: Malli ships with a Schema Inferrer and has also a special registry type, lazy-registry, which allows pulling in Schemas on-demand from anywhere, in spirit of F# Type Providers.

The inferrer is quite naive (69loc), but works:

(require '[malli.provider :as mp])

(mp/provide
  [{:id 1, :name "Liisa", :address {:street "Rantatie"}}
   {:id 2, :name "Tiina", :address {:street "Hämeenkatu", :zip 33100}}])
;[:map 
; [:id int?] 
; [:name string?] 
; [:address [:map 
;            [:street string?] 
;            [:zip {:optional true} int?]]]]

16

u/lucywang000 Oct 09 '20 edited Oct 09 '20

Bravo!

When designing the declarative syntax for the clj-statecharts library, Malli has greatly help me to design it, and reduced the code I need to write to verify the state machine declaration and do necessary conversion of a state machine specification to a canonical format.

For example both "{:actions send-request}" and "{:actions [send-request]}" are acceptable, but the former is always converted into the latter format. With malli's transformer/decode/encode support I could do declare any conversions alongside the schema itself.

Also since a statecharts could have any level of nested child states, the recursive schema of malli is so powerful that implementing that uses less than 15 lines of code!

Ofc these could be also done with clojure.spec, but malli is much more flexible after evaluating the two. For instance, with spec it's not possible to have one map schema M and two "child" schemas M1 and M2 that both only add one extra key to M (you have to repeat the "base schema" three times for M/M1/M2!), but in malli it feels like a breeze since it's all data.

Another use case is using malli to describe the shape of data in re-frame's app-db. I find writing the shape of data (and sometimes their constraints using malli's ":fn" schema) before coding all the events/subs would greatly help me design a module, even if the shape is constantly evolving - just like TDD, but in a lower level.

Also I find it extremely helpful to add a global interceptor in re-frame to validate the app-db matches the schema after each event (guarded with goog.DEBUG so stripped in production builds).

12

u/dustingetz Oct 08 '20

Roadmap:

  • Better support for dates - needed in day-to-day projects

  • Declarative schema manipulation - just because it's possible

  • Sequence schemas - enabling schema-based destructuring

  • Schema defn syntax - for domain code, enables linting

  • Full clj-kondo integration - a linter that sparks joy

  • Validating schema properties and children using Malli

  • Two-way bindings for JSON Schema

1

u/agumonkey Oct 08 '20

is a sequence schema useful to handle a protocol ?

1

u/Borkdude Oct 09 '20

I think it's useful to specify function arguments?

8

u/bowmhoust Oct 08 '20

On first glance I love it! It even comes with DOT visualization and Swagger/JSON Schema export, how cool is that?

6

u/_amogh_ Oct 09 '20

Thank you! Huge respect for Metosin! Few days ago I was trying to attempt API validation with clojure.spec. The error reporting is really hard to workaround. I had come to this conclusion that error to validation has to be done from the ground up. Seemed like a lot of effort!

Glad that they did it!
Thank you so much! 🙏🙏🙏

9

u/stingraycharles Oct 08 '20

I have mixed feelings about this. In a way, I’m sad because it may cause more fragmentation, as this directly “competes” with spec. On the other hand, development of spec is going extremely slow, with quite a few rough edges and deficiencies in the current alpha, so I’m very excited to see this happening and curious to look at its potential.

How exactly should I compare this library against spec?

Regardless, thanks a lot for all the effort that went into this!

11

u/dustingetz Oct 08 '20

Alex says Spec 2 is still in the hammock so if I were looking for a place to start today I would start with Malli

7

u/bowmhoust Oct 08 '20

How exactly should I compare this library against spec?

There's a section on this in the Blog Post

2

u/stingraycharles Oct 08 '20

Thank you very much, that answers my question perfectly.