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 #ifndef I18N_ADDRESSINPUT_VALIDATION_TASK_H_
16 #define I18N_ADDRESSINPUT_VALIDATION_TASK_H_
18 #include <libaddressinput/address_field.h>
19 #include <libaddressinput/address_problem.h>
20 #include <libaddressinput/address_validator.h>
21 #include <libaddressinput/supplier.h>
22 #include <libaddressinput/util/basictypes.h>
23 #include <libaddressinput/util/scoped_ptr.h>
26 namespace addressinput {
32 // A ValidationTask object encapsulates the information necessary to perform
33 // validation of one particular address and call a callback when that has been
34 // done. Calling the Run() method will load required metadata, then perform
35 // validation, call the callback and delete the ValidationTask object itself.
36 class ValidationTask {
38 ValidationTask(const AddressData& address,
41 const FieldProblemMap* filter,
42 FieldProblemMap* problems,
43 const AddressValidator::Callback& validated);
47 // Calls supplier->Load(), with Validate() as callback.
48 void Run(Supplier* supplier) const;
51 friend class ValidationTaskTest;
53 // Uses the address metadata of |hierarchy| to validate |address_|, writing
54 // problems found into |problems_|, then calls the |validated_| callback and
55 // deletes this ValidationTask object.
56 void Validate(bool success,
57 const LookupKey& lookup_key,
58 const Supplier::RuleHierarchy& hierarchy);
60 // Checks all fields for UNEXPECTED_FIELD problems.
61 void CheckUnexpectedField(const std::string& region_code) const;
63 // Checks all fields for MISSING_REQUIRED_FIELD problems.
64 void CheckMissingRequiredField(const std::string& region_code) const;
66 // Checks the hierarchical fields for UNKNOWN_VALUE problems.
67 void CheckUnknownValue(
68 const Supplier::RuleHierarchy& hierarchy) const;
70 // Checks the POSTAL_CODE field for problems.
71 void CheckPostalCodeFormatAndValue(
72 const Supplier::RuleHierarchy& hierarchy) const;
74 // Checks the STREET_ADDRESS field for USES_P_O_BOX problems.
76 const Supplier::RuleHierarchy& hierarchy) const;
78 // Writes (|field|,|problem|) to |problems_|.
79 void ReportProblem(AddressField field, AddressProblem problem) const;
81 // Writes (|field|,|problem|) to |problems_|, if this pair should be reported.
82 void ReportProblemMaybe(AddressField field, AddressProblem problem) const;
84 // Returns whether (|field|,|problem|) should be reported.
85 bool ShouldReport(AddressField field, AddressProblem problem) const;
87 const AddressData& address_;
88 const bool allow_postal_;
89 const bool require_name_;
90 const FieldProblemMap* filter_;
91 FieldProblemMap* const problems_;
92 const AddressValidator::Callback& validated_;
93 const scoped_ptr<const Supplier::Callback> supplied_;
94 const scoped_ptr<LookupKey> lookup_key_;
96 DISALLOW_COPY_AND_ASSIGN(ValidationTask);
99 } // namespace addressinput
102 #endif // I18N_ADDRESSINPUT_VALIDATION_TASK_H_