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

Validating params or attributes #93

Open
lifelofranco opened this issue Sep 26, 2022 · 4 comments
Open

Validating params or attributes #93

lifelofranco opened this issue Sep 26, 2022 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@lifelofranco
Copy link

lifelofranco commented Sep 26, 2022

First of all, thanks for the great work! 💯

In interactor with the interactor-contracts gem, it's possible to validate a hash like this:

expects do
  required(:user).filled(type?: User)
  required(:params).schema do
    required(:email).filled(:str?)
    required(:first_name).filled(:str?)
    optional(:middle_name)
  end
end

Is there a similar way? Or a recommended way of achieving the same thing?

@sunny
Copy link
Owner

sunny commented Sep 27, 2022

Hi @lifelofranco!

Good call. There’s no such way to validate keys inside hashes with the current validation system.

One way you could do this out of the box would be using must. E.g.:

class CreateUser
  input :user, type: User
  input :params,
        must: {
          have_email: -> h { h[:email].present? },
          have_first_name: -> h { h[:first_name].present? },
        }

In the future perhaps we could build another validator for hashes that could perhaps look like this:

class CreateUser
  input :user, type: User
  input :params,
        hash: -> do
          input :email 
          input :first_name 
          input :middle_name, optional: true
        end

Edit: Fixed syntax for -> h

@sunny sunny added the enhancement New feature or request label Sep 27, 2022
@sunny
Copy link
Owner

sunny commented Dec 29, 2022

Another option would be to add support for contracts like the interactor-contract gem does.

Is there any of these solutions you would like to try and implement @lifelofranco?

@sunny sunny added help wanted Extra attention is needed good first issue Good for newcomers labels Feb 19, 2023
@pboling
Copy link
Contributor

pboling commented Jul 12, 2023

I think there is a typo in the current example (h -> vs -> h).

class CreateUser
  input :user, type: User
  input :params,
        must: {
          have_email: -> (h) { h[:email].present? },
          have_first_name: -> (h) { h[:first_name].present? },
        }

@sunny
Copy link
Owner

sunny commented Jul 13, 2023

@pboling Good call! I’ve fixed the comment 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants