Skip to content

Commit

Permalink
check option keys
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Mar 7, 2020
1 parent c94a77f commit 827dadd
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 24 deletions.
186 changes: 173 additions & 13 deletions calcit.cirru

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "jiyinyiyong <jiyinyiyong@gmail.com>",
"license": "MIT",
"devDependencies": {
"shadow-cljs": "^2.8.90",
"shadow-cljs": "^2.8.91",
"source-map-support": "^0.5.16",
"ws": "^7.2.1"
}
Expand Down
2 changes: 1 addition & 1 deletion shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
; "clojars" {:url "https://mirrors.ustc.edu.cn/clojars/"}
; }
:dependencies [
[medley "1.2.0"]
[medley "1.3.0"]
]
:builds {
:app {
Expand Down
13 changes: 10 additions & 3 deletions src/lilac/core.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

(ns lilac.core
(:require-macros [lilac.core])
(:require [lilac.util :refer [re?]]
[lilac.util :refer [preview-data]]
(:require [lilac.util :refer [re? preview-data check-keys]]
[clojure.string :as string]
[clojure.set :refer [difference]]))

Expand Down Expand Up @@ -42,7 +41,9 @@

(defn any+
([] (any+ nil))
([options] {:lilac-type :any, :options options, :some? (:some? options)}))
([options]
(check-keys "checking any+" options [:some?])
{:lilac-type :any, :options options, :some? (:some? options)}))

(defn boolean+ ([] (boolean+ nil)) ([options] {:lilac-type :boolean}))

Expand Down Expand Up @@ -513,6 +514,7 @@
(defn list+
([item] (list+ item nil))
([item options]
(check-keys "checking list+" options [:allow-seq?])
{:lilac-type :list, :item item, :options options, :allow-seq? (:allow-seq? options)}))

(defn map+
Expand All @@ -529,6 +531,7 @@
(defn number+
([] (number+ nil))
([options]
(check-keys "checking number+" options [:max :min])
{:lilac-type :number, :max (:max options), :min (:min options), :options options}))

(defn optional+
Expand All @@ -546,6 +549,7 @@
(defn record+
([pairs] (record+ pairs nil))
([pairs options]
(check-keys "checking record+" options [:exact-keys? :check-keys? :all-optional?])
{:lilac-type :record,
:pairs pairs,
:options options,
Expand All @@ -566,6 +570,7 @@
(defn string+
([] (string+ nil))
([options]
(check-keys "checking string+" options [:nonblank?])
{:lilac-type :string,
:re (:re options),
:nonblank? (:nonblank? options),
Expand All @@ -577,6 +582,7 @@
([items] (tuple+ items nil))
([items options]
(assert (vector? items) "expects items of tuple+ in vector")
(check-keys "checking tuple+" options [:in-list? :check-size?])
{:lilac-type :tuple,
:items items,
:options options,
Expand All @@ -586,4 +592,5 @@
(defn vector+
([item] (vector+ item nil))
([item options]
(check-keys "checking vector+" options [:allow-seq?])
{:lilac-type :vector, :item item, :options options, :allow-seq? (:allow-seq? options)}))
2 changes: 1 addition & 1 deletion src/lilac/main.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
(defn run-demo! []
(let [result (validate-lilac router-data (lilac-router+))]
(if (:ok? result) (println "Passed validation!") (println (:formatted-message result)))
(dev-check "1" (number+))))
(dev-check "1" (number+ {:x 1}))))

(defn main! [] (println "Started.") (run-demo!))

Expand Down
2 changes: 1 addition & 1 deletion src/lilac/router.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
:put (lilac-method+),
:delete (lilac-method+),
:next (optional+ (vector+ (lilac-router-path+)))}
{:valid-keys? true}))
{:check-keys? true}))

(deflilac
lilac-router+
Expand Down
6 changes: 6 additions & 0 deletions src/lilac/util.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@

(ns lilac.util )

(defn check-keys [message data xs]
(let [valid-keys (set xs), real-keys (keys data)]
(doseq [k real-keys]
(when (not (contains? valid-keys k))
(js/console.warn message "unexpected key" (pr-str k) ", expect" (pr-str valid-keys))))))

(defn preview-data [x]
(cond
(string? x) (pr-str x)
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 827dadd

Please sign in to comment.