1 // Copyright (C) 2014 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 // The public interface to the address validation features of libaddressinput.
16 // The AddressValidator will examine an AddressData struct and return a map of
17 // the problems found with the different fields of this struct.
19 #ifndef I18N_ADDRESSINPUT_ADDRESS_VALIDATOR_H_
20 #define I18N_ADDRESSINPUT_ADDRESS_VALIDATOR_H_
22 #include <libaddressinput/address_field.h>
23 #include <libaddressinput/address_problem.h>
24 #include <libaddressinput/callback.h>
25 #include <libaddressinput/util/basictypes.h>
30 namespace addressinput {
35 typedef std::multimap<AddressField, AddressProblem> FieldProblemMap;
37 // Validates an AddressData struct. Sample usage:
41 // : supplier_(new MySupplier),
42 // validator_(new AddressValidator(supplier_.get())),
43 // validated_(BuildCallback(this, &MyClass::Validated)) {}
45 // virtual ~MyClass() {}
47 // void ValidateAddress() const {
48 // address_.region_code = "US";
49 // address_.administrative_area = "CA";
50 // validator_.Validate(address_, filter_, &problems_, *validated_);
53 // void Validated(bool success,
54 // const AddressData& address,
55 // const FieldProblemMap& problems) {
56 // if (success && problems.empty()) {
62 // AddressData address_;
63 // FieldProblemMap filter_;
64 // FieldProblemMap problems_;
65 // const scoped_ptr<Supplier> supplier_;
66 // const scoped_ptr<AddressValidator> validator_;
67 // const scoped_ptr<const AddressValidator::Callback> validated_;
69 class AddressValidator {
71 typedef i18n::addressinput::Callback<const AddressData&,
72 const FieldProblemMap&> Callback;
74 // Does not take ownership of |supplier|.
75 AddressValidator(Supplier* supplier);
79 // Validates the |address| and populates |problems| with the validation
80 // problems, filtered according to the |filter| parameter.
82 // Set |allow_postal| to allow postal addresses, rather than only addresses
83 // describing physical locations.
85 // Set |require_name| if recipient should be considered a required field.
87 // If the |filter| is NULL or empty, then all discovered validation problems
88 // are returned. If the |filter| contains problem elements, then only those
89 // field-problem pairs present in the |filter| will be returned.
91 // Calls the |validated| callback when validation is done. All objects passed
92 // as parameters must be kept available until the callback has been called.
94 // The |success| parameter of the callback indicates whether it was possible
95 // to perform validation. If |success| is true, then |problems| will contain
96 // information about any problems found with the |address|.
97 void Validate(const AddressData& address,
100 const FieldProblemMap* filter,
101 FieldProblemMap* problems,
102 const Callback& validated) const;
105 Supplier* const supplier_;
107 DISALLOW_COPY_AND_ASSIGN(AddressValidator);
110 } // namespace addressinput
113 #endif // I18N_ADDRESSINPUT_ADDRESS_VALIDATOR_H_