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

Using dry-validation to check class attribute and / or external actor (API) #309

Closed
nicolas-besnard opened this issue Jan 26, 2017 · 3 comments

Comments

@nicolas-besnard
Copy link

nicolas-besnard commented Jan 26, 2017

I'd like to use dry-validation to ensure a particular class attributes is greater than an other.

For exemple:

Dry::Validation.Schema do
  required(:contribution).filled?
  required(:payment_amount).filled(:int?)

  rule(payment_amount_is_greater_than_contribution_amount: [:contribution, :payment_amount]) do |contribution, payment_amount|
    payment_amount.gt?(value(:contribution).amount)
  end
end

But I got the error ArgumentError: +amount+ is not a valid predicate name

My second use case will be an external API call as a validator:

Dry::Validation.Schema do
  required(:user).filled?

  rule(user_has_remote_account: [:user]) do |user|
    value(:user).remote_account?
  end
end

I think I can use a custom predicate for that, but I'm not sure it's the right way to do it.

Any chance someone can help me ? :)

@solnic
Copy link
Member

solnic commented Jan 26, 2017

rule uses internal DSL so you can't access normal values there. You can use validate instead:

  validate(user_has_remote_account: [:user]) do |user|
    user.remote_account?
  end

@nicolas-besnard
Copy link
Author

Thanks for your quick answer ! Unfortunately, this method is not documented on your website :(

@solnic
Copy link
Member

solnic commented Jan 26, 2017 via email

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