martinthenth / goal

A parameter validation library - based on Ecto
https://hexdocs.pm/goal
MIT License
71 stars 10 forks source link

Feature: `one_of_many` Validation #89

Open Joraeuw opened 6 months ago

Joraeuw commented 6 months ago

I really like this library and I would like to improve it.

I have the idea to add a one_of_many validation. As the name suggests it will check if only one of N fields is present.

Here's a simple example:

 defparams :login do
        optional(:email, :string)
        optional(:username, :string)
        required(:password, :string)

        one_of_many([:email, :username])
  end
martinthenth commented 6 months ago

Thanks for your suggestion. I hadn't planned on introducing this feature 🤔 I'll think on the idea for a bit. The same can be achieved with some business logic, and there would be a question whether it is useful to provide configurability for example for two_of_many, three_of_many etc

Joraeuw commented 6 months ago

I think it can be done. In my opinion only one_of_many and some_of_many make sense, where some_of_many would take an integer of how many of the provided fields are expected. Also how about adding support for structs. For example if your API expects images to be provided from the frontend. Its usually coming as the Plug.Upload struct.

tomkr4l commented 3 months ago

+1 for this feature. I suggest to look to grape gem for inspiration which provides options exactly_one_of, at_least_one_of, all_or_none_of, mutually_exclusive. With combination of these It's possible to have almost complete control over required params and their dependencies.