[mvc-works/lilac "0.1.1-a2"]
(require '[lilac.core :refer [validate-lilac number+ string+ or+]])
(validate-lilac 1 (number+))
(validate-lilac 1
(or+ (number+) (string+)))
If validation is passed, it returns:
{:ok? true}
or the return value would contain informations why it's not correct:
{:ok? false
:data x
:rule x
:coord []
:message "failure reason..."
:formatted-message "formatted failure reason..."}
Supported APIs:
(:require [lilac.core :refer [validate-lilac deflilac register-custom-rule!
optional+ keyword+ boolean+ number+ string+ custom+ tuple+ vector+
list+ record+ not+ and+ map+ any+ set+ nil+ or+ is+]])
For example:
(validate-lilac 10 (and+ [(number+) (number+ {:min 0})]))
(validate-lilac 10 (or+ [(number+) (string+)]))
(validate-lilac nil (optional+ (number+)))
(validate-lilac
{:a 100, :b ["red" "blue"]}
(record+ {:a (number+)} {:exact-keys? true}))
(deflilac lilac-good-number+ (n) (number+ {:min n}))
Notice:
map+
for dictionaries.tuple+
is a vector with each item in specific type. Use :in-list?
for list instead of vector.Meanings of record options:
exact-keys?
, keys are exactly the same as rules, no more no fewercheck-keys?
, keys are inside the rules, no moreFor more details browse source code:
Lilac is designed to validate recursive data, based on a "component" concept behind deflilac
:
(require '[lilac.core :refer [deflilac record+ string+]])
(deflilac lilac-tree+ []
(record+ {:name (string+)
:children (vector+ (lilac-tree+))}))
To provide lilac.core/custom+
:
(defn method-1 [x]
(if (and (> x 10) (< x 20))
{:ok? true}
{:ok? false, :message (str "expects number between 10 amd 20, got " x)}))
(validate-lilac 11 (custom+ method-1))
To added custom validation type called method-2+
(something like number+
), use an API:
(defn validate-method-2 [data rule coord]
(if (and (> data 10) (< data 20))
{:ok? true}
{:ok? false,
:data data,
:rule rule,
:coord coord,
:message (str "expects number between 10 amd 20, got " data)}))
(defn method-2+ [] {:lilac-type :method-2})
(lilac.core/register-custom-rule! :method-2 validate-method-2)
(validate-lilac 11 (method-2+))
If you like the idea in Lilac, fork the project and develop on your own intention. This project does not accept large changes.
The project is developed with Cirru toolchains. Clojure code are compiled from calcit.cirru
. Make sure you are using Calcit Editor if you need the fix to be merged.
Since Lilac has APIs similar to number
or
and
vector
, which are core functions/variables in Clojure. I have to add prefix/suffix in names. Lilac uses suffix of +
in APIs, inspired by lilac.
MIT
Can you improve this documentation? These fine people already did:
jiyinyiyong, ChenYong & JonEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close