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

Add interpolation key with custom validate #311

Closed
nicolas-besnard opened this issue Feb 5, 2017 · 2 comments
Closed

Add interpolation key with custom validate #311

nicolas-besnard opened this issue Feb 5, 2017 · 2 comments

Comments

@nicolas-besnard
Copy link

nicolas-besnard commented Feb 5, 2017

I'm using dry-validation for my models with ActiveRecord.

I'd like to know if there's a way to add a custom interpolation key to my validation.

For example:

schema = Dry::Validation.Schema do
  required(:time_table).value(type?: ModelClass)

  validate(previous_term_paid?: [:time_table]) do |time_table|
    if time_table.term > 1 && !time_table.previous.paid?
      false
    else
      true
    end
  end
end

schema.call(time_table: Model.find(...))

Is there a way to add time_table.previous.term to my error message ?

@siassaj
Copy link

siassaj commented Sep 3, 2017

+1
simplest form:

validate(previous_term_paid?: [:time_table]) do |time_table, context|
    if time_table.term > 1 && !time_table.previous.paid?
      context.keys[:term] = time_table.previous.term
      false
    else
      true
    end
  end

@flash-gordon
Copy link
Member

This will be added during #440

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

3 participants