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

:tuple requires vector #734

Closed
NoahTheDuke opened this issue Aug 8, 2022 · 4 comments
Closed

:tuple requires vector #734

NoahTheDuke opened this issue Aug 8, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@NoahTheDuke
Copy link
Contributor

Hey there! I just ran into an issue where using mt/strip-extra-keys-transformer wasn't working because I had [:tuple OtherSchema] and was providing a seq: (OtherSchema). I'm not using any Malli validation yet (planned but currently relying on json schema validation) which may have caught this, but it seems worth considering changing. I'd prefer to not have to call (m/encode Example (vec obj) mt/strip-extra-keys-transformer) just to be sure that I'm providing a vector, as sometimes I'm transforming straight from a database call and the output is a seq instead of a vector.

Thanks so much!

@vharmain
Copy link
Member

vharmain commented Aug 8, 2022

Hi!

I guess tuples should be vectors by design. README says:

A :tuple describes a fixed length Clojure vector of heterogeneous elements

I'm not sure what is the reason for this (perf? implementation complexity?) but maybe @ikitommi can chime in. :)

However, one option is to use sequence schema instead.

@vharmain vharmain added the enhancement New feature or request label Aug 8, 2022
@ikitommi
Copy link
Member

ikitommi commented Aug 8, 2022

perf, of course :) you can easily add a custom transformer in user space that does the transforming for tuples, relevant helpers exists already, see: https://github.com/metosin/malli/blob/master/src/malli/transform.cljc#L416-L422.

PR to add :tuple to mt/-collection-transformer most welcome. You can easily compose that with the strip-keys-thing.

@NoahTheDuke
Copy link
Contributor Author

However, one option is to use sequence schema instead.

Shortly after I posted, I learned that the collection could be empty, so a sequence schema is better for my use case lol. Thanks for mentioning it anyway. Still glad I posted!

PR to add :tuple to mt/-collection-transformer most welcome.

Sure thing! Thanks for pointing that out. I'll write it up tonight.

@ikitommi
Copy link
Member

PR merged, thanks! For ease of use, I think it would be good to have a new built-in transformer that would have this as built-in (e.g. string + collections). But, it's another issue. Closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants