Skip to content

Commit

Permalink
Add rules for beneficiary address
Browse files Browse the repository at this point in the history
  • Loading branch information
dwilkie committed Jan 9, 2025
1 parent 117781d commit 04977fc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/request_schemas/schema_rules/beneficiary_address_rules.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class BeneficiaryAddressRules
def validate(attributes)
address = attributes[:address]
end
end
21 changes: 21 additions & 0 deletions app/request_schemas/v1/beneficiary_address_request_schema.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module V1
class BeneficiaryAddressRequestSchema < JSONAPIRequestSchema
option :beneficiary_address_rules, default -> { BeneficiaryAddressRules.new }

params do
required(:data).value(:hash).schema do
required(:type).filled(:str?, eql?: "address")
Expand All @@ -16,4 +18,23 @@ class BeneficiaryAddressRequestSchema < JSONAPIRequestSchema
end
end
end

attribute_rule do |attributes|
(4..3).each do |level|
division_attributes = [:code, :name].map { |type| :"administrative_division_level_#{level}_#{type}" }

next if division_attributes.all? { |division_attribute| attributes[division_attribute].blank? }

(3..2).each do |parent_level|
next if level == parent_level

parent_division_attributes = [:code, :name].map { |type| :"administrative_division_level_#{parent_level}_#{type}" }

next if parent_division_attributes.any? { |parent_division_attribute| attributes[parent_division_attribute].present? }

key([:data, :attributes, parent_division_attributes.first]).failure("must be present")
break
end
end
end
end
2 changes: 1 addition & 1 deletion spec/request_schemas/v1/beneficiary_request_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ module V1
).to have_valid_field(:data, :attributes, :metadata)
end

it "handles postprocessing" do
it "handles post processing" do
result = validate_schema(
input_params: {
data: {
Expand Down

0 comments on commit 04977fc

Please sign in to comment.