Skip to content

Commit

Permalink
Validate swiss zip_code in wizards(#1452)
Browse files Browse the repository at this point in the history
  • Loading branch information
amaierhofer committed Dec 31, 2024
1 parent 337f9c6 commit e97a385
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/models/wizards/steps/signup/person_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Wizards::Steps::Signup::PersonFields < Wizards::Step
validates :gender, :street, :housenumber, :town, :zip_code,
:country, presence: true

validate :assert_is_valid_swiss_post_code

def initialize(...)
super

Expand All @@ -48,4 +50,12 @@ def initialize(...)
self.country ||= Settings.addresses.imported_countries.to_a.first
end
end

private

def assert_is_valid_swiss_post_code
if zip_code.present? && Countries.swiss?(country) && !zip_code.to_s.match(/\A\d{4}\z/)
errors.add(:zip_code)
end
end
end
13 changes: 13 additions & 0 deletions spec/models/wizards/steps/signup/person_fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@
expect(form.errors.full_messages).to eq ["Person muss 6 Jahre oder älter sein"]
end
end

describe "zip_code" do
it "validates swiss zip code format" do
form.attributes = required_attrs.merge(zip_code: "123")
expect(form).not_to be_valid
expect(form.errors.full_messages).to eq ["PLZ ist nicht gültig"]
end

it "accepts any format for non-swiss countries" do
form.attributes = required_attrs.merge(zip_code: "123", country: "DE")
expect(form).to be_valid
end
end
end

it "sets country default to ch" do
Expand Down

0 comments on commit e97a385

Please sign in to comment.