A validation library for Clojure.
Clojure
Latest commit 086ed90 Oct 2, 2013 @r0man lein-release plugin: bumped version from 0.5.6 to 0.5.7-SNAPSHOT for …
…next development cycle
Permalink
Failed to load latest commit information.
src/validation
test/validation
.gitignore
.travis.yml
README.md
project.clj

README.md

VALIDATION-CLJ Build Status

A simple validation library for Clojure.

Installation

Via Clojars: http://clojars.org/validation-clj

Usage

Need some records validated?

(def *alice* {:nick "alice" :email "alice"})

(def *bob*
  {:nick "bob"
   :email "bob@example.com"
   :password "secret"
   :password-confirmation "secret"})

Use the validation library.

(use 'validation.core 'validation.errors)

Define a validation which generates the valid-user?, validate-user and validate-user! functions.

(defn new-user? [user]
  (nil? (:id user)))

(defvalidate user
  (presence-of :nick)
  (min-length-of :nick 2)
  (max-length-of :nick 16)
  (presence-of :email)
  (is-email :email)
  (presence-of :password :if new-user?)
  (confirmation-of :password :if new-user?))

The valid-user? fn checks if the record is valid or not.

(valid-user? *alice*)
;=> false

(valid-user? *bob*)
;=> true

The validate-user fn returns the record itself with possible error messages attached to the metadata.

(validate-user *alice*)
;=> {:nick "alice"}

(validate-user *bob*)
;=> {:nick "bob"
;    :email "bob@example.com"
;    :password "secret"
;    :password-confirmation "secret"}

The error-messages reads the error messages from the meta data of the validated record.

(error-messages (validate-user *bob*))
;=> nil

(error-messages (validate-user *alice*))
;=> {:email ["is not a valid email address." "can't be blank."]
;    :password ["can't be blank."]}

The validate-user! fn is similar to validate-user, but uses the error-kit condition system to signal validation errors.

(use 'slingshot.core)

(try+
  (validate-user! *alice*)
  (catch validation.error {user :record errors :errors}
    errors))
;=> {:email ["is not a valid email address." "can't be blank."]
;    :password ["can't be blank."]}

(validate-user! *bob*)
;=> {:nick "bob"
;    :email "bob@example.com"
;    :password "secret"
;    :password-confirmation "secret"}

For anything else look at the tests ...

License

Copyright (C) 2013 Roman Scherer

Distributed under the Eclipse Public License, the same as Clojure.