This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-01-29
Channels
- # announcements (13)
- # aws-lambda (19)
- # babashka (11)
- # beginners (90)
- # calva (35)
- # chlorine-clover (16)
- # cider (8)
- # clj-kondo (6)
- # cljsrn (10)
- # clojure (44)
- # clojure-australia (1)
- # clojure-europe (35)
- # clojure-nl (6)
- # clojure-norway (7)
- # clojure-uk (101)
- # clojurescript (79)
- # community-development (40)
- # conjure (1)
- # contributions-welcome (4)
- # core-logic (2)
- # cursive (6)
- # datomic (4)
- # figwheel-main (5)
- # fulcro (18)
- # girouette (1)
- # graalvm (3)
- # hoplon (2)
- # hugsql (2)
- # jobs (5)
- # malli (26)
- # off-topic (74)
- # pathom (36)
- # portal (4)
- # reagent (10)
- # reitit (23)
- # remote-jobs (1)
- # shadow-cljs (27)
- # spacemacs (10)
- # sql (3)
- # startup-in-a-month (6)
- # tools-deps (55)
- # vim (11)
- # xtdb (15)
what order do :and
schemas operate in
is it possible to do
(m/explain [:and
int?
(m/-simple-schema
{:pred #(> % 3)})] "asdf")
and have the int?
check prevent the pred from running and erroring?
like in spec
(s/def ::greater-than-3
(s/and int? #(> % 3)))
#'user/foo
user=> (m/validate foo "abcd")
false
user=> (m/validate foo 1)
false
user=> (m/validate foo 3)
false
user=> (m/validate foo 4)
true
interesting, I wonder why :fn
works but -simple-schema
doesn't
cool, I'll use :fn
🙂
user=> (def foo2 [:and int? (m/-simple-schema {:pred #(> % 3)})])
#'user/foo2
user=> (m/validate foo2 4)
true
user=> (m/validate foo2 3)
false
user=> (m/validate foo2 1)
false
user=> (m/validate foo2 "and")
false
user=>
@dharrigan, validate succeeds, but explain fails
could be a bug perhaps?
(m/explain foo2 "and")
=> java.lang.ClassCastException
I guess it does makes sense that explain on an :and
would give all the reasons something fails, but it doesn't make sense -simple-schema
fails where :fn
doesn't.
If you are just checking >
you could also use [:int {:min 3}]
or [:and int? [:>= 3]]
.
First one works best with JSON Schema
ah, the actual example here is a bit different, it's for a kebab case keyword
[:and
{:description "A keyword joined by hyphens"
:swagger/description "A string joined by hyphens"
:swagger/example "kebab-case-string"
:error/message "should be a kebab-case keyword"}
keyword?
[:fn kebab-case-keyword?]]
(where in this case kebab-case-keyword? fails on non-keywords)
WIP: Schemas of Schemas:
(-> (m/-simple-schema
{:type :int
:pred string?
:properties-schema [:map [:min nat-int?] [:max nat-int?]]
:property-pred (m/-min-max-pred nil)})
(m/properties-schema))
; => [:map [:min nat-int?] [:max nat-int?]]
also:
(let [OneOf (m/-simple-schema
(fn [{:keys [count]} values]
(let [value? (set values)]
{:type :user/over
:pred value?
:properties-schema [:map [:count [:= count]]]
:children-schema (into [:tuple] (map (fn [x] [:= x]) values))
:type-properties {:error/message (str "should be one of " values)
:json-schema {:type "oneOf", :values values}}})))
schema (m/schema [OneOf {:count 6} 1 2 3 4 5 6])]
{:properties-schema (m/properties-schema schema)
:children-schema (m/children-schema schema)})
;{:properties-schema [:map [:count [:= 6]]]
; :children-schema [:tuple [:= 1] [:= 2] [:= 3] [:= 4] [:= 5] [:= 6]]}