https://github.com/bluemont/validata.git
git clone 'https://github.com/bluemont/validata.git'
(ql:quickload :bluemont.validata)
Validata is a Clojure library to validates maps. Its goals are:
There are many alternatives for validation in Clojure; I list some alternatives below. My goal is to make validata clean and simple relative to some of the other options.
Add this to your project.clj
dependencies:
[validata "0.1.8"]
Here is an example:
(ns example.core
(:require [validata.core :as v]))
(def validations
{:uuid [v/uuid-string v/required]
:name [v/string v/required]
:notes [v/string]
:created-at [v/timestamp-string]
:updated-at [v/timestamp-string]})
(v/errors {} validations)
; {:uuid ["key is required"], :name ["key is required"]}
(v/errors {:uuid "e0da523c-fdfc-46d5-bf6d-a895dd3235c1"} validations)
; {:name ["key is required"]}
(v/errors {:uuid "e0da523c-fdfc-46d5-bf6d-a895dd3235c1"
:name "validata"
:notes 2.7128} validations)
; {:notes ["value must be a string"]}
(v/errors {:uuid "e0da523c-fdfc-46d5-bf6d-a895dd3235c1"
:name "validata"} validations)
; {}
(v/valid? {:uuid "e0da523c-fdfc-46d5-bf6d-a895dd3235c1"} validations)
; false
(v/valid? {:uuid "e0da523c-fdfc-46d5-bf6d-a895dd3235c1"
:name "validata"} validations)
; true
To display errors for extra (unexpected keys), use this form of errors
:
(v/errors {:uuid "ed2b4b7d-1111-47e1-aa79-5b204758cd01"
:name "validata"
:junk 42} validations false)
; {:junk [:error "key is unexpected"]}
Clojure has many validation libraries; here are a few I've looked at:
The libraries have varying goals and approaches; take a look and give them a try. Please let me know if you see ideas that would benefit this project.
Copyright 2014 Bluemont Labs LLC
Distributed under the Eclipse Public License, the same as Clojure.