Skip to content

Commit

Permalink
Handle constrained maps when creating builder fns
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmbailey committed May 26, 2019
1 parent d9dfac6 commit 014d73a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ target/**
*~
pom.xml
.nrepl-port
.lsp
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. This change

## [Unreleased]

## [1.0.4] - 2019-05-26
### Fixed
- Handle constrained schema maps.

## [1.0.3] - 2019-05-19
### Fixed
- Don't eval schema forms in clojurescript.
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject travelboss/schema-plus "1.0.3"
(defproject travelboss/schema-plus "1.0.4"
:description "Adds easy mock generation and builder functions to plumatic/schema definitions"
:url "http://github.com/travelboss/schema-plus"
:license {:name "MIT"
Expand Down
12 changes: 8 additions & 4 deletions src/schema_plus/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
[clojure.string :as string]
[schema.core :as s]
[schema-generators.generators :as sg]
[clojure.test.check.generators :as cg]))
[clojure.test.check.generators :as cg])
(:import [schema.core Constrained]))

(def generator-registry
"Holds a map of Schema -> Generator for default generation behavior."
Expand Down Expand Up @@ -132,12 +133,15 @@
(str (namespace schema-name) "/+" (name schema-name)))

; don't eval in case of clojurescript, TODO: should find a better way of disabling this
my-form (if (:ns &env) schema-form (eval schema-form))
possibly-constrained-form (if (:ns &env) schema-form (eval schema-form))
real-form (if (instance? Constrained possibly-constrained-form)
(:schema possibly-constrained-form)
possibly-constrained-form)
; build a seq of [setter-fn-name field-name] for all fields
fn-names-and-keys (remove
nil?
(if (and make-builders? (map? my-form))
(for [k (keys my-form)]
(if (and make-builders? (map? real-form))
(for [k (keys real-form)]
(when (s/specific-key? k)
[(symbol (str base-name "-with-" (name (s/explicit-schema-key k))))
(s/explicit-schema-key k)]))
Expand Down
13 changes: 13 additions & 0 deletions test/schema_plus/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,19 @@
(declare +MyPerson-build)
(declare +MyPerson->)
(declare +MyPersonWithHat-build)
(declare +MyConstrainedMap)
(defschema+ MyPerson
{:name s/Str
:age s/Int
(s/optional-key :occupation) s/Str})
(defschema+ MyPersonWithHat
(merge MyPerson
{:hat-type s/Keyword}))
(defschema+ MyConstrainedMap
(s/constrained
{:a s/Int
:b s/Str}
(constantly true)))

(deftest test-builder-functions

Expand Down Expand Up @@ -263,6 +269,13 @@
(+MyPersonWithHat-with-hat-type :fedora)
(+MyPersonWithHat-build)))))

(testing "Builder functions on constrained maps."
(is (= {:a 1 :b "2"}
(-> (+MyConstrainedMap {})
(+MyConstrainedMap-with-a 1)
(+MyConstrainedMap-with-b "2")
(+MyConstrainedMap-build)))))

(testing "Handle non specific map schemas"
; only care that an exception isn't thrown
(defschema+ MyNonSpecificSchema
Expand Down

0 comments on commit 014d73a

Please sign in to comment.