Schema validation and transformation, using simple functions. This is a library of boring functions that do mundane validation and transformation. It isn't meant to be a fancy introspective schema definition language.
This library is compatible with Clojure and ClojureScript.
Documentation: https://cljdoc.org/d/daaku/schema
The building block for schema
are validator functions. These take a
value, and return one of 3 things:
nil
to indicate the value should be dropped, and the rest of the
chain will not be run.(error ...)
which indicates an error, and the rest
of the chain will not be run.With this in mind, a simple validator could be:
(ns myapp
(:require [daaku.schema :as sc]))
(defn ensure-int [v]
(if (int? v)
v
(sc/error "not an int")))
Similarly, the pattern allows for transformation, for example:
(defn toggle [v]
(if (= "enable" v)
true
false))
The library provide a variety of predicates and transformers. They are all written as functions that return a validator, with the given options (if any).
Of course, you usually want to work with map
s. So here's how you would
do that:
(def countries #{"India", "Belgium", "USA"})
(def address-schema
(sc/schema {:street [(sc/required) (sc/string)]
:city [(sc/optional) (sc/string)]
:country [(sc/required) (sc/in countries)]}))
(def person-schema
(sc/schema {:name [(sc/required) (sc/string)]
:age [(sc/required) (sc/to-int)]
:address [(sc/optional) address-schema]}))
(def samples [{}
{:name "Yoda" :age "842"}
{:name "Yoda" :age "old"}
{:name "Yoda" :age 842 :address {}}])
(for [input samples]
(let [validated (person-schema input)]
(if (sc/error? validated)
(println "error: \n" (sc/error-value validated) "\n for: \n" input)
(println "success: \n" validated))))
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close