-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split validator namespace into internal and external.
- Loading branch information
David Frese
committed
Mar 20, 2024
1 parent
58e3fe1
commit 7a7de63
Showing
5 changed files
with
49 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
(ns active.data.struct.internal.validator) | ||
|
||
(defprotocol IMapValidator | ||
"Note: Both methods are called on every construction and change; | ||
usually only one of them needs to be implemented, resp. the other as | ||
`(constantly nil)`." | ||
|
||
(-validate-field! [this changed-key new-value] | ||
"Throws an exception if the new value is not acceptable for the given key.") | ||
(-validate-map! [this m] | ||
"Throws an exception if an invariant is broken in the given map." | ||
;; Note: m can be any kind of map; not guaranteed to be a | ||
;; struct-map. | ||
)) | ||
|
||
(defprotocol IDeferredMapValidator | ||
(-get-validator! [this] "Return the the actual IMapValidator, or nil. Called once for each create/update of a map.")) | ||
|
||
(defn resolve! [t] | ||
(if (satisfies? IDeferredMapValidator t) | ||
(-get-validator! t) | ||
t)) | ||
|
||
(defn validate-single! [t key value] | ||
(-validate-field! t key value)) | ||
|
||
(defn validate-map-only! [t m] | ||
(-validate-map! t m)) | ||
|
||
(defn validate! | ||
([t m] | ||
(assert (map? m)) | ||
(validate! t m (keys m) (vals m))) | ||
([t m changed-keys changed-values] | ||
(dorun (map (partial -validate-field! t) | ||
changed-keys | ||
changed-values)) | ||
(-validate-map! t m))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters