Skip to content

Commit

Permalink
move map keys to map schema
Browse files Browse the repository at this point in the history
  • Loading branch information
niquola committed Nov 12, 2020
1 parent ad16e0a commit a00d9f8
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 54 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,10 @@ Apply clojure.spec regular expressions for collections!!!!
* :nth {integer: schema} - apply schema to nth element
* :minItems & :maxItems - min/max items in collection
* :filter - TODO: apply filter to collection, then apply schema to


## EDN parser

Comming with basis for LSP for zen models.

TODO: see https://github.com/borkdude/edamame
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{:paths ["src" "pkg" "test"]
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
borkdude/edamame {:mvn/version "0.0.11-alpha.15"}
healthsamurai/matcho {:mvn/version "0.3.7"}}

:aliases
Expand Down
50 changes: 31 additions & 19 deletions pkg/zen.edn
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
:type map
:keys {:minLength {:type integer
:min 0
:desc "inclusive minimal length of string"}
:zen/desc "inclusive minimal length of string"}
:maxLength {:type integer :min 0}
:regex {:type regex}}}

Expand Down Expand Up @@ -85,37 +85,49 @@

map {:zen/tags #{coll schema}
:type map
:keys {:values {:type map :confirms #{schema}}}}
:keys {:values {:type map :confirms #{schema}}
:schema-key {:type map
:keys {:key {:type keyword}
:resolve {:type map :values {:type symbol}}}
:require #{:key}}
:keys {:type map
:values {:type map
:confirms #{schema}
:keys {:type {:type symbol}}}}
:require {:type set :every {:type keyword}}}}

;; map {:zen/tags #{coll schema}
;; :type map
;; :keys {:values schema}}

schema {:zen/tags #{schema}
:zen/desc "zen schema"
:type map
:schema-key {:key :type}
:keys {:type {:type symbol}
:confirms {:type set :every {:type symbol}}
:desc {:type string}

:schema-key {:type map
:keys {:key {:type keyword}
:resolve {:type map :values {:type symbol}}}
:require #{:key}}
;; part of map
:keys {:type map
:values {:type map
:confirms #{schema}
:keys {:type {:type symbol}}}}
:require {:type set :every {:type keyword}}}
:confirms {:type set :every {:type symbol}}}
:require [:type]}

resource {:zen/tags #{schema}
:type map
:keys {:id {:type string}
:resourceType {:type string}}}

;; ctx {:zen/tags #{schema}
;; :zen/desc "schema for meta-store context"
;; :keys {:tags {:type map :value {:type map :confirms #{tag}}}
;; :symbols {:type map}}}

ctx {:zen/tags #{schema}
:zen/desc "schema for context"
:type map
:keys {:tags {:type set :every {:type symbol}}
:symbols {:type map}}}

operation {:zen/tags #{schema}
:zen/desc "Basic operation schema"
:type map
:keys {:params {:type map :confirms #{schema}}
:result {:type map :confirms #{schema}}
:middleware {:type map
:keys {:before {:type vector}
:after {:type vector}}}}}

;; reference {:zen/tags #{schema}
;; :type map
Expand Down
21 changes: 0 additions & 21 deletions pkg/zen/http.edn

This file was deleted.

1 change: 1 addition & 0 deletions src/zen/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(def load-ns zen.store/load-ns)
(def read-ns zen.store/read-ns)
(def new-context zen.store/new-context)

(defn validate [ctx symbols data]
(zen.validation/validate ctx symbols data))

Expand Down
6 changes: 3 additions & 3 deletions test/myapp.edn
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{:ns myapp

email {:zen/tags #{:zen/schema}
email {:zen/tags #{zen/schema}
:zen/desc "email type"
:type zen/string
:regex #"^.*@.*$"}

User {:zen/tags #{:zen/schema}
User {:zen/tags #{zen/schema}
:type zen/map
:keys {:id {:type zen/string}
:name {:type zen/string}
:password {:type zen/string}
:email {:type email}}}

admin {:zen/tags #{:myapp/User}
admin {:zen/tags #{User}
:zen/desc "Administrator record"
:id "admin"
:name "admin"
Expand Down
11 changes: 7 additions & 4 deletions test/zen/store_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(ns zen.store-test
(:require [zen.core :as sut]
[clojure.test :refer [deftest is]]))
[clojure.test :refer [deftest is]]
[edamame.core]
[clojure.java.io :as io]))


(deftest core-schema-test
Expand All @@ -19,9 +21,10 @@

(keys @ctx)




(comment
(doseq [[k v] (edamame.core/parse-string
(slurp (.getPath (io/resource "zen.edn"))))]
(println k (meta k) "\n " (meta v) v)))


)
Expand Down
15 changes: 8 additions & 7 deletions test/zen/validation_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@
[{:message "Expected type of 'map, got 1",
:type "type",
:path [:keys],
:schema ['zen/schema :keys]}
nil?]})
:schema ['zen/schema :schema-key 'zen/map :keys]}]})

(vmatch #{'myapp/Settings} {:headers {:a "str"}}
{:errors empty?})
Expand All @@ -218,20 +217,22 @@
:schema ['myapp/Settings :headers :content-type :minLength]}
nil?]})

(vmatch #{'zen/schema} {:keys {:prop 1}}
(vmatch #{'zen/schema} {:type 'zen/map :keys {:prop 1}}
{:errors
[{:message "Expected type of 'map, got 1",
:type "type",
:path [:keys :prop],
:schema ['zen/schema :keys :values]}]})
:schema ['zen/schema :schema-key 'zen/map :keys :values]}]})

(vmatch #{'zen/schema} {:keys {:ups {:type 'zen/string
(vmatch #{'zen/schema} {:type 'zen/map
:keys {:ups {:type 'zen/string
:minLength "ups"}}}
{:errors
[{:message "Expected type of 'integer, got 'string",
:type "primitive-type",
:path [:keys :ups :minLength],
:schema ['zen/schema :keys :values 'zen/schema :schema-key 'zen/string :minLength]}]})
:schema
['zen/schema :schema-key 'zen/map :keys :values 'zen/schema :schema-key 'zen/string :minLength]}]})

(vmatch #{'zen/schema}
{:type 'zen/map
Expand All @@ -250,7 +251,7 @@
[{:message "Expected type of 'integer, got 'string",
:type "primitive-type",
:path [:keys :maxLength :min],
:schema ['zen/schema :keys :values 'zen/schema :schema-key 'zen/integer :min]}
:schema ['zen/schema :schema-key 'zen/map :keys :values 'zen/schema :schema-key 'zen/integer :min]}
{:type "unknown-key",
:message "unknown key :ups",
:path [:keys :minLength :ups]}]})
Expand Down

0 comments on commit a00d9f8

Please sign in to comment.