I am trying to use :*
and got this message {:type :malli.core/invalid-schema, :data {:schema :*}}
there is some setup to enable regex-like support?
I think https://github.com/metosin/malli/blob/a58e04b265b1b6658bb9fe791fd103cfab452e53/src/malli/core.cljc#L1873 (sequence-schemas) happened after the last release hehe
If you are using tools-deps you can reference a github commit.
Hi all,
I’m trying to define a map with several optional fields, that at least 1 of them must exist.
For example, :a
and :b
are optional, :c
is required.
{:a 1 :c 1} ; valid
{:b 1 :c 1} ; valid
{:a 1 :b 1 :c 1} ; valid
{:c 1} ; nope, invalid
As a reference, with `spec` , I’d write something like
(s/def ::my-map
(s/keys :req-un [::c (or ::a ::b)]))
@evg.tso you can compose with :and
, see example here: https://malli.io/?value=%7B%3Ax%201%2C%20%3Ay%202%7D&schema=%5B%3Aand%0A%20%5B%3Amap%20%5B%3Ax%20int%3F%5D%20%5B%3Ay%20int%3F%5D%5D%0A%20%5B%3Afn%0A%20%20%7B%3Aerror%2Fmessage%20%22x%20should%20be%20greater%20than%20y%22%7D%0A%20%20(fn%20%5B%7B%3Akeys%20%5Bx%20y%5D%7D%5D%20(%3E%20x%20y))%5D%5D
The thing with :and
is that is doesn’t survive mu/merge
very well.
To be more specific, my use case is with reitit
.
There are several routes with common fields, but each route has specific fields that are different from one another.
• POST /share/this
would accept either :a
or :b
and must contain :c
(for example {:a 1 :c 1}
• POST /share/that
would accept either :a
or :b
and must contain :d
(for example {:a 1 :d 1}
.
└── schema with optional fields (at least one must exist)
├── mu/merge-ed schema with specific fields
└── mu/merge-ed schema with specific fields
(mu/merge
[:map [:specific-field string?]]
[:and
[:map [:x int?] [:y int?]]])
=> [:and [:map [:x int?] [:y int?]]]
I see. What if :and
was considered as a constrained-kinda thing, where the first value is the actual schema and the rest are just extra rules for that. Would solve a lot of things. :thinking_face:
need to check would it break existing contracts or not.
would this be more correct:
(mu/merge
[:map [:specific-field string?]]
[:and
[:map [:x int?] [:y int?]]
map?]])
=> [:and [:map [:spesific-field string?] [:x int?] [:y int?]] map?]
That would be great!
In most of my schemas I use :and
to add additional data like a :fn
or a custom error message.
[metosin/malli "0.3.0"]
🥳 🥳 🥳
Hi! I’m using malli together with reitit and reitit-swagger and I found a problem when I start using custom malli’s registry. tldr; generated swagger partials for an endpoint contains “definitions” key which is not valid according to swagger2 spec. also it messes up with references trying to point to one of defined schemas. As a workaround I added a middleware to walk throw generated object which will be encoded as json and pull all definitions to the top level but it looks ugly and not generic enough. Also there is a small inconsistency in encoding keywords to json.
https://malli.io/?value=%5B1%20%5B2%20%5B3%20%5B4%20nil%5D%5D%5D%5D&schema=%5B%3Aschema%0A%20%7B%3Aregistry%20%7B%3Afoo%2FConsCell%20%5B%3Amaybe%20%5B%3Atuple%20%3Aint%20%5B%3Aref%20%3Afoo%2FConsCell%5D%5D%5D%7D%7D%0A%20%3Afoo%2FConsCell%5D
here is an illustration — :$ref key contains a string "#/definitions/:foo/ConsCell"
with :
inside. and there is a key :foo/ConsCell
in :definitions
after encoding to json this schema will be invalid because of :
present in “$ref” key
I see. PR would be welcome to fix this. Maybe stringify all reference keys?
I would like to try ) where such fix should go? reitit-swagger or malli itself?
In malli, either malli.json-schema
or malli.swagger
ns.
cool, will be back with PR )
congrats on the release!