From 04977fcaee2422ffea8e5d05b001f72be422726e Mon Sep 17 00:00:00 2001 From: David Wilkie Date: Thu, 9 Jan 2025 13:18:23 +0700 Subject: [PATCH] Add rules for beneficiary address --- .../schema_rules/beneficiary_address_rules.rb | 5 +++++ .../v1/beneficiary_address_request_schema.rb | 21 +++++++++++++++++++ .../v1/beneficiary_request_schema_spec.rb | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 app/request_schemas/schema_rules/beneficiary_address_rules.rb diff --git a/app/request_schemas/schema_rules/beneficiary_address_rules.rb b/app/request_schemas/schema_rules/beneficiary_address_rules.rb new file mode 100644 index 000000000..c5515466c --- /dev/null +++ b/app/request_schemas/schema_rules/beneficiary_address_rules.rb @@ -0,0 +1,5 @@ +class BeneficiaryAddressRules + def validate(attributes) + address = attributes[:address] + end +end diff --git a/app/request_schemas/v1/beneficiary_address_request_schema.rb b/app/request_schemas/v1/beneficiary_address_request_schema.rb index 140b0163c..b379c1203 100644 --- a/app/request_schemas/v1/beneficiary_address_request_schema.rb +++ b/app/request_schemas/v1/beneficiary_address_request_schema.rb @@ -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") @@ -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 diff --git a/spec/request_schemas/v1/beneficiary_request_schema_spec.rb b/spec/request_schemas/v1/beneficiary_request_schema_spec.rb index 9efe14547..79365d8e6 100644 --- a/spec/request_schemas/v1/beneficiary_request_schema_spec.rb +++ b/spec/request_schemas/v1/beneficiary_request_schema_spec.rb @@ -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: {