Database Reference
In-Depth Information
Valip ( https://github.com/weavejester/valip ) provides this. It's aimed at validating
input from web forms, so it expects to validate maps with string values. We'll need to work
around this expectation a time or two, but it isn't dificult.
Getting ready
We need to make sure that the Valip library is in our Leiningen project.clj ile:
(defproject cleaning-data "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/data.xml "0.0.8"]
[valip "0.2.0"]])
Also, we need to load it into our script or REPL:
(use 'valip.core
'valip.predicates)
How to do it…
To validate some data, we have to deine predicates to test the data ields against, then deine
the ields and predicates to validate, plus validate error messages.
1.
First, we need data to validate:
(def user
{:given-name "Fox"
:surname "Mulder"
:age 51
:badge "JTT047101111"})
2. We also need to deine a predicate to determine whether a number is present or
not. The present? predicate deined by Valip fails if its input isn't a string:
(defn number-present? [x]
(and (present? (str x))
(or (instance? Integer x)
(instance? Long x))))
3.
We'd also like to validate the badge numbers. Taking this one as a template,
let's say they begin with three uppercase letters followed by one or more digits.
We can express this using this predicate:
(defn valid-badge [n]
(not (nil? (re-find #"[A-Z]{3}\d+" n))))
 
Search WWH ::




Custom Search