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

New notation for array inputs #39

Open
williampollet opened this issue Jun 30, 2021 · 15 comments
Open

New notation for array inputs #39

williampollet opened this issue Jun 30, 2021 · 15 comments
Labels
enhancement New feature or request

Comments

@williampollet
Copy link
Contributor

williampollet commented Jun 30, 2021

Hey @sunny !!

Thanks for the amazing work :D

I have an idea, regarding inputs and outputs.

What do you think about accepting a new notation when expecting an array of similar objects, such as:

    inputs :orders, type: [Orders]

(Similar to the GraphQL notation)

This way it will be easier to type arrays.

@sunny
Copy link
Owner

sunny commented Jun 30, 2021

That makes a lot of sense! I’d be happy to merge a PR for that 🙏🏻

@sunny
Copy link
Owner

sunny commented Jul 31, 2021

Actually, since now the array syntax for types is used as an "or":

input :age, type: [Integer, Float]

I think it would make more sense to just keep the plural and not have an array:

input :order, type: Order
inputs :orders, type: Order

But perhaps better than the plural which can be a bit too discreet, having it as a new array: true option could make it much more explicit:

input :order, type: Order
input :orders, type: Order, array: true

@sunny
Copy link
Owner

sunny commented Dec 15, 2021

@williampollet Would you be willing to try a patch for this feature using the array: true syntax? 🙏🏻

@williampollet
Copy link
Contributor Author

williampollet commented Jan 11, 2022

I'm taking it !

@sunny sunny added the enhancement New feature or request label Feb 10, 2022
@sunny sunny changed the title [Enhancement] New notation for arrays New notation for array inputs Feb 10, 2022
@viralpraxis
Copy link
Contributor

One import disadvantage of array: true it that it won't support type: [T, [T]] input constraints usecase.

In gerenal [T] seems to be more generic ADT-like approach

@sunny
Copy link
Owner

sunny commented Feb 29, 2024

Good point. I think I’d still like the constraints here to be as light as possible and not try to handle all possible types.

One way we could accept more types would be to lean on another constraints/validation gem.

@viralpraxis
Copy link
Contributor

One way we could accept more types would be to lean on another constraints/validation gem.

Do you have examples of such gems in mind?

@sunny
Copy link
Owner

sunny commented Mar 4, 2024

I was thinking of dry-validations, or ActiveRecord validations (perhaps with type_validator). But there may be others.

@viralpraxis
Copy link
Contributor

I think we could come up with some sort of global configuration option that will switch from our (default) type validation backend to gem-provided backend

dry-rb is more popular, should I try to implement a PoC for this? @sunny

@sunny
Copy link
Owner

sunny commented Mar 4, 2024

I think it would make sense to use dry-validations but I wouldn’t want the gem as a dependency for people who are fine with the simpler current syntax.

I don’t know what the best course of action for that would be. Maybe this means that the glue code between actor and dry-validations lives in a different gem?

@viralpraxis
Copy link
Contributor

Yeah, I think we only need to expose some interface so that other gems might use it to provide 3rd-party-gem-backed validation. Also some sort of a global configuration option to select implementation

@sunny
Copy link
Owner

sunny commented Mar 5, 2024

Yeah, makes sense. We could have for example:

  • config.validations = :default,
  • config.validations = :rails if you include the service_actor-rails gem,
  • config.validations = :dry_validation with a new service_actor-dry-validation gem.

To allow that, perhaps the current validations could use more generic before, after & around hooks to avoid having to monkey-patch.

@viralpraxis
Copy link
Contributor

To allow that, perhaps the current validations could use more generic before, after & around hooks to avoid having to monkey-patch.

Could you elaborate on that? Seems like I'm missing something here

@viralpraxis
Copy link
Contributor

Apart from dry-validations, can we add array: true syntax right now? I'm interested in this feature

@sunny
Copy link
Owner

sunny commented Mar 11, 2024

To allow that, perhaps the current validations could use more generic before, after & around hooks to avoid having to monkey-patch.

I was thinking of a way of “hooking” into actor from outside gems. Using for example before_call :validate_type that could be an interface that can allow outside gems the to add some validation behavior (or any kind of other behavior) and perhaps remove some other behavior (skip_before_call :validate_type). (See also #111).

Not sure this would be the best way to do that, just some thoughts.

Apart from dry-validations, can we add array: true syntax right now? I'm interested in this feature

Sure!

Note that you can do a similar check today with:

input :names,
      type: Array,
      must: {
        be_strings: -> array { array.all?(String) }
      }

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