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

Implement malli.util/select, similar to spec-alpha2/select #724

Open
hgranthorner opened this issue Jul 15, 2022 · 6 comments
Open

Implement malli.util/select, similar to spec-alpha2/select #724

hgranthorner opened this issue Jul 15, 2022 · 6 comments
Labels
enhancement New feature or request

Comments

@hgranthorner
Copy link
Contributor

I think it would be pretty great to have a malli equivalent to the spec-alpha2/select. For those that don't know, select would take a schema and a keyset, and mark all keys not in the keyset as optional. For example:

(malli.util/select [:map [:a int?] [:b string?]] [:a])
=> [:map [:a int?] [:b {:optional true} string?]]

select should also allow for selecting subkeys. For example, here we mark the root level key as optional, but if it exists, :c is required:

(malli.util/select [:map [:a int?] [:b [:map [:c string?]]]] [:a {:b [:c]}])
=> [:map [:a int?] [:b {:optional true} [:map [:c string?]]]]

I think this would be great, and serve much the same need as the spec-alpha2/select function. I've taken a couple cracks at implementing this myself, but haven't quite figured it out.

@hgranthorner
Copy link
Contributor Author

hgranthorner commented Jul 15, 2022

I may have a working example here, but is almost definitely missing some edge cases and can definitely be implemented much more easily/performantly... I'm just not sure how.

@vharmain vharmain added the enhancement New feature or request label Jul 30, 2022
@bortexz
Copy link

bortexz commented Aug 10, 2022

Hacked around another implementation of select that seems to work, in case anyone's interested: https://gist.github.com/bortexz/a9e253ab7b7ba3815409bb869f91c846

Edit: We could have select-eql, require-eql and optional-eql, equivalents to *-keys that operate on EQL instead of keys vector

@NoahTheDuke
Copy link
Contributor

Oh good call with the gist, here's my implementation, which makes all non-selected keys optional.

@Yutsa
Copy link

Yutsa commented Apr 12, 2023

Any updates on this ? Watched the "Maybe not" talk by Rich Hickey and having composability of specs looks really good

@crimsonhawk47
Copy link

I'm also interested in this

@eval
Copy link
Contributor

eval commented Sep 19, 2023

I released https://github.com/eval/malli-select. It allows for selecting across map-of, vector etc. (e.g. [{:addresses [:street]}]), it will assert correct paths, and optional attributes can be pruned (to aid data generation). Feeback welcome!

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

7 participants