Make request validation shorter on domain create

This commit is contained in:
Martin Lensment 2015-01-27 13:42:02 +02:00
parent cad6d68bfa
commit ebfa997124
4 changed files with 22 additions and 27 deletions

View file

@ -75,32 +75,9 @@ class EppController < ApplicationController
epp_errors.empty?
end
def mutually_exclusive(*selectors)
present_count = 0
selectors.each do |selector|
full_selector = [@prefix, selector].join(' ')
el = params[:parsed_frame].css(full_selector).first
present_count += 1 if el && el.text.present?
end
return if present_count <= 1
epp_errors << {
code: '2306',
msg: I18n.t(:are_mutally_exclusive, params: selectors.join(', '))
}
end
# let's follow grape's validations: https://github.com/intridea/grape/#parameter-validation-and-coercion
def exactly_one_of(*selectors)
present_count = 0
selectors.each do |selector|
full_selector = [@prefix, selector].join(' ')
el = params[:parsed_frame].css(full_selector).first
present_count += 1 if el && el.text.present?
end
return if present_count == 1
return if element_count(*selectors) == 1
epp_errors << {
code: '2306',
@ -108,6 +85,14 @@ class EppController < ApplicationController
}
end
def mutually_exclusive(*selectors)
return if element_count(*selectors) <= 1
epp_errors << {
code: '2306',
msg: I18n.t(:mutally_exclusive_params, params: selectors.join(', '))
}
end
def optional(selector, *validations)
full_selector = [@prefix, selector].join(' ')
el = params[:parsed_frame].css(full_selector).first
@ -121,6 +106,16 @@ class EppController < ApplicationController
end
end
def element_count(*selectors)
present_count = 0
selectors.each do |selector|
full_selector = [@prefix, selector].join(' ')
el = params[:parsed_frame].css(full_selector).first
present_count += 1 if el && el.text.present?
end
present_count
end
def xml_attrs_present?(ph, attributes) # TODO: THIS IS DEPRECATED AND WILL BE REMOVED IN FUTURE
attributes.each do |x|
epp_errors << {