Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mx/defn and malli.destructure/parse unexpected handling of seqexps #1005

Open
larkery opened this issue Feb 1, 2024 · 4 comments
Open

mx/defn and malli.destructure/parse unexpected handling of seqexps #1005

larkery opened this issue Feb 1, 2024 · 4 comments
Labels
documentation Documentation improvemen help wanted Help most welcome

Comments

@larkery
Copy link

larkery commented Feb 1, 2024

If I write

(require '[malli.experimental :as mx])
(require '[malli.core :as m])

(mx/defn ^:malli/always f [xs :- [:* :int]]
  (malli.core/validate [:* :int] xs))

(f 1)
;; => false

(f [1])
;; => Execution error (ExceptionInfo) at malli.core/-exception (core.cljc:136).
;;    :malli.core/invalid-input

(m/validate [:* :int] [1])
;; => true

I would expect (f 1) to throw an except and (f [1]) to return true.

I think this is because the schemas for the arguments are combined using [:cat ...] and [:cat [:* :int]] 'flattens' the expression, so [[1]] is not valid for [:cat [:* :int]]. In this simple case the args schema should be something like [:tuple [:* :int]] for which [[1]] is valid.

Of course this is not so simple for varargs.

@ikitommi
Copy link
Member

ikitommi commented Feb 3, 2024

Yes, this is because of the auto-flattening of sequence schemas so it's a feature. You can escape it by wrapping it with non-sequence schema like :schema:

(mx/defn ^:malli/always f [xs :- [:schema [:* :int]]]
  (malli.core/validate [:* :int] xs))

or just use non-sequential like :vector:

(mx/defn ^:malli/always f [xs :- [:vector :int]]
  (malli.core/validate [:* :int] xs))

I don't think we should change this, maybe just document this into https://github.com/metosin/malli/blob/master/docs/function-schemas.md. PR most welcome on this.

@ikitommi
Copy link
Member

ikitommi commented Feb 4, 2024

would you have time to document this? e.g. PR most welcome

@ikitommi ikitommi added help wanted Help most welcome documentation Documentation improvemen labels Feb 4, 2024
@larkery
Copy link
Author

larkery commented Feb 4, 2024 via email

@ikitommi
Copy link
Member

No auto-wrapping now, flattening is needed to describe Varargs. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Documentation improvemen help wanted Help most welcome
Projects
None yet
Development

No branches or pull requests

2 participants