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

[Ftr] Add ability to provide custom validations and coercions #97

Open
taxonomic-blackfish opened this issue Feb 6, 2020 · 1 comment

Comments

@taxonomic-blackfish
Copy link

Is your feature request related to a problem? Please describe.
I want to be able to provide a custom type where a coercion is applied. In my case, I have a set that is too large to be worth printing to the screen.

Describe the solution you'd like
I would like an API where I can define functions that allows me to validate data, coerce data, and report errors to the user about what went wrong. If this exists, I did not see it in the docs.

For example:

{:type {;; validate returns `nil` on success and `str` on failure
        :validate (fn [x] (if (= (% x 2) 0) "No even values"))
        ;; coerce must always succeed (no error/exception)
        :coerce some-func-here}}

Or combine them into something (perhaps?) simpler.

{:type (fn [x] (if (= (% x 2) 0)
                 (throw (Exception. "No even values"))
                 (some-func-here x))}

Describe alternatives you've considered
I can do all this boilerplate in my code, but if it's a global option, I have to do it with all subcommands. I would rather define these functions in one place and know that the option that's being passed to my function has the correct type/value.

I also attempted using :type (fn [x] x) to test if it accepts functions, and it does not (though this is clear after inspecting the code).

@bartkl
Copy link

bartkl commented Sep 24, 2022

I would love this functionality too.

I actually missed this issue when creating my own one, which is probably a duplicate, but I'll leave that up to the maintainer(s): #157

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

No branches or pull requests

2 participants