forked from metosin/malli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,588 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/usr/bin/env bash | ||
# Should work if the env var is empty | ||
clojure -A:$CLOJURE_ALIAS -M:test -m kaocha.runner "$@" | ||
clojure -M:test -m kaocha.runner "$@" |
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,67 @@ | ||
# Constraints | ||
|
||
Typical nested schemas like `[:vector :string]` have corresponding | ||
levels in the values they describe: the `:vector` for the outer | ||
value and `:string for the first level of nesting. | ||
|
||
The exceptions are composite schemas like `[:and :int [:< 42]]` which | ||
describe the same value. This is problematic for generation: one | ||
schema must generate values and the other filter them, yielding brittle | ||
generators. Validators may also perform redundant checks, such has both | ||
`:int` and `[:< 42]` needing to check the class of the validated value. | ||
|
||
Constraints are intended to address this situation. A parent schema | ||
has other "constraint" schemas attached to them which may collaborate | ||
with the each other to yield reliable generators and lean validators. | ||
|
||
For example, `[:int {:max 41}]` is actually two schemas (when constraints are enabled): | ||
- the parent schema `:int` | ||
- a constraint `[:max 41]` | ||
|
||
## Reading this document | ||
|
||
This document assumes this has been evaluated in the current namespace: | ||
|
||
```clojure | ||
(require '[malli.core :as m] | ||
'[malli.constraint.protocols :as mcp] | ||
'[malli.constraint :as mc]) | ||
|
||
(defn constraint-options [] | ||
(-> {:registry (m/default-schemas)} | ||
mc/with-base-constraints)) | ||
``` | ||
|
||
## Activating Constraints | ||
|
||
Constraints are an opt-in Malli feature. | ||
|
||
To activate constraints locally, use `mc/with-base-constraints` to upgrade | ||
your options map. You can see an example in `constraint-options` above. | ||
|
||
To activate the base constraints globally, call `(malli.constraint/activate-base-constraints!)`. | ||
|
||
Constraints are themselves Schemas that also live in the registry. | ||
|
||
Behind the scenes, an atom `malli.constraint.extension/constraint-extensions` | ||
is used to configure constraints. The entire atom is ignored if `::m/constraint-options` | ||
WIPWIPWIP | ||
|
||
TODO rename constraint-options? or the atom? default-constraint-options? | ||
|
||
|
||
## Constraint vs Schema | ||
|
||
A constraint implements the same protocols as a Schema, and additionally | ||
`malli.constraint.protocols/Constraint`. | ||
|
||
Schemas that support schemas also implement | ||
`malli.constraint.protocols/ConstrainedSchema`. | ||
|
||
Validators on schemas ensure they check preconditions | ||
|
||
## Constraint Extensions Registry | ||
|
||
Constraints schema represents | ||
|
||
Constraint extensions are described as a schema in `malli.dev.constraint/ConstraintExtension`. |
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,33 @@ | ||
(ns malli.constraint) | ||
|
||
#?(:cljs (goog-define mode "on") | ||
:clj (def mode (or (System/getProperty "malli.constraint/mode") "on"))) | ||
|
||
(defprotocol Constraint | ||
(-constraint? [this]) | ||
(-constraint-form [this])) | ||
|
||
(defprotocol ConstrainedSchema | ||
(-constrained-schema? [this]) | ||
(-get-constraint [this]) | ||
(-set-constraint [this c])) | ||
|
||
(extend-type #?(:clj Object, :cljs default) | ||
Constraint | ||
(-constraint? [_] false) | ||
(-intersect [_ _ _]) | ||
|
||
ConstrainedSchema | ||
(-constrained-schema? [this] false) | ||
(-get-constraint [this]) | ||
(-set-constraint [this c])) | ||
|
||
(extend-type nil | ||
Constraint | ||
(-constraint? [_] false) | ||
(-constraint-form [_]) | ||
|
||
ConstrainedSchema | ||
(-constrained-schema? [this] false) | ||
(-get-constraint [this]) | ||
(-set-constraint [this c])) |
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,22 @@ | ||
(ns malli.constraint.extension) | ||
|
||
#_ | ||
[:atom [:map | ||
[:registry [:map-of Type IntoSchema]] | ||
[:extensions [:map-of Type [:map | ||
[:-walk | ||
{:optional true} | ||
[:=> [:maybe Schema] Schema Walker Path Constraint Options]]]]]]] | ||
(defonce ^:private constraint-extensions (atom {})) | ||
|
||
(defn get-constraint-extension [type] | ||
(get-in @constraint-extensions [:extensions type])) | ||
|
||
(defn get-constraint [type] | ||
(get-in @constraint-extensions [:registry type])) | ||
|
||
(defn register-constraints [reg] | ||
(swap! constraint-extensions update :registry #(merge % reg))) | ||
|
||
(defn register-constraint-extensions! [extensions] | ||
(swap! constraint-extensions update :extensions #(merge-with into % extensions))) |
Oops, something went wrong.