1 // Copyright (C) 2013 Google Inc.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #ifndef I18N_ADDRESSINPUT_ADDRESS_VALIDATOR_H_
16 #define I18N_ADDRESSINPUT_ADDRESS_VALIDATOR_H_
18 #include <libaddressinput/address_field.h>
19 #include <libaddressinput/address_problem.h>
20 #include <libaddressinput/util/scoped_ptr.h>
27 namespace addressinput {
30 class LoadRulesDelegate;
34 typedef std::vector<AddressProblem> AddressProblems;
35 typedef std::multimap<AddressField, AddressProblem::Type> AddressProblemFilter;
37 // Validates an AddressData structure. Sample usage:
38 // class MyClass : public LoadRulesDelegate {
40 // MyClass() : validator_(AddressValidator::Build(
41 // scoped_ptr<Downloader>(new MyDownloader),
42 // scoped_ptr<Storage>(new MyStorage),
44 // validator_->LoadRules("US");
47 // virtual ~MyClass() {}
49 // virtual void OnAddressValidationRulesLoaded(
50 // const std::string& country_code,
55 // void ValidateAddress() {
56 // AddressData address;
57 // address.country_code = "US";
58 // address.administrative_area = "CA";
59 // AddressProblems problems;
60 // AddressProblemFilter filter;
61 // AddressValidator::Status status =
62 // validator_->ValidateAddress(address, filter, &problems);
63 // if (status == AddressValidator::SUCCESS) {
69 // scoped_ptr<AddressValidator> validator_;
71 class AddressValidator {
73 // The status of address validation.
75 // Address validation completed successfully. Check |problems| to see if any
76 // problems were found.
79 // The validation rules are not available, because LoadRules() was not
80 // called or failed. Reload the rules.
83 // The validation rules are being loaded. Try again later.
87 virtual ~AddressValidator();
89 // Builds an address validator. Takes ownership of |downloader| and |storage|,
90 // which cannot be NULL. Does not take ownership of |load_rules_delegate|,
91 // which can be NULL. The caller owns the result.
92 static scoped_ptr<AddressValidator> Build(
93 scoped_ptr<Downloader> downloader,
94 scoped_ptr<Storage> storage,
95 LoadRulesDelegate* load_rules_delegate);
97 // Loads the generic validation rules for |country_code| and specific rules
98 // for the country's administrative areas, localities, and dependent
99 // localities. A typical data size is 10KB. The largest is 250KB. If a country
100 // has language-specific validation rules, then these are also loaded.
103 // https://i18napis.appspot.com/ssl-aggregate-address/data/US
105 // If the rules were loaded successfully before or are still being loaded,
106 // then does nothing. Notifies |load_rules_delegate| when the loading
108 virtual void LoadRules(const std::string& country_code) = 0;
110 // Validates the |address| and populates |problems| with the validation
111 // problems, filtered according to the |filter| parameter.
113 // If the |filter| is empty, then all discovered validation problems are
114 // returned. If the |filter| contains problem elements, then only the problems
115 // in the |filter| may be returned.
117 // If the |problems| parameter is NULL, then checks whether the validation
118 // rules are available, but does not validate the |address|.
119 virtual Status ValidateAddress(const AddressData& address,
120 const AddressProblemFilter& filter,
121 AddressProblems* problems) const = 0;
123 // Fills in |suggestions| for the partially typed in |user_input|, assuming
124 // the user is typing in the |focused_field|. If the number of |suggestions|
125 // is over the |suggestion_limit|, then returns no |suggestions| at all.
127 // If the |solutions| parameter is NULL, the checks whether the validation
128 // rules are available, but does not fill in suggestions.
130 // Sample user input 1:
131 // country code = "US"
132 // postal code = "90066"
133 // focused field = POSTAL_CODE
134 // suggestions limit = 1
136 // [{administrative_area: "CA"}]
138 // Sample user input 2:
139 // country code = "CN"
140 // dependent locality = "Zongyang"
141 // focused field = DEPENDENT_LOCALITY
142 // suggestions limit = 10
144 // [{dependent_locality: "Zongyang Xian",
145 // locality: "Anqing Shi",
146 // administrative_area: "Anhui Sheng"}]
147 virtual Status GetSuggestions(
148 const AddressData& user_input,
149 AddressField focused_field,
150 size_t suggestion_limit,
151 std::vector<AddressData>* suggestions) const = 0;
153 // Canonicalizes the administrative area in |address_data|. For example,
154 // "texas" changes to "TX". Returns true on success, otherwise leaves
155 // |address_data| alone and returns false.
156 virtual bool CanonicalizeAdministrativeArea(AddressData* address_data)
160 } // namespace addressinput
163 #endif // I18N_ADDRESSINPUT_ADDRESS_VALIDATOR_H_