Skip to content

Commit

Permalink
[#38] Add nested schema defaults test
Browse files Browse the repository at this point in the history
  • Loading branch information
KGOH committed Feb 14, 2023
1 parent 4cf3d82 commit 36aeff2
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions test/zen/schema_test.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns zen.schema-test
(:require [zen.schema :as sut]
[matcho.core :as matcho]
[clojure.test :as t]
[clojure.string :as str]
[zen.core]))
Expand Down Expand Up @@ -88,6 +89,9 @@
(t/is (= ts-typedef-assert (str/join "" (distinct (::ts r)))))))


(defmethod sut/compile-key :my/defaults [_ _ _] {:priority -1})


(sut/register-compile-key-interpreter!
[:my/defaults ::default]
(fn [_ ztx defaults]
Expand All @@ -100,27 +104,63 @@
(t/deftest default-value-test
(t/testing "set default value"
(def ztx (zen.core/new-context {}))
(zen.core/get-tag ztx 'zen/type)

(def my-ns
'{:ns my

defaults
{:zen/tags #{zen/property zen/schema}
:type zen/boolean}
{:zen/tags #{zen/schema zen/is-key}
:zen/desc "only primitive default values are supported currently"
:for #{zen/map}
:priority 100
:type zen/map
:key {:type zen/keyword}
:values {:type zen/case
:case [{:when {:type zen/boolean}}
{:when {:type zen/date}}
{:when {:type zen/datetime}}
{:when {:type zen/integer}}
{:when {:type zen/keyword}}
{:when {:type zen/number}}
{:when {:type zen/qsymbol}}
{:when {:type zen/regex}}
{:when {:type zen/string}}
{:when {:type zen/symbol}}]}}

HumanName
{:zen/tags #{zen/schema}
:type zen/map
:require #{:family :given}
:keys {:given {:type zen/vector
:minItems 1
:every {:type zen/string}}
:family {:type zen/string}}}

DefaultHumanName
{:zen/tags #{zen/schema}
:type zen/map
:my/defaults {:family "None"}}

User
{:zen/tags #{zen/schema}
:type zen/map
:my/defaults {:active true}
:keys {:id {:type zen/string}
:name {:type zen/vector
:every {:confirms #{HumanName DefaultHumanName}}}
:active {:type zen/boolean}
:email {:type zen/string}}}})

(zen.core/load-ns ztx my-ns)

#_(matcho/match (zen.core/errors ztx) #_"NOTE: FIXME: keys that use get-cached during compile time won't be recompiled when these schemas used in get-cached updated. E.g. adding new is-key for zen/schema won't cause zen/schema recompile and the key won't be recognized by zen/schema validation"
empty?)

(def data
{:id "foo"
:email "bar@baz"})
:email "bar@baz"
:name [{:given ["foo"]}]})

(def r
(sut/apply-schema ztx
Expand All @@ -129,4 +169,9 @@
data
{:interpreters [::default]}))

(t/is (:active (::with-defaults r)))))
(matcho/match (::with-defaults r)
{:id "foo"
:email "bar@baz"
:active true
:name [{:family "None"
:given ["foo"]}]})))

0 comments on commit 36aeff2

Please sign in to comment.