michaelklishin/validateur — pull request #23 by scttnlsn (2014-02-18 04:30:22+00:00)
Title: Predicate validation

Curious how you feel about introducing a validator that accepts arbitrary predicate functions. I find that I already have useful functions that return `true` or `false` but validateur expects something of the form `[valid? {field #{msg}}]`.

One potential solution:

```clojure
(defn validate [attribute predicate & {:keys [message] :or {message "is invalid"}}]
(fn [m]
(if (predicate m)
[true {}]
[false {attribute #{message}}])))
```

Example usage:

```clojure
(validate :email unique-email? :message "is already taken")
```

Similarly it would be nice to be able to apply validators selectively:

```clojure
(presence-of :password :when new-user?)
```

As it is currently, I believe the `:when predicate` would have to be added separately to each validator. Perhaps some of the `:message`/`:message-fn` (and perhaps `:when`) handling could be extracted into something more reusable.

Alternatively, I'm currently using:

```clojure
(defn validate-when [predicate validator]
(fn [m]
(if (predicate m)
(validator m)
[true {}])))
```

Example usage:

```clojure
(validate-when new-user? (presence-of :password))
```

I'd be happy to submit a pull request if any of this is something you're interested in including.