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

@ -142,7 +142,7 @@ class Epp::DomainsController < EppController
end end
def validate_update def validate_update
if params[:parsed_frame].css('chg registrant').present? && params[:parsed_frame].css('legalDocument').blank? if element_count('update > chg > registrant') > 0
requires('extension > extdata > legalDocument') requires('extension > extdata > legalDocument')
end end

View file

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

View file

@ -500,4 +500,4 @@ en:
could_not_determine_object_type_check_xml_format_and_namespaces: 'Could not determine object type. Check XML format and namespaces.' could_not_determine_object_type_check_xml_format_and_namespaces: 'Could not determine object type. Check XML format and namespaces.'
unknown_expiry_relative_pattern: 'Expiry relative must be compatible to ISO 8601' unknown_expiry_relative_pattern: 'Expiry relative must be compatible to ISO 8601'
unknown_expiry_absolute_pattern: 'Expiry absolute must be compatible to ISO 8601' unknown_expiry_absolute_pattern: 'Expiry absolute must be compatible to ISO 8601'
are_mutally_exclusive: '%{params} are mutually exclusive' mutally_exclusive_params: 'Mutually exclusive parameters: %{params}'

View file

@ -629,7 +629,7 @@ describe 'EPP Domain', epp: true do
}) })
response = epp_plain_request(xml, :xml) response = epp_plain_request(xml, :xml)
response[:msg].should == 'keyData, dsData are mutually exclusive' response[:msg].should == 'Mutually exclusive parameters: keyData, dsData'
response[:result_code].should == '2306' response[:result_code].should == '2306'
end end
end end