Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(868)

Side by Side Diff: components/payments/payments_validators.cc

Issue 2713033004: Layered component for web payments (Closed)
Patch Set: Rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/payments/payments_validators.h"
6
7 #include "third_party/re2/src/re2/re2.h"
8 #include "url/gurl.h"
9
10 namespace payments {
11
12 // We limit the maximum length of string to 2048 bytes for security reasons.
13 static const int maximumStringLength = 2048;
14
15 bool PaymentsValidators::isValidCurrencyCodeFormat(
16 const std::string& code,
17 const std::string& system,
18 std::string* optionalErrorMessage) {
19 if (system == "urn:iso:std:iso:4217") {
20 if (RE2::FullMatch(code, "[A-Z]{3}"))
21 return true;
22
23 if (optionalErrorMessage)
24 *optionalErrorMessage = "'" + code +
25 "' is not a valid ISO 4217 currency code, should "
26 "be 3 upper case letters [A-Z]";
27
28 return false;
29 }
30
31 if (code.size() > maximumStringLength) {
32 if (optionalErrorMessage)
33 *optionalErrorMessage =
34 "The currency code should be at most 2048 characters long";
35 return false;
36 }
37 if (!GURL(system).is_valid()) {
38 if (optionalErrorMessage)
39 *optionalErrorMessage =
40 "The system should be a valid URL";
41 return false;
42 }
43 return true;
44 }
45
46 bool PaymentsValidators::isValidAmountFormat(
47 const std::string& amount,
48 std::string* optionalErrorMessage) {
49 if (RE2::FullMatch(amount, "-?[0-9]+(\\.[0-9]+)?"))
50 return true;
51
52 if (optionalErrorMessage)
53 *optionalErrorMessage = "'" + amount + "' is not a valid amount format";
54
55 return false;
56 }
57
58 bool PaymentsValidators::isValidCountryCodeFormat(
59 const std::string& code,
60 std::string* optionalErrorMessage) {
61 if (RE2::FullMatch(code, "[A-Z]{2}"))
62 return true;
63
64 if (optionalErrorMessage)
65 *optionalErrorMessage = "'" + code +
66 "' is not a valid CLDR country code, should be 2 "
67 "upper case letters [A-Z]";
68
69 return false;
70 }
71
72 bool PaymentsValidators::isValidLanguageCodeFormat(
73 const std::string& code,
74 std::string* optionalErrorMessage) {
75 if (RE2::FullMatch(code, "([a-z]{2,3})?"))
76 return true;
77
78 if (optionalErrorMessage)
79 *optionalErrorMessage = "'" + code +
80 "' is not a valid BCP-47 language code, should be "
81 "2-3 lower case letters [a-z]";
82
83 return false;
84 }
85
86 bool PaymentsValidators::isValidScriptCodeFormat(
87 const std::string& code,
88 std::string* optionalErrorMessage) {
89 if (RE2::FullMatch(code, "([A-Z][a-z]{3})?"))
90 return true;
91
92 if (optionalErrorMessage)
93 *optionalErrorMessage = "'" + code +
94 "' is not a valid ISO 15924 script code, should be "
95 "an upper case letter [A-Z] followed by 3 lower "
96 "case letters [a-z]";
97
98 return false;
99 }
100
101 bool PaymentsValidators::isValidShippingAddress(
102 const mojom::PaymentAddressPtr& address,
103 std::string* optionalErrorMessage) {
104 if (!isValidCountryCodeFormat(address->country, optionalErrorMessage))
105 return false;
106
107 if (!isValidLanguageCodeFormat(address->language_code, optionalErrorMessage))
108 return false;
109
110 if (!isValidScriptCodeFormat(address->script_code, optionalErrorMessage))
111 return false;
112
113 if (address->language_code.empty() && !address->script_code.empty()) {
114 if (optionalErrorMessage)
115 *optionalErrorMessage =
116 "If language code is empty, then script code should also be empty";
117
118 return false;
119 }
120
121 return true;
122 }
123
124 bool PaymentsValidators::isValidErrorMsgFormat(
125 const std::string& error,
126 std::string* optionalErrorMessage) {
127 if (error.length() <= maximumStringLength)
128 return true;
129
130 if (optionalErrorMessage)
131 *optionalErrorMessage =
132 "Error message should be at most 2048 characters long";
133
134 return false;
135 }
136
137 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/payments_validators.h ('k') | components/payments/payments_validators_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698