Allow regular expressions in postal codes

Programmers can take advantage of this feature when defining custom
default settings. And, since many CONSUL installations had custom
changes in the `custom/verification/residence.rb` model and those
changes might use regular expressions, we're making it easier to migrate
that code to the new system to define valid postal codes.

We aren't documenting this feature in the description in the admin
section because most administrators don't know what regular expressions
are.

Note that, in order to simplify the setting, we already define the `/\A`
and `\Z/` characters. So, if the custom code had something like
`postal_code =~ /^280/`, the setting would have to be "280*" (without
the quotes) or, in order to comply with a length validation,
"280[0-9]{2}" (without the quotes).
This commit is contained in:
Javi Martín
2021-11-27 18:48:42 +01:00
parent a79bbac894
commit d6b85a038c
2 changed files with 53 additions and 1 deletions

View File

@@ -110,7 +110,7 @@ class Verification::Residence
if code_or_range.include?(":") if code_or_range.include?(":")
Range.new(*code_or_range.split(":").map(&:strip)).include?(postal_code&.strip) Range.new(*code_or_range.split(":").map(&:strip)).include?(postal_code&.strip)
else else
postal_code&.strip == code_or_range.strip /\A#{code_or_range.strip}\Z/.match?(postal_code&.strip)
end end
end end
end end

View File

@@ -126,6 +126,58 @@ describe Verification::Residence do
expect(residence).to be_valid expect(residence).to be_valid
end end
it "allows regular expressions" do
Setting["postal_codes"] = "007,[A-Za-z]{2}-[0-9]{3},86"
residence.postal_code = "007"
expect(residence).to be_valid
residence.postal_code = "86"
expect(residence).to be_valid
residence.postal_code = "AB-123"
expect(residence).to be_valid
residence.postal_code = "zz-789"
expect(residence).to be_valid
residence.postal_code = "006"
expect(residence).not_to be_valid
residence.postal_code = "87"
expect(residence).not_to be_valid
residence.postal_code = "AB-12"
expect(residence).not_to be_valid
residence.postal_code = "AB-1234"
expect(residence).not_to be_valid
residence.postal_code = "A-123"
expect(residence).not_to be_valid
residence.postal_code = "ABC-123"
expect(residence).not_to be_valid
residence.postal_code = "ABC-12"
expect(residence).not_to be_valid
residence.postal_code = "AB-A12"
expect(residence).not_to be_valid
residence.postal_code = "12A-12"
expect(residence).not_to be_valid
residence.postal_code = "123-12"
expect(residence).not_to be_valid
residence.postal_code = "ABC-A1"
expect(residence).not_to be_valid
residence.postal_code = "AB-123\n123"
expect(residence).not_to be_valid
end
it "is not valid with postal codes not included in settings" do it "is not valid with postal codes not included in settings" do
residence.postal_code = "12345" residence.postal_code = "12345"
expect(residence).not_to be_valid expect(residence).not_to be_valid