1// Copyright (C) 2014 Google Inc. 2// 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 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 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. 14 15#ifndef I18N_ADDRESSINPUT_VALIDATION_TASK_H_ 16#define I18N_ADDRESSINPUT_VALIDATION_TASK_H_ 17 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> 24 25#include <string> 26 27namespace i18n { 28namespace addressinput { 29 30class LookupKey; 31struct AddressData; 32 33// A ValidationTask object encapsulates the information necessary to perform 34// validation of one particular address and call a callback when that has been 35// done. Calling the Run() method will load required metadata, then perform 36// validation, call the callback and delete the ValidationTask object itself. 37class ValidationTask { 38 public: 39 ValidationTask(const AddressData& address, 40 bool allow_postal, 41 bool require_name, 42 const FieldProblemMap* filter, 43 FieldProblemMap* problems, 44 const AddressValidator::Callback& validated); 45 46 ~ValidationTask(); 47 48 // Calls supplier->Load(), with Validate() as callback. 49 void Run(Supplier* supplier) const; 50 51 private: 52 friend class ValidationTaskTest; 53 54 // Uses the address metadata of |hierarchy| to validate |address_|, writing 55 // problems found into |problems_|, then calls the |validated_| callback and 56 // deletes this ValidationTask object. 57 void Validate(bool success, 58 const LookupKey& lookup_key, 59 const Supplier::RuleHierarchy& hierarchy); 60 61 // Checks all fields for UNEXPECTED_FIELD problems. 62 void CheckUnexpectedField(const std::string& region_code) const; 63 64 // Checks all fields for MISSING_REQUIRED_FIELD problems. 65 void CheckMissingRequiredField(const std::string& region_code) const; 66 67 // Checks the hierarchical fields for UNKNOWN_VALUE problems. 68 void CheckUnknownValue(const Supplier::RuleHierarchy& hierarchy) const; 69 70 // Checks the POSTAL_CODE field for problems. 71 void CheckPostalCodeFormatAndValue( 72 const Supplier::RuleHierarchy& hierarchy) const; 73 74 // Checks the STREET_ADDRESS field for USES_P_O_BOX problems. 75 void CheckUsesPoBox(const Supplier::RuleHierarchy& hierarchy) const; 76 77 // Writes (|field|,|problem|) to |problems_|. 78 void ReportProblem(AddressField field, AddressProblem problem) const; 79 80 // Writes (|field|,|problem|) to |problems_|, if this pair should be reported. 81 void ReportProblemMaybe(AddressField field, AddressProblem problem) const; 82 83 // Returns whether (|field|,|problem|) should be reported. 84 bool ShouldReport(AddressField field, AddressProblem problem) const; 85 86 const AddressData& address_; 87 const bool allow_postal_; 88 const bool require_name_; 89 const FieldProblemMap* filter_; 90 FieldProblemMap* const problems_; 91 const AddressValidator::Callback& validated_; 92 const scoped_ptr<const Supplier::Callback> supplied_; 93 const scoped_ptr<LookupKey> lookup_key_; 94 95 DISALLOW_COPY_AND_ASSIGN(ValidationTask); 96}; 97 98} // namespace addressinput 99} // namespace i18n 100 101#endif // I18N_ADDRESSINPUT_VALIDATION_TASK_H_ 102