-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Zipcode] Validation à la création d'un User ou d'un Partner (#597)
* Validates address zipcode on create context only for User and Partner * UI for admin campaigns * campaigns admin fill rates * Validates address zipcode on create context only for User and Partner * Fix rebase conflict Co-authored-by: Mathieu Ripert <[email protected]>
- Loading branch information
1 parent
44463c3
commit 8b7581e
Showing
13 changed files
with
159 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.is-invalid.ap-input#user_address ~ .ap-input-icon { | ||
right: 34px !important; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class PostalAddressValidator < ActiveModel::EachValidator | ||
REGEX_ZIPCODE = %r{ | ||
\d{2}[ ]?\d{3} # France | ||
| | ||
973\d{2} # Guyane | ||
| | ||
9[78][01]\d{2} # Guadeloupe | ||
| | ||
9[78]4\d{2} # La Réunion | ||
| | ||
9[78]2\d{2} # Martinique | ||
| | ||
976\d{2} # Mayotte | ||
}x | ||
|
||
def initialize(options) | ||
@with_zipcode = !options[:with_zipcode].nil? ? options[:with_zipcode] : true | ||
super | ||
end | ||
|
||
def validate_each(record, attribute, value) | ||
unless @with_zipcode.nil? || value.match?(REGEX_ZIPCODE) | ||
record.errors.add(attribute, (options[:message] || I18n.t("errors.messages.missing_zipcode"))) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe PostalAddressValidator do | ||
subject do | ||
Class.new do | ||
include ActiveModel::Validations | ||
attr_accessor :address | ||
validates :address, postal_address: {with_zipcode: true, message: "invalid_message"} | ||
end.new | ||
end | ||
|
||
context "when the postal address is valid" do | ||
let(:address) { generate(:french_address) } | ||
|
||
it "allows the input" do | ||
subject.address = address | ||
expect(subject).to be_valid | ||
end | ||
end | ||
|
||
context "when the postal address is invalid" do | ||
let(:invalid_message) { "invalid_message" } | ||
|
||
it "invalidates the input" do | ||
subject.address = "21 Rue Bergère" | ||
expect(subject).not_to be_valid | ||
end | ||
|
||
it "alerts the consumer" do | ||
subject.address = "address" | ||
subject.valid? | ||
expect(subject.errors[:address]).to include(invalid_message) | ||
end | ||
end | ||
end |