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

validation blocks with inherit: true option ignore other options #533

Open
YaroslavZahoruiko opened this issue Nov 1, 2021 · 3 comments

Comments

@YaroslavZahoruiko
Copy link

YaroslavZahoruiko commented Nov 1, 2021

I think that when we use inherit: true, we should merge new options to parent validation bock as it work in property inherit: true and move the validation position to the end of the groups.
https://github.com/trailblazer/reform/blob/master/lib/reform/validation/groups.rb#L17

@apotonick
Copy link
Member

Could you make a quick example of the validation blocks and "positions" for such an :inherit case, @YaroslavZahoruiko ? 🍻

@YaroslavZahoruiko
Copy link
Author

YaroslavZahoruiko commented Nov 9, 2021

@apotonick  
in module or in inherited class

property :name
validation :valid_name do
  required(:name).filled(:str?)
end

in the main class

ValidNamePresent = proc { valid_name.present? }
validation :valid_name, inherit: true, if: ValidNamePresent

or we want add dependency from other validation

validation :default do
end

validation :valid_name, inherit: true, if: :default

this code will not work
as a quick fix i suggest fixing in https://github.com/trailblazer/reform/blob/master/lib/reform/validation/groups.rb#L17

  def add(name, options)
    if options[:inherit] && (cfg = find { |cfg| cfg.first == name })
      self.delete(cfg)
      group = cfg[1]
      options.reverse_merge(cfg[2])
    end

    i = index_for(options)

    self.insert(i, [name, group ||= @group_class.new, options]) # Group.new
    group
  end

@YaroslavZahoruiko
Copy link
Author

YaroslavZahoruiko commented Nov 9, 2021

but we need to think about another validation dependency
module

module AddressForm
property :address do
    property :first_name
    property :last_name
    property :phone
end

validation :address_validation do
    #some validation
end

validation :phone_validation if: :address_validation do
    #some validation
end

condition for phone_validation will always be true because the position phone_validation will be before address_validation when we will overwrite it

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