

Red Tape: a Clojure library for working with user-submitted forms - stevelosh
http://sjl.bitbucket.org/red-tape/

======
lkrubner
Some related topics:

Myles Megyesi has a good post called "Data Validation in Clojure"

[http://blog.8thlight.com/myles-megyesi/2012/10/16/data-
valid...](http://blog.8thlight.com/myles-megyesi/2012/10/16/data-validation-
in-clojure.html)

Megyesi points to this library:

[https://github.com/mylesmegyesi/metis](https://github.com/mylesmegyesi/metis)

"Metis is a library for data validation in Clojure inspired by Active Record
Validations."

There is also Formative:

[https://github.com/jkk/formative](https://github.com/jkk/formative)

"Web forms for Clojure and ClojureScript - rendering, parsing, and validating"

Pour:

[https://github.com/Kaali/pour](https://github.com/Kaali/pour)

"[Validates] form fields to specified constraints/validators and also to
convert incoming fields, which are strings, to valid data in the application
domain."

Validateur:

[https://github.com/michaelklishin/validateur](https://github.com/michaelklishin/validateur)

"Functional validations inspired by Ruby's ActiveModel
[http://clojurevalidations.info"](http://clojurevalidations.info")

What I would like to know is which of these are good, and what makes each
different from the other. I have not had time to test all of these libraries,
and there is no one library that has emerged as the dominant one in the
Clojure eco-system -- rather, we are still at the point where everyone is
doing their own thing.

Sandbar is more of a full framework but includes form handling:

[https://github.com/brentonashworth/sandbar](https://github.com/brentonashworth/sandbar)

Noir also handled form input but Noir is now deprecated.

The Clojure eco-system prefers small libraries as opposed to large frameworks,
and that is a good thing most of the time, but in the case of form validation
I have had a difficult time determining which libraries might be interesting
to use.

~~~
stevelosh
Yeah, there are a TON of Clojure form/data validation libraries out there. I
wasn't happy with any of them, which is why I made Red Tape.

Red Tape is designed specifically with user-submitted, web-based forms. It
might work with other stuff too, but that's what I had in mind for it.

All the existing libraries seem to suffer from one or more problems when
you're dealing with user-submitted, web-based forms.

Some of them are pure "validation" libraries. That doesn't work for web forms
because you want to not only validate that "34" is a valid user id or
whatever, but also _transform_ it into your representation of a user to make
it easier to work with. So pure validation isn't enough, we need to validate
and transform fields.

Some libraries use a complicated DSL of keywords and vectors and stuff to
decribe validations. Red Tape just uses functions, because functions are
really simple and easy to get your head around.

Almost _none_ of the libraries handle "initial data". This is one of the
things that was annoying as hell when I wrote django-goodfields, so I made
sure Red Tape had support for it built in right from the start.

Initial data is important when you're working with websites in the real world.
For example, you have a profile page for a user with their name, bio, etc.
When they first load the page you want to prepopulate that with the existing
data in your database. But if they change some of it and submit and there's an
error, you want to prepopulate the form with the data they submitted. Not the
data from your db, and _not the cleaned data_ , but the original strings they
submitted. As far as I could tell, none of the existing Clojure validation
libraries help you out here.

Another thing to consider is "dynamic" forms, where you have some extra data
that's not a form field, but that you need to use when validating a form. The
example in Red Tape's docs of a "video delete" form is a good one. Red Tape
uses its "form arguments" to handle this. Some libraries use a similar method,
and some ignore it entirely.

EDIT: A problem I forgot to mention is that some "form libraries" want to
handle generating the HTML for the form fields. Unless you're making a vanilla
Twitter Bootstrap site with the plainest HTML forms imagineable this ends up
being awful. In the real world your frontend developers will always want
control of their forms' markup, especially if they're using something more
dynamic like angular or backbone. Libraries that try to handle the HTML
generation tend to hide away stuff like "what should the initial data for this
field be" so you can't get to it as easily when you're trying to write the
HTML yourself. Red Tape is designed to make it as easy as possible for your
frontend developers to get what they need from your forms when writing their
markup.

And of course there's the perpetual Clojure problem of documentation. Half the
validation libraries out there have a 100-line README and no other docs.

So yeah, I definitely looked at the other options, but I couldn't find one
that satisfied all of the above problems to my liking. So I wrote it!

~~~
hga
I'm looking forward to trying Red Tape.

I previously found Metis to potentially be the best, but it's _fragile_ , uses
a DSL of vectors and if you e.g. spell one incorrectly it'll silently fail.
The DSL isn't very complicated, but the lack of robustness, when something
will only fire when input data is wrong, is seriously subpar.

~~~
stevelosh
Yeah, I've tried to err on the side of failing loudly when something is wrong
instead of silently, for example: [https://github.com/sjl/red-
tape/blob/master/src/red_tape/cor...](https://github.com/sjl/red-
tape/blob/master/src/red_tape/core.clj#L199-248)

