From a00d9f8ad85b86077edb875043f739b03b9b1d26 Mon Sep 17 00:00:00 2001 From: niquola Date: Thu, 12 Nov 2020 10:16:10 +0300 Subject: [PATCH] move map keys to map schema --- README.md | 7 +++++ deps.edn | 1 + pkg/zen.edn | 50 ++++++++++++++++++++++-------------- pkg/zen/http.edn | 21 --------------- src/zen/core.clj | 1 + test/myapp.edn | 6 ++--- test/zen/store_test.clj | 11 +++++--- test/zen/validation_test.clj | 15 ++++++----- 8 files changed, 58 insertions(+), 54 deletions(-) delete mode 100644 pkg/zen/http.edn diff --git a/README.md b/README.md index db8d49e..d5dbf6c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/deps.edn b/deps.edn index 39e706d..254fb78 100644 --- a/deps.edn +++ b/deps.edn @@ -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 diff --git a/pkg/zen.edn b/pkg/zen.edn index 96955fd..0bbd478 100644 --- a/pkg/zen.edn +++ b/pkg/zen.edn @@ -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}}} @@ -85,26 +85,27 @@ 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} @@ -112,10 +113,21 @@ :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 diff --git a/pkg/zen/http.edn b/pkg/zen/http.edn deleted file mode 100644 index 783a2f9..0000000 --- a/pkg/zen/http.edn +++ /dev/null @@ -1,21 +0,0 @@ -{ns zen.http - - api {types #{zen/schema}} - - server {types #{zen/schema}} - - op {types #{zen/schema} :isa #{zen/op}} - - metastore-op {types #{zen/schema} - :isa #{zen/op op}} - - store-api {types #{api}} - - search {types #{zen/schema} - :isa #{op} - :keys {:search-params {:isa #{zen/map}}}} - - static-resource {types #{zen/schema} - :isa #{zen/op op}} - -} diff --git a/src/zen/core.clj b/src/zen/core.clj index ff65dd2..7200d6e 100644 --- a/src/zen/core.clj +++ b/src/zen/core.clj @@ -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)) diff --git a/test/myapp.edn b/test/myapp.edn index 6ba27b1..ca5b944 100644 --- a/test/myapp.edn +++ b/test/myapp.edn @@ -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" diff --git a/test/zen/store_test.clj b/test/zen/store_test.clj index d65cda3..ce91740 100644 --- a/test/zen/store_test.clj +++ b/test/zen/store_test.clj @@ -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 @@ -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))) ) diff --git a/test/zen/validation_test.clj b/test/zen/validation_test.clj index 47d531d..6d31c18 100644 --- a/test/zen/validation_test.clj +++ b/test/zen/validation_test.clj @@ -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?}) @@ -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 @@ -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]}]})