Skip to content

Commit

Permalink
add :check-size? in tuple; alpha release
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Feb 13, 2020
1 parent 32f0203 commit 4125860
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Lilac: some validation functions in ClojureScript
[![Clojars Project](https://img.shields.io/clojars/v/mvc-works/lilac.svg)](https://clojars.org/mvc-works/lilac)

```edn
[mvc-works/lilac "0.1.1-a1"]
[mvc-works/lilac "0.1.1-a2"]
```

```clojure
Expand Down
310 changes: 261 additions & 49 deletions calcit.cirru

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>mvc-works</groupId>
<artifactId>lilac</artifactId>
<version>0.1.1-a1</version>
<version>0.1.1-a2</version>
<name>lilac</name>

<url>https://github.com/mvc-works/lilac</url>
Expand Down
24 changes: 22 additions & 2 deletions src/lilac/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,26 @@
(let [items (:items rule)
next-coord (conj coord 'tuple)
in-list? (:in-list? rule)
check-size? (:check-size? rule)
check-values (fn []
(loop [xs items, ys data, idx 0]
(if (empty? xs)
{:ok? true}
(if check-size?
(if (and (empty? ys) (= (count items) (count data)))
{:ok? true}
{:ok? false,
:coord next-coord,
:rule rule,
:data ys,
:message (get-in
rule
[:options :message]
(str
"expects tuple of "
(count items)
" items, got "
(count data)))})
{:ok? true})
(let [r0 (first xs)
y0 (first ys)
child-coord (conj next-coord idx)
Expand Down Expand Up @@ -531,7 +547,11 @@
([items] (tuple+ items nil))
([items options]
(assert (vector? items) "expects items of tuple+ in vector")
{:lilac-type :tuple, :items items, :options options, :in-list? (:in-list? options)}))
{:lilac-type :tuple,
:items items,
:options options,
:in-list? (:in-list? options),
:check-size? (:check-size? options)}))

(defn vector+
([item] (vector+ item nil))
Expand Down
8 changes: 7 additions & 1 deletion src/lilac/test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,13 @@
(=ok false (validate-lilac (list 1 "1" true) (tuple+ [(number+) (string+) (boolean+)])))))
(testing
"tuple not right type"
(is (=ok false (validate-lilac [1 "1" true] (tuple+ [(number+) (number+) (boolean+)]))))))
(is (=ok false (validate-lilac [1 "1" true] (tuple+ [(number+) (number+) (boolean+)])))))
(testing
"tuple not right type"
(is (=ok false (validate-lilac [1 "1"] (tuple+ [(number+)] {:check-size? true})))))
(testing
"tuple not right type"
(is (=ok true (validate-lilac [1 "1"] (tuple+ [(number+)] {:check-size? false}))))))

(deftest
test-vector
Expand Down
3 changes: 2 additions & 1 deletion src/lilac/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
(set? x) "a set"
(list? x) "a list"
(nil? x) "nil"
:else (str "Unknown(" (subs (str x) 0 10) ")")))
(seq? x) "a seq"
:else (str "Unknown: " (subs (str x) 0 10))))

(def type-of-re (type #"x"))

Expand Down

0 comments on commit 4125860

Please sign in to comment.