| OLD | NEW |
| (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 namespace payments { |
| 8 |
| 9 // We limit the maximum length of the currency code to 2048 bytes for security |
| 10 // reasons. |
| 11 static const int maxCurrencyCodeLength = 2048; |
| 12 |
| 13 bool PaymentsValidators::isValidCurrencyCodeFormat( |
| 14 const std::string& code, |
| 15 std::string* optionalErrorMessage) { |
| 16 if (code.size() <= maxCurrencyCodeLength) |
| 17 return true; |
| 18 |
| 19 if (optionalErrorMessage) |
| 20 *optionalErrorMessage = |
| 21 "The currency code should be at most 2048 characters long"; |
| 22 |
| 23 return false; |
| 24 } |
| 25 |
| 26 bool PaymentsValidators::isValidAmountFormat( |
| 27 const std::string& amount, |
| 28 std::string* optionalErrorMessage) { |
| 29 // if (ScriptRegexp("^-?[0-9]+(\\.[0-9]+)?$", |
| 30 // TextCaseSensitive).match(amount) == 0) |
| 31 // return true; |
| 32 |
| 33 if (optionalErrorMessage) |
| 34 *optionalErrorMessage = "'" + amount + "' is not a valid amount format"; |
| 35 |
| 36 return false; |
| 37 } |
| 38 |
| 39 bool PaymentsValidators::isValidCountryCodeFormat( |
| 40 const std::string& code, |
| 41 std::string* optionalErrorMessage) { |
| 42 // if (ScriptRegexp("^[A-Z]{2}$", TextCaseSensitive).match(code) == 0) |
| 43 // return true; |
| 44 |
| 45 if (optionalErrorMessage) |
| 46 *optionalErrorMessage = "'" + code + |
| 47 "' is not a valid CLDR country code, should be 2 " |
| 48 "upper case letters [A-Z]"; |
| 49 |
| 50 return false; |
| 51 } |
| 52 |
| 53 bool PaymentsValidators::isValidLanguageCodeFormat( |
| 54 const std::string& code, |
| 55 std::string* optionalErrorMessage) { |
| 56 // if (ScriptRegexp("^([a-z]{2,3})?$", TextCaseSensitive).match(code) == 0) |
| 57 // return true; |
| 58 |
| 59 if (optionalErrorMessage) |
| 60 *optionalErrorMessage = "'" + code + |
| 61 "' is not a valid BCP-47 language code, should be " |
| 62 "2-3 lower case letters [a-z]"; |
| 63 |
| 64 return false; |
| 65 } |
| 66 |
| 67 bool PaymentsValidators::isValidScriptCodeFormat( |
| 68 const std::string& code, |
| 69 std::string* optionalErrorMessage) { |
| 70 // if (ScriptRegexp("^([A-Z][a-z]{3})?$", TextCaseSensitive).match(code) == |
| 71 // 0) |
| 72 // return true; |
| 73 |
| 74 if (optionalErrorMessage) |
| 75 *optionalErrorMessage = "'" + code + |
| 76 "' is not a valid ISO 15924 script code, should be " |
| 77 "an upper case letter [A-Z] followed by 3 lower " |
| 78 "case letters [a-z]"; |
| 79 |
| 80 return false; |
| 81 } |
| 82 |
| 83 bool PaymentsValidators::isValidShippingAddress( |
| 84 const blink::mojom::PaymentAddressPtr& address, |
| 85 std::string* optionalErrorMessage) { |
| 86 if (!isValidCountryCodeFormat(address->country, optionalErrorMessage)) |
| 87 return false; |
| 88 |
| 89 if (!isValidLanguageCodeFormat(address->language_code, optionalErrorMessage)) |
| 90 return false; |
| 91 |
| 92 if (!isValidScriptCodeFormat(address->script_code, optionalErrorMessage)) |
| 93 return false; |
| 94 |
| 95 if (!address->language_code.size() && address->script_code.size()) { |
| 96 if (optionalErrorMessage) |
| 97 *optionalErrorMessage = |
| 98 "If language code is empty, then script code should also be empty"; |
| 99 |
| 100 return false; |
| 101 } |
| 102 |
| 103 return true; |
| 104 } |
| 105 |
| 106 } // namespace payments |
| OLD | NEW |